Skip to content

Commit

Permalink
js: ERR_INVALID_ARG_TYPE and ERR_BUFFER_OUT_OF_BOUNDS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jan 23, 2025
1 parent f1a5ac4 commit 1c52106
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
24 changes: 19 additions & 5 deletions src/bun.js/bindings/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl

result.append("The "_s);

if (arg_name.contains(' ')) {
if (arg_name.endsWith(" argument"_s)) {
result.append(arg_name);
} else {
result.append("\""_s);
Expand All @@ -379,13 +379,18 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl
unsigned length = expected_types.size();
if (length == 1) {
result.append(expected_types.at(0).toWTFString(globalObject));
} else if (length == 2) {
result.append(expected_types.at(0).toWTFString(globalObject));
result.append(" or "_s);
result.append(expected_types.at(1).toWTFString(globalObject));
} else {
for (unsigned i = 0; i < length - 1; i++) {
JSValue expected_type = expected_types.at(i);
if (i > 0) result.append(", "_s);
result.append(expected_type.toWTFString(globalObject));
RETURN_IF_EXCEPTION(scope, {});
result.append(", "_s);
}
result.append(" or "_s);
result.append("or "_s);
result.append(expected_types.at(length - 1).toWTFString(globalObject));
}

Expand Down Expand Up @@ -1021,6 +1026,17 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject
return JSC::JSValue::encode(createError(globalObject, error, message));
}

case ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS: {
auto arg0 = callFrame->argument(1);
if (!arg0.isUndefined()) {
auto str0 = arg0.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, {});
auto message = makeString("\""_s, str0, "\" is outside of buffer bounds"_s);
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, message));
}
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, "Attempt to access memory outside buffer bounds"_s));
}

case ErrorCode::ERR_IPC_DISCONNECTED:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_IPC_DISCONNECTED, "IPC channel is already disconnected"_s));
case ErrorCode::ERR_SERVER_NOT_RUNNING:
Expand All @@ -1031,8 +1047,6 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_SOCKET_BAD_TYPE, "Bad socket type specified. Valid types are: udp4, udp6"_s));
case ErrorCode::ERR_ZLIB_INITIALIZATION_FAILED:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_ZLIB_INITIALIZATION_FAILED, "Initialization failed"_s));
case ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, "Attempt to access memory outside buffer bounds"_s));
case ErrorCode::ERR_IPC_ONE_PIPE:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_IPC_ONE_PIPE, "Child process can have only one IPC pipe"_s));
case ErrorCode::ERR_SOCKET_ALREADY_BOUND:
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ function spawnSync(file, args, options) {
} else if (typeof input === "string") {
bunStdio[0] = Buffer.from(input, encoding || "utf8");
} else {
throw $ERR_INVALID_ARG_TYPE(`options.stdio[0]`, ["Buffer", "TypedArray", "DataView", "string"], input);
throw $ERR_INVALID_ARG_TYPE(`options.stdio[0]`, ["string", "Buffer", "TypedArray", "DataView"], input);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/js/node/dgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ function sliceBuffer(buffer, offset, length) {
if (typeof buffer === "string") {
buffer = Buffer.from(buffer);
} else if (!ArrayBuffer.isView(buffer)) {
throw $ERR_INVALID_ARG_TYPE("buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer);
throw $ERR_INVALID_ARG_TYPE("buffer", ["string", "Buffer", "TypedArray", "DataView"], buffer);
}

offset = offset >>> 0;
Expand Down Expand Up @@ -562,12 +562,12 @@ Socket.prototype.send = function (buffer, offset, length, port, address, callbac
if (typeof buffer === "string") {
list = [Buffer.from(buffer)];
} else if (!ArrayBuffer.isView(buffer)) {
throw $ERR_INVALID_ARG_TYPE("buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer);
throw $ERR_INVALID_ARG_TYPE("buffer", ["string", "Buffer", "TypedArray", "DataView"], buffer);
} else {
list = [buffer];
}
} else if (!(list = fixBufferList(buffer))) {
throw $ERR_INVALID_ARG_TYPE("buffer list arguments", ["Buffer", "TypedArray", "DataView", "string"], buffer);
throw $ERR_INVALID_ARG_TYPE("buffer list arguments", ["string", "Buffer", "TypedArray", "DataView"], buffer);
}

if (!connected) port = validatePort(port, "Port", false);
Expand Down
9 changes: 3 additions & 6 deletions test/js/node/test/parallel/needs-test/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ test('Additional asserts', () => {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
// message: 'The "error" argument must be of type Object, Error, Function or RegExp. Received: "Error message"',
message: 'The "error" argument must be of type Object, Error, Function or RegExp.' + invalidArgTypeHelper('Error message'),
message: 'The "error" argument must be of type Object, Error, Function, or RegExp.' + invalidArgTypeHelper('Error message'),
}
);

Expand All @@ -1059,7 +1059,7 @@ test('Additional asserts', () => {
() => assert.throws(() => {}, input),
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "error" argument must be of type Object, Error, Function or RegExp.' + invalidArgTypeHelper(input)
message: 'The "error" argument must be of type Object, Error, Function, or RegExp.' + invalidArgTypeHelper(input)

}
);
Expand Down Expand Up @@ -1317,8 +1317,7 @@ test('Additional assert', () => {
'+ actual - expected\n' +
'\n' +
"+ 'test test'\n" +
"- 'test foobar'\n" +
' ^\n',
"- 'test foobar'\n"
}
);

Expand Down Expand Up @@ -1597,5 +1596,3 @@ test('assert/strict exists', () => {

/* eslint-enable no-restricted-syntax */
/* eslint-enable no-restricted-properties */


13 changes: 7 additions & 6 deletions test/js/node/test/parallel/test-dgram-send-bad-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function checkArgs(connected) {
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /The "buffer" argument must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received undefined/
message: 'The "buffer" argument must be of type string, Buffer, TypedArray, or DataView. Received undefined'
}
);

Expand Down Expand Up @@ -95,7 +95,7 @@ function checkArgs(connected) {
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError',
message: /"offset" is outside of buffer bounds|Attempt to access memory outside buffer bounds/,
message: '"offset" is outside of buffer bounds',
}
);

Expand All @@ -104,7 +104,7 @@ function checkArgs(connected) {
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError',
message: /"length" is outside of buffer bounds|Attempt to access memory outside buffer bounds/,
message: '"length" is outside of buffer bounds',
}
);

Expand All @@ -113,7 +113,7 @@ function checkArgs(connected) {
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError',
message: /"length" is outside of buffer bounds|Attempt to access memory outside buffer bounds/,
message: '"length" is outside of buffer bounds',
}
);
}
Expand All @@ -129,7 +129,7 @@ function checkArgs(connected) {
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /The "buffer" argument must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received type number \(23\)/,
message: 'The "buffer" argument must be of type string, Buffer, TypedArray, or DataView. Received type number (23)'
}
);

Expand All @@ -139,7 +139,8 @@ function checkArgs(connected) {
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /The "?buffer list arguments(" argument)? must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received an instance of Array/
message: 'The "buffer list arguments" argument must be of type string, Buffer, TypedArray, or DataView. ' +
'Received an instance of Array'
}
);
}
Expand Down

0 comments on commit 1c52106

Please sign in to comment.