Skip to content

Commit e274bc7

Browse files
committed
prevent out of bound color values in doSetTile_char
some Lua code is leaking a bg of -1 into this function, resulting in an out of bounds reference to `uccolor`. this logic prohibits such out of bound reads the upstream problem still needs to be found
1 parent 7f53047 commit e274bc7

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Template for new versions:
5959
## New Features
6060

6161
## Fixes
62+
- a safety check was added to ``Screen::doSetTile_char`` fur out of bound pen color values
6263

6364
## Misc Improvements
6465

docs/dev/Lua API.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,9 @@ environment by the mandatory init file dfhack.lua:
37543754
``COLOR_GREY`` and ``COLOR_DARKGREY`` can also be spelled ``COLOR_GRAY`` and
37553755
``COLOR_DARKGRAY``.
37563756

3757+
Note: ``COLOR_RESET`` is not valid in a `Pen <lua-screen-pen>`, and using it in a Pen color field
3758+
will result in runtime warnings and may result in color flashing or other unexpected results.
3759+
37573760
* State change event codes, used by ``dfhack.onStateChange``
37583761

37593762
Available only in the `core context <lua-core-context>`, as is the event itself:

library/lua/gui/widgets/text_area/text_area_content.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function TextAreaContent:init()
3737
self.cursor = nil
3838

3939
self.main_pen = dfhack.pen.parse({
40-
bg=COLOR_RESET,
40+
bg=COLOR_BLACK,
4141
bold=true
4242
}, self.text_pen)
4343

library/modules/Screen.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ distribution.
5454
#include "df/renderer.h"
5555
#include "df/plant.h"
5656

57+
#include <algorithm>
58+
#include <span>
5759
#include <string>
5860
#include <vector>
5961
#include <map>
@@ -209,14 +211,15 @@ static bool doSetTile_char(const Pen &pen, int x, int y, bool use_graphics)
209211
*texpos_lower = df::global::init->texpos_border_interior; // basic black background
210212
}
211213

212-
auto rgb_fg = &gps->uccolor[fg][0];
213-
auto rgb_bg = &gps->uccolor[bg][0];
214-
screen[1] = rgb_fg[0];
215-
screen[2] = rgb_fg[1];
216-
screen[3] = rgb_fg[2];
217-
screen[4] = rgb_bg[0];
218-
screen[5] = rgb_bg[1];
219-
screen[6] = rgb_bg[2];
214+
if (fg >= 0 && fg <= COLOR_MAX)
215+
std::ranges::copy(gps->uccolor[fg], &screen[1]);
216+
else
217+
WARN(screen).print("in doSetTile_char, fg {} out of range\n", fg);
218+
219+
if (bg >= 0 && bg <= COLOR_MAX)
220+
std::ranges::copy(gps->uccolor[bg], &screen[4]);
221+
else
222+
WARN(screen).print("in doSetTile_char, bg {} out of range\n", bg);
220223

221224
return true;
222225
}

0 commit comments

Comments
 (0)