Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Buffer.fill with a non-null empty fill including uninitialized bytes #14199

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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