Skip to content

Commit

Permalink
node:fs: fix arg parsing of readSync (#10527)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Apr 26, 2024
1 parent 189aa22 commit b257a30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2574,34 +2574,28 @@ pub const Arguments = struct {
if (current.isNumber() or current.isBigInt()) {
args.offset = current.to(u52);

if (arguments.remaining.len < 2) {
JSC.throwInvalidArguments(
"length and position are required",
.{},
ctx,
exception,
);

if (arguments.remaining.len < 1) {
JSC.throwInvalidArguments("length is required", .{}, ctx, exception);
return null;
}
if (arguments.remaining[0].isNumber() or arguments.remaining[0].isBigInt())
args.length = arguments.remaining[0].to(u52);

if (args.length == 0) {
JSC.throwInvalidArguments(
"length must be greater than 0",
.{},
ctx,
exception,
);
const arg_length = arguments.next().?;
arguments.eat();

if (arg_length.isNumber() or arg_length.isBigInt()) {
args.length = arg_length.to(u52);
}
if (args.length == 0) {
JSC.throwInvalidArguments("length must be greater than 0", .{}, ctx, exception);
return null;
}

if (arguments.remaining[1].isNumber() or arguments.remaining[1].isBigInt())
args.position = @as(ReadPosition, @intCast(arguments.remaining[1].to(i52)));

arguments.remaining = arguments.remaining[2..];
if (arguments.next()) |arg_position| {
arguments.eat();
if (arg_position.isNumber() or arg_position.isBigInt()) {
args.position = @as(ReadPosition, @intCast(arg_position.to(i52)));
}
}
} else if (current.isObject()) {
if (current.getTruthy(ctx.ptr(), "offset")) |num| {
if (num.isNumber() or num.isBigInt()) {
Expand Down
13 changes: 13 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,19 @@ describe("readSync", () => {
}
closeSync(fd);
});

it("works with offset + length passed but not position", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
{
const count = readSync(fd, four, 0, 4);
const u32 = new Uint32Array(four.buffer)[0];
expect(u32).toBe(firstFourBytes);
expect(count).toBe(4);
}
closeSync(fd);
});

it("works without position set", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
Expand Down

0 comments on commit b257a30

Please sign in to comment.