From 2921b23116f63e83ba4c504e2dcec257d3ab86b0 Mon Sep 17 00:00:00 2001 From: "Laurent Alonso(fr)" Date: Thu, 7 May 2020 20:31:37 +0200 Subject: [PATCH] asan: avoid some memory problems... --- src/dialogxml/widgets/control.cpp | 9 +++++++++ src/game/boe.locutils.cpp | 7 ++++++- src/game/boe.text.cpp | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/dialogxml/widgets/control.cpp b/src/dialogxml/widgets/control.cpp index 5285b0cc..7ec8ff03 100644 --- a/src/dialogxml/widgets/control.cpp +++ b/src/dialogxml/widgets/control.cpp @@ -319,6 +319,10 @@ static unsigned char applyShift(unsigned char c){ '~', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', }; + if (c<' ') + return ' '; + if (c>=0x7f) + return c; return afterShift[c - ' ']; } @@ -331,6 +335,11 @@ static unsigned char removeShift(unsigned char c){ '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\\',']', '`', }; + // ASAN: c can be called with 0 by cControl::getAttachedKeyDescription() + if (c<' ') + return ' '; + if (c>=0x7f) + return c; return afterUnShift[c - ' ']; } diff --git a/src/game/boe.locutils.cpp b/src/game/boe.locutils.cpp index 902ba5da..0e9ea5b5 100644 --- a/src/game/boe.locutils.cpp +++ b/src/game/boe.locutils.cpp @@ -215,7 +215,12 @@ short combat_obscurity(short x, short y) { } ter_num_t coord_to_ter(short x,short y) { - return is_out() ? univ.out[x][y] : univ.town->terrain(x,y); + if (is_out()) + return univ.out[x][y]; + // ASAN called by place_road(..., false) with (x,y)=(univ.town->max_dim,univ.town->max_dim) + if (x<0 || y<0 || x>=univ.town->max_dim || y>=univ.town->max_dim) + return 0; + return univ.town->terrain(x,y); } //// diff --git a/src/game/boe.text.cpp b/src/game/boe.text.cpp index 5b8b5953..94a31c69 100644 --- a/src/game/boe.text.cpp +++ b/src/game/boe.text.cpp @@ -979,7 +979,8 @@ void add_string_to_buf(std::string str, unsigned short indent) { size_t last = 0, new_last = str.find_last_not_of(' '); while(last < str.length() && str[last] == text_buffer[prev_pointer].line[last]) last++; - while(text_buffer[prev_pointer].line[--last] == ' '); + // ASAN last can be 0 + while(last>0 && text_buffer[prev_pointer].line[--last] == ' '); bool is_dup = false; if(last == new_last) { size_t num_pos = 0;