Skip to content

fix(bjs): strndup crash on scripts without directory path — likely root cause of issue #2450#2466

Open
Swissola wants to merge 1 commit into
BruceDevices:devfrom
Swissola:fix/interpreter-path-crash
Open

fix(bjs): strndup crash on scripts without directory path — likely root cause of issue #2450#2466
Swissola wants to merge 1 commit into
BruceDevices:devfrom
Swissola:fix/interpreter-path-crash

Conversation

@Swissola
Copy link
Copy Markdown

Fixes a crash in run_bjs_script_headless() that is the likely root cause of #2450 (crash launching app store on Cardputer ADV).

Bug

int slash = filename.lastIndexOf('/');
scriptName = strdup(filename.c_str() + slash + 1);
scriptDirpath = strndup(filename.c_str(), slash);  // ← slash can be -1

String::lastIndexOf() returns -1 when no / is present. strndup takes size_t, so -1 coerces to SIZE_MAX (~4 GB). The allocator either triggers a heap assertion panic, or returns NULL. The subsequent JS_NewString(ctx, scriptDirpath) then dereferences NULL, crashing before the script runs.

Secondary case: when slash == 0 (file at filesystem root, e.g. /main.js), strndup(p, 0) produces an empty string "" instead of "/", breaking any relative path resolution inside the script via __dirpath.

Fix

Handle the no-slash and root-slash cases before the normal strndup path:

int slash = filename.lastIndexOf('/');
if (slash < 0) {
    scriptDirpath = strdup("/");
    scriptName = strdup(filename.c_str());
} else {
    scriptName = strdup(filename.c_str() + slash + 1);
    scriptDirpath = strndup(filename.c_str(), slash == 0 ? 1 : slash);
}

Connection to #2450

The app store fetches and invokes scripts. Depending on how the invocation path is constructed, the filename passed to run_bjs_script_headless() may lack a leading /. On a memory-constrained Cardputer ADV, the near-infinite strndup allocation attempt hits the heap guard and panics, which matches the reported crash-on-launch behaviour.

🤖 Generated with Claude Code

… path

run_bjs_script_headless() called strndup(filename.c_str(), slash) where
slash = filename.lastIndexOf('/') returns -1 when no path separator is
present. strndup takes size_t, so -1 coerces to SIZE_MAX (~4 GB) and the
allocator either panics on the heap assertion or returns NULL. The
subsequent JS_NewString(ctx, scriptDirpath) then dereferences NULL,
crashing the interpreter before the script runs.

The same bug bites when the path is at root level (slash == 0):
strndup(p, 0) produces an empty string, making __dirpath "" instead of
"/" and breaking any relative path resolution inside the script.

Fix: handle the no-slash and root-slash cases explicitly before falling
through to the normal strndup path.

Likely root cause of issue BruceDevices#2450 (crash launching the app store on
Cardputer ADV), where the app store script path may be constructed
without a leading slash depending on the invocation path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant