Fix undefined behavior from array “shape-punning” #1194
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.
In
stb_voxel_render.h
, there were three cases where a 2D array of dimension[X][Y]
was iterated as a 1D array of dimension[1][X*Y]
. While this is clever and is correct in terms of the actual memory layout, a second index outside the corresponding dimension ([i][j]
,j >= Y
]) actually produces undefined behavior and gives the compiler freedom to do all sorts of terrible things.The same thing happens in
stb_tilemap_editor.h
,tests/caveview/cave_mesher.c
, andtests/resample_test.cpp
.Prior to this commit, a compiler warning regarding the undefined behavior appears on gcc 11.2.1 for at least some of these cases when the tests are compiled with
-Waggressive-loop-optimizations
(included in-Wall
). For example:This commit fixes the undefined behavior by iterating these 2D arrays with the conventional nested loops.
(This is an important but conceptually trivial fix, originally suggested by Jerry James. I don’t believe it warrants adding anyone to the list of contributors.)