Repository navigation
Fix undefined behavior in sound, sith and std - #68
Merged
Merged
Conversation
Three loops decremented the channel pointer before checking the count, so the last step pointed before Sound_apChannels (NULL - 1 with no channels). Same iteration order. Validation (Linux host, MSVC not run): - GCC ASan+UBSan: Canyonlands headless, 1,000 frames, no reports (was 15 locations). End state identical to the Clang native run and to the run before this change. - MinGW i686 Debug and standalone Release build. Unit tests: 13 of 14 pass; std fails with the 2 known stdConffile tests. std.System: 61 tests, 2 known failures, 2 ignored. Regression run: 0.0025, OK. - Not run: MSVC DX6/DX9 builds, host check.
With size 0 the array stays NULL, and memset on NULL is undefined. Validation (Linux host, MSVC not run): - GCC ASan+UBSan: Canyonlands headless, 1,000 frames, no reports (was 15 locations). End state identical to the Clang native run and to the run before this change. - MinGW i686 Debug and standalone Release build. Unit tests: 13 of 14 pass; std fails with the 2 known stdConffile tests. std.System: 61 tests, 2 known failures, 2 ignored. Regression run: 0.0025, OK. - Not run: MSVC DX6/DX9 builds, host check.
The marker sits right after the user's bytes, so its address is often unaligned. It's now always 4 bytes, which is what Free already checked. Validation (Linux host, MSVC not run): - GCC ASan+UBSan: Canyonlands headless, 1,000 frames, no reports (was 15 locations). End state identical to the Clang native run and to the run before this change. - MinGW i686 Debug and standalone Release build. Unit tests: 13 of 14 pass; std fails with the 2 known stdConffile tests. std.System: 61 tests, 2 known failures, 2 ignored. Regression run: 0.0025, OK. - Not run: MSVC DX6/DX9 builds, host check.
sithCog_CogStatus checked index > numCogs, so index == numCogs read one element past aCogs. Adds the first sithCog tests to sithTests: the boundary index, an index past the array, and the last valid index. Validation (MinGW-w64 i686 GCC 13, DX9 Debug, under Wine 10): - Passed: sith 41/41, including the 3 new sithCog tests. With the old bound, TestRejectsIndexEqualToNumCogs fails (expected 0, was 1). - Unit suites otherwise unchanged: 12 of 13 pass; std fails the 2 known stdConffile tests. - Not run: MSVC, DX6, Release, guard and QOL profiles.
seelenkuchen
marked this pull request as ready for review
November 2, 2026 22:38
smlu
approved these changes
Nov 8, 2026
Owner
|
Thanks for providing these fixes! |
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.
Four small fixes for undefined behavior. Three were found by running the engine under AddressSanitizer and UndefinedBehaviorSanitizer on my fork (a native Linux build loading Canyonlands for 1,000 frames). The fourth is an off-by-one found while reading the code.
On the 32-bit builds this repository ships, the first three produce the same results as before. Only the last changes behavior, and only in a debug console command.
Commits
Sound_apChannels, and to NULL − 1 when there are no channels. The iteration order is the same.memseton NULL is undefined even with a size of 0.void*oruint32_t*is undefined. It's now written and read as 4 bytes throughmemcpy, which compiles to the same access on x86.Freealready checked 4 bytes.sithCog_CogStatus(the dev-modecogstatuscommand) checkedindex > numCogs, socogstatus <numCogs>read one element pastaCogs. It now checks>=. This adds the firstsithCogtests tosithTests, andDocs/Testing.mdmentions the new coverage.Validation
This was built and tested here on this branch's exact changes, on top of a local MinGW-w64 build setup that isn't part of this PR (GCC 13, i686, DX9 Debug, under Wine 10).
sith41/41, including the 3 newsithCogtests. With the old bound,TestRejectsIndexEqualToNumCogsfails (expected 0, got 1). The other unit suites are unchanged: 12 of 13 pass.std263/265 (astdConffile%ascan and the__LINE__value in a Printf message, both specific to GCC/Wine).Draft until CI has run.