Skip to content

Commit

Permalink
fix Buffer.fill with a non-null empty fill including uninitialized bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Sep 27, 2024
1 parent 5e97fb8 commit 22f1942
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/bun.js/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ static inline JSC::EncodedJSValue jsBufferConstructorFunction_allocBody(JSC::JSG
}
auto startPtr = uint8Array->typedVector() + start;
auto str_ = value.toWTFString(lexicalGlobalObject);
if (str_.isEmpty()) {
memset(startPtr, 0, length);
RELEASE_AND_RETURN(scope, JSC::JSValue::encode(uint8Array));
}

ZigString str = Zig::toZigString(str_);

if (UNLIKELY(!Bun__Buffer_fill(&str, startPtr, end - start, encoding))) {
Expand Down
4 changes: 4 additions & 0 deletions test/js/node/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,10 @@ for (let withOverridenBufferWrite of [false, true]) {
expect(buf[4]).toBe(128);
});

it("fill(N, empty string) should be the same as fill(N) and not include any uninitialized bytes", () => {
expect(Buffer.alloc(100, "")).toEqual(Buffer.alloc(100));
});

// https://github.com/joyent/node/issues/1758
it("check for fractional length args, junk length args, etc.", () => {
// Call .fill() first, stops valgrind warning about uninitialized memory reads.
Expand Down

0 comments on commit 22f1942

Please sign in to comment.