From b257a309772db2e9dfb745f97068aea6a32ca151 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 25 Apr 2024 20:49:17 -0700 Subject: [PATCH] node:fs: fix arg parsing of readSync (#10527) --- src/bun.js/node/node_fs.zig | 36 +++++++++++++++--------------------- test/js/node/fs/fs.test.ts | 13 +++++++++++++ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 06c5bbbd00..6b95b2aa25 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -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); + 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, - ); - + 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()) { diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index b28ff5f252..5251211357 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -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);