Skip to content
Open
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
46 changes: 45 additions & 1 deletion src/bun.js/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2845,9 +2845,52 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionsetgroups, (JSGlobalObject * globalObje
return JSValue::encode(jsNumber(result));
}

JSC_DEFINE_HOST_FUNCTION(Process_functionInitgroups, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);

if (callFrame->argumentCount() < 2) {
return Bun::ERR::MISSING_ARGS(scope, globalObject, "user, group"_s, 2, callFrame->argumentCount());
}

auto userValue = callFrame->argument(0);
auto groupValue = callFrame->argument(1);

// Convert user to string
Bun::V::validateString(scope, globalObject, userValue, "user"_s, jsUndefined());
RETURN_IF_EXCEPTION(scope, {});
auto userStr = userValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, {});

// Convert group to gid_t
gid_t group;
if (groupValue.isNumber()) {
Bun::V::validateUint32(scope, globalObject, groupValue, "group"_s, jsUndefined());
RETURN_IF_EXCEPTION(scope, {});
group = groupValue.toUInt32(globalObject);
} else if (groupValue.isString()) {
auto resolvedGroup = maybe_gid_by_name(scope, globalObject, groupValue);
RETURN_IF_EXCEPTION(scope, {});
group = resolvedGroup.toUInt32(globalObject);
} else {
return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, "group"_s, "number or string"_s, groupValue);
}

// Call initgroups
auto result = initgroups(userStr.utf8().data(), group);
if (result != 0) {
throwSystemError(scope, globalObject, "initgroups"_s, errno);
return {};
}

RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(jsUndefined());
Comment on lines +2882 to +2888
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Redundant exception check after error return.

Line 2887 RETURN_IF_EXCEPTION(scope, {}); is unreachable. If initgroups fails, the exception is thrown at line 2883 and the function returns at line 2884. If initgroups succeeds, no exception is pending. This line can be safely removed.

🧹 Proposed cleanup
     auto result = initgroups(userStr.utf8().data(), group);
     if (result != 0) {
         throwSystemError(scope, globalObject, "initgroups"_s, errno);
         return {};
     }
 
-    RETURN_IF_EXCEPTION(scope, {});
     return JSValue::encode(jsUndefined());
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (result != 0) {
throwSystemError(scope, globalObject, "initgroups"_s, errno);
return {};
}
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(jsUndefined());
if (result != 0) {
throwSystemError(scope, globalObject, "initgroups"_s, errno);
return {};
}
return JSValue::encode(jsUndefined());
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/bun.js/bindings/BunProcess.cpp` around lines 2882 - 2888, The
RETURN_IF_EXCEPTION(scope, {}); after the initgroups call is redundant and
unreachable: when initgroups fails throwSystemError(scope, globalObject,
"initgroups"_s, errno) already throws and the function returns; when it succeeds
there is no pending exception. Remove the lone RETURN_IF_EXCEPTION(scope, {});
statement from the function in BunProcess.cpp (the block that calls initgroups
and throwSystemError) so only the error throw and the final return
JSValue::encode(jsUndefined()) remain.

}

#endif

JSC_DEFINE_HOST_FUNCTION(Process_functionAssert, (JSGlobalObject * globalObject, CallFrame* callFrame))
JSC_DEFINE_HOST_FUNCTION(Process_functionAssert, (JSC::JSGlobalObject * globalObject, CallFrame* callFrame))
{
auto& vm = JSC::getVM(globalObject);
auto throwScope = DECLARE_THROW_SCOPE(vm);
Expand Down Expand Up @@ -4051,6 +4094,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu
seteuid Process_functionseteuid Function 1
setgid Process_functionsetgid Function 1
setgroups Process_functionsetgroups Function 1
initgroups Process_functionInitgroups Function 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify Node.js process.initgroups.length is 2
node -e "console.log('process.initgroups.length:', process.initgroups.length)"

Repository: oven-sh/bun

Length of output: 83


🏁 Script executed:

# Check if the file exists and read the relevant lines
file_path="src/bun.js/bindings/BunProcess.cpp"
if [ -f "$file_path" ]; then
  echo "=== Line 4097 (LUT entry) ==="
  sed -n '4095,4099p' "$file_path"
  echo ""
  echo "=== Lines 2853-2855 (enforcement check) ==="
  sed -n '2851,2857p' "$file_path"
else
  echo "File not found: $file_path"
fi

Repository: oven-sh/bun

Length of output: 773


Incorrect function length in LUT entry.

The initgroups function requires 2 arguments (user and group), as enforced by the missing args check at lines 2853-2855. The LUT entry should specify Function 2 to match Node.js where process.initgroups.length === 2.

🔧 Proposed fix
-  initgroups                       Process_functionInitgroups                          Function 1
+  initgroups                       Process_functionInitgroups                          Function 2
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
initgroups Process_functionInitgroups Function 1
initgroups Process_functionInitgroups Function 2
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/bun.js/bindings/BunProcess.cpp` at line 4097, The LUT entry for
initgroups is declared with the wrong arity; update the lookup table entry that
currently reads "initgroups                       Process_functionInitgroups    
Function 1" to specify Function 2 so the function length matches the actual
implementation and Node.js behavior; ensure the symbol names to change are
initgroups and Process_functionInitgroups in the LUT so
process.initgroups.length === 2.

setuid Process_functionsetuid Function 1
#endif
@end
Expand Down