Commit ae25da3
committed
frontend/drivers/platform_emscripten: NULL-check allocs in OPFS and FetchFS boot init
Four unchecked allocations in the emscripten platform driver's
boot-time filesystem mount paths:
=== OPFS mount: strdup ===
char *parent = strdup(opfs_mount);
path_parent_dir(parent, strlen(parent));
strdup unchecked; strlen(NULL) NULL-derefs on OOM before
path_parent_dir even runs.
=== FetchFS manifest: calloc + strdup ===
char *line = calloc(sizeof(char), max_line_len);
...
char *base_url = strdup(line);
Pre-patch the calloc was unchecked. If it returned NULL:
- getline() glibc/musl tolerate NULL line pointer (it
allocates its own buffer internally), so the getline call
itself would not immediately crash.
- But the subsequent strdup(line) at line ~869 would run
against whatever getline stored into line - if the manifest
was empty getline would have returned -1 and we skip the
strdup. If non-empty, getline would have allocated a
buffer and the strdup works.
- However, inside the while-loop at line ~885, getline is
called again with &line still pointing at getline's
internal allocation - still fine.
- The real risk: if the initial calloc returned NULL and the
manifest had exactly one line, we'd skip the 'base URL'
parse but continue into wasmfs_create_fetch_backend which
needs base_url. This path is robustly handled by the
existing !base_url check inside the loop.
So the calloc NULL-check is defensive rather than strictly
required. But the consistency with the other allocations in
this block argues for adding it.
=== FetchFS per-entry: two strdups ===
char *parent = strdup(realfs_path);
path_parent_dir(parent, strlen(parent));
...
char *parent = strdup(fetchfs_path);
path_parent_dir(parent, strlen(parent));
Both unchecked; strlen(NULL) NULL-derefs on OOM.
=== Fix: abort() on OOM matching the init policy ===
All the other failure paths in these init blocks call
abort() (fopen failure, wasmfs_create_fetch_backend failure,
wasmfs_create_file failure, mkdir failure). These are boot-
time filesystem mount operations on the web build; if any
of them fail there is no sensible recovery path. Match that
policy: print a diagnostic and abort on each OOM.
No functional change when memory is available. On a
memory-starved browser tab (rare, but can happen on mobile
or with many other tabs open) we get a clear abort with a
log message pointing at the OOM site rather than a SIGSEGV
from strlen/memcpy of NULL.
=== Swept-clean in the same pass ===
Other frontend/drivers/ files verified clean:
- platform_unix.c: 7 sites, all NULL-checked with prior
audit comments in place (android_app, savedState, newargv
exec fork, inotify_data, path_change_data, voice_out /
speed_out accessibility).
- platform_darwin.m: 4 sites all guarded - watch_data /
watches callocs with proper cleanup chain, change_data
calloc with the prior-audit explicit-free-on-failure
handling, the watches[i].path strdup at line ~1030 is
intentionally unchecked because the field is only read by
the teardown path (for free()) - strdup failure is
benign, just means no debug-log path, teardown sees NULL
and skips the free.
- platform_ps2.c: 3 sites all intentionally designed around
OOM - they're memory-probing loops that halve the request
size until malloc succeeds or size reaches 0. Idiomatic
for the platform.
- platform_xdk.c: 2 sites both NULL-checked with prior
audit comments.
- platform_switch.c: 2 sites both NULL-checked (malloc with
early-return, realloc with tmp-pattern + goto error).
- platform_wii.c: 1 site NULL-checked.
- platform_dos.c: 1 site NULL-checked with prior audit
comment.
- platform_ctr.c: 1 site NULL-checked via 'if (code_buffer)'
wrapping the subsequent writes.
- platform_emscripten.c main() platform data
calloc: NULL-checked with prior audit comment - return 1
from main() on failure.
Thread-safety: all four sites run on the emscripten main
thread at process entry / mount setup time. No concurrency.
Reachability: emscripten memory is browser-tab-bounded; OOM
at process entry is rare but possible (especially on mobile
devices with other tabs consuming the shared memory pool).
The fix changes the failure mode from 'opaque SIGSEGV in
strlen' to 'explicit abort with OOM log message' - strictly
better for debugging.1 parent dcaec9b commit ae25da3
1 file changed
Lines changed: 38 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
811 | 811 | | |
812 | 812 | | |
813 | 813 | | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
814 | 822 | | |
815 | 823 | | |
816 | 824 | | |
| |||
860 | 868 | | |
861 | 869 | | |
862 | 870 | | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
863 | 887 | | |
864 | 888 | | |
865 | 889 | | |
| |||
909 | 933 | | |
910 | 934 | | |
911 | 935 | | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
912 | 944 | | |
913 | 945 | | |
914 | 946 | | |
| |||
920 | 952 | | |
921 | 953 | | |
922 | 954 | | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
923 | 961 | | |
924 | 962 | | |
925 | 963 | | |
| |||
0 commit comments