Summary
Fix multiple native-code bugs and unsafe patterns in emscripten/ARimageFsetDisplay.cpp
Environment
- Product/Service: FeatureSET-Display — Emscripten/wasm native layer
- File:
emscripten/ARimageFsetDisplay.cpp
Problem Description
Several distinct bugs and unsafe patterns exist in the native C++ layer:
- NULL-pointer dereference (
lines 109–128): loadNFTMarker logs an error when ar2ReadSurfaceSet returns NULL but continues execution, then dereferences the null pointer.
exit() terminates the wasm runtime (lines 148, 154): exit(0) / exit(-1) on file errors tear down the entire wasm runtime in the browser instead of returning an error code to JS.
- Memory leak in
setup(): setup() mallocs imgBW with width*height*4, but loadNFTMarker later overwrites the pointer with surfaceSet[...]->imgBW — the original malloc'd buffer leaks. A second malloc based on uninitialized num_F_points_NFT also leaks.
- Pointer truncation in log statements:
ARLOGi("...%d", arc->imgBW) and (int)arc->imgBW truncate 64-bit pointers — use %p / uintptr_t.
- Bounds check after write (
line 153): The if (surfaceSetCount == PAGES_MAX) exit(-1) guard fires after the array has already been written; the check must be reordered to precede the write.
Expected Behavior
loadNFTMarker returns an error code when ar2ReadSurfaceSet returns NULL; execution does not continue.
- File errors return error codes to JS instead of calling
exit().
- No memory leaks in
setup() or loadNFTMarker.
- Pointer values logged correctly with
%p / uintptr_t.
- Bounds check occurs before writing to the array.
Actual Behavior
- NULL dereference crash possible after a failed surface-set load.
exit() calls kill the wasm module on file errors, breaking the page.
imgBW malloc leak on every loadNFTMarker call.
- Pointer values silently truncated in logs on 64-bit targets.
- Out-of-bounds array write possible before the
PAGES_MAX guard triggers.
Tasks
Impact
High — NULL dereference and exit() calls are crash/runtime-termination bugs. Memory leaks compound on repeated marker loads.
Additional Context
These fixes pair naturally with rebuilding the wasm binary anyway (see issue 3b — toolchain refresh). Fixing exit() calls is a prerequisite for any production use in a browser tab.
Summary
Fix multiple native-code bugs and unsafe patterns in
emscripten/ARimageFsetDisplay.cppEnvironment
emscripten/ARimageFsetDisplay.cppProblem Description
Several distinct bugs and unsafe patterns exist in the native C++ layer:
lines 109–128):loadNFTMarkerlogs an error whenar2ReadSurfaceSetreturnsNULLbut continues execution, then dereferences the null pointer.exit()terminates the wasm runtime (lines 148, 154):exit(0)/exit(-1)on file errors tear down the entire wasm runtime in the browser instead of returning an error code to JS.setup():setup()mallocsimgBWwithwidth*height*4, butloadNFTMarkerlater overwrites the pointer withsurfaceSet[...]->imgBW— the original malloc'd buffer leaks. A secondmallocbased on uninitializednum_F_points_NFTalso leaks.ARLOGi("...%d", arc->imgBW)and(int)arc->imgBWtruncate 64-bit pointers — use%p/uintptr_t.line 153): Theif (surfaceSetCount == PAGES_MAX) exit(-1)guard fires after the array has already been written; the check must be reordered to precede the write.Expected Behavior
loadNFTMarkerreturns an error code whenar2ReadSurfaceSetreturnsNULL; execution does not continue.exit().setup()orloadNFTMarker.%p/uintptr_t.Actual Behavior
exit()calls kill the wasm module on file errors, breaking the page.imgBWmalloc leak on everyloadNFTMarkercall.PAGES_MAXguard triggers.Tasks
loadNFTMarkeragainst NULL return fromar2ReadSurfaceSetand return an error codeexit(0)/exit(-1)with proper error-code returns (lines 148, 154)imgBWmalloc before overwriting the pointer inloadNFTMarker; audit thenum_F_points_NFT-based mallocARLOGicalls to use%p/(uintptr_t)arc->imgBWPAGES_MAXbounds check to occur before the array writeImpact
High — NULL dereference and
exit()calls are crash/runtime-termination bugs. Memory leaks compound on repeated marker loads.Additional Context
These fixes pair naturally with rebuilding the wasm binary anyway (see issue 3b — toolchain refresh). Fixing
exit()calls is a prerequisite for any production use in a browser tab.