Repository navigation
fix(runtime): prevent corrupt B3D hangs, bone crashes, unsupported audio exits, and empty Replace loops (7/10) - #16
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes shared Blitz runtime paths where mod/map data alone can hang or terminate the game.
.b3dmodel can leave the loader running forever, freezing the game during loading..b3dwith aBONEnode outside aMESHnode can crash through a null-pointer dereference.LoadSoundbut terminate the game withRuntimeError("unsupport format")as soon as playback starts.Replacefrom a script causes an infinite loop, with unbounded memory growth if the replacement string is nonempty.Severity and impact
.b3d, or a.b3dwith corrupted chunk lengths or texture-coordinate set countsLoadMesh/LoadAnimMeshnever returns (100% CPU), requiring forced termination.b3dwhose root is a pivot and whose bone node is outside the mesh node (a sibling/ancestor)MeshLoader::numVertices()replaceormsg_replaceThese shared runtime changes (
src/blitz3d/src/modules/bb/) apply to both Stranded II and Extended and to Windows/macOS/web builds. Loading/playback results for valid files and original numeric/save semantics are unchanged.Reproduction and evidence
1-a. Infinite loop on truncated B3D —
loader_b3d.cpp:33-53readChunk()pushes the current position and returns 0. The caller's loop then usesexitChunk()tofseekback to that position.fseekclears the EOF flag, sochunkSize()'sfeof()check passes again. With the parent chunk end beyond the file end, failedreadChunk()→fseekto the same position repeats forever..b3dto every length from 8–226 bytes and reading through actualfopenproduced 116 non-returning cases out of 219.1-b. Texture-coordinate set counts / brush texture counts —
loader_b3d.cpp:140, 179-183VRTSloops over the file's rawtc_setsvalue for each vertex. A corrupted value of0x7fffffffcauses about 2.1 billion iterations for one vertex (fuzzer-discovered input; did not return for more than 10 seconds before the fix).(n_texs-n_read)*4and(tc_size-tc_read)*4areintmultiplications. Large values can become negative, seek backward, and create a loop reading the same bytes again.2. Bone-node null dereference —
loader_b3d.cpp:225→meshloader.cpp:37-39readBone()unconditionally callsMeshLoader::numVertices()before its loop to check weight bounds. Outside aMESHnode,ml_meshisnullptr.d8d5d1b),readBone()accessesMeshLoaderonly when weights exist, so weightless bone nodes originally loaded successfully. Adding the bounds check introduced this regression.member access within null pointer of type 'MLMesh',SEGV on unknown address 0x000000000020inMeshLoader::numVertices().3. Unsupported audio formats —
audio.openal.cpp:91-94,wav_stream.cpp:36-55,ogg_stream.cpp:55OpenALChannel::setStream()throwsRTEX("unsupport format")for anything other than 8/16-bit mono/stereo. The exception reachesbbruntime_runand terminates the game.WAVAudioStreamconfirmed thatstereo24.wav,mono32.wav, andsurround16.wavall follow the successfulLoadSound→ playbackRTEXpath.LoadSoundreturns 0 (sfx_play/sfx_musicinsfx.bb, and the resource loader inressources.bb).4. Empty search string in
Replace—string.cpp:27-34nadvances only by the replacement length while the string grows by the same amount, so the loop never ends. Iftois also empty, it repeats at the same position.replace(parser_commands.bb:5456) andmsg_replace(parser_commands.bb:2294) pass script values unchanged. The original 1.118 implementation uses the same loop, so this hang predates the port.Changes
loader_b3d.cpp:truncatedflag sochunkSize()returns 0. Loading finishes using only data already read. The flag resets inclear()for each file.skip()withskipValues(), calculating byte counts in 64 bits and doing no seek for counts ≤0.meshloader.cpp:numVertices()returns 0 if no mesh is open. Weights on bone nodes outside a mesh are ignored by the existing bounds check.wav_stream.cpp,ogg_stream.cpp: reject streams other than 8/16-bit mono/stereo at header parsing, makingLoadSoundreturn 0.audio.openal.cpp: returnsfalseinstead of throwing when format mapping fails. Existing callers reclaim the channel and return channel 0.string.cpp: returns the original string unchanged for an empty search string.Validation
Completed (macOS arm64, Homebrew clang 23):
loader_b3d.cpp/meshloader.cppdirectly with scene-class stubs:.b3dvariants with 1–4 texture-coordinate sets and 0–5 components, plus a skinned mesh: vertex/UV/triangle dumps identical before and after.tc_sets=0x7ffffffffile: did not return for more than 10 seconds before → loads immediately after.wav_stream.cpp/stream.cpp: 16-bit mono and 8-bit stereo retain the playback path; 24/32-bit and six-channel inputs returnLoadSound0.bbReplacefunction body for before/after comparison: ordinary replacement results match; empty search times out before → returns the original string after.git diff --check.Not completed:
dev.py check, or actual game execution (no dependencies/cmake in this environment). Windows CI build/check must verify these.Score rationale
Harnesses confirmed hangs/crashes reproducible from mod/map data, and the fixes are narrowly scoped. Valid-data results are unchanged, keeping risk low. However, these are not issues encountered in ordinary play with only the base resources, and actual game-runtime validation remains with CI, so the score is 7.
Follow-up work
loader_b3d.cppis also changed by fix(windows): prevent invalid UTF-8 path crashes and preserve command-line arguments (7/10) #9. This PR modifiesreadChunk/chunkSize/read/skip/readVertices; fix(windows): prevent invalid UTF-8 path crashes and preserve command-line arguments (7/10) #9 changes thefopensection. Conflicts are possible.SDiv,_bbMod) terminates the Windows game runtime immediately without the original "Integer divide by zero" message because there is no SEH translator. This requires compiler changes and is excluded here.