Skip to content

Fix segfault in lookupMaterialColor from unbounded color index - #255

Merged
ab9rf merged 2 commits into
DFHack:masterfrom
biroadama:fix-color-index-oob
Jul 12, 2026
Merged

Fix segfault in lookupMaterialColor from unbounded color index#255
ab9rf merged 2 commits into
DFHack:masterfrom
biroadama:fix-color-index-oob

Conversation

@biroadama

Copy link
Copy Markdown

Fixes a segfault in lookupMaterialColor caused by an unbounded array index
state_color[0] was used directly as an index into descriptors.colors without checking whether it was in range. For materials with an invalid or out-of-range color index (reproduced with an inorganic material, matIndex=307), this caused a segfault instead of falling back gracefully.
This adds a bounds check before dereferencing, falling back to defaultColor — consistent with the fallback behavior already used elsewhere in this same function — when the index is invalid.
Reproduced via gdb backtrace:
Thread 21 "dwarfort" received signal SIGSEGV, Segmentation fault.
lookupMaterialColor (matType=0, matIndex=307, ...) at ContentLoader.cpp:710
710 return al_map_rgb_f(cd->red, cd->green, cd->blue) * dyeColor;

state_color[0] was used directly as an index into descriptors.colors
without bounds-checking. Materials with an invalid/out-of-range color
index (observed with matIndex=307) caused a segfault instead of
falling back to defaultColor.

@ab9rf ab9rf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but needs a changelog

@ab9rf
ab9rf merged commit 389b423 into DFHack:master Jul 12, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants