fix level stats widget string overflow (#1228)

* set HU_MAXLINELENGTH to 120
This commit is contained in:
Roman Fomin 2023-10-17 22:52:37 +07:00 committed by GitHub
parent f69d621337
commit 3c4823a07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 12 deletions

View File

@ -100,7 +100,7 @@ void HUlib_clear_all_lines (hu_multiline_t *const m)
static boolean add_char_to_line(hu_line_t *const t, const char ch) static boolean add_char_to_line(hu_line_t *const t, const char ch)
{ {
if (t->len == HU_MAXLINELENGTH) if (t->len == HU_MAXLINELENGTH - 1)
return false; return false;
else else
{ {

View File

@ -48,7 +48,7 @@ extern patch_t **hu_font;
#define CR_ORIG (-1) // [FG] reset to original color #define CR_ORIG (-1) // [FG] reset to original color
#define HU_MAXLINELENGTH 80 #define HU_MAXLINELENGTH 120
//jff 2/26/98 maximum number of messages allowed in refresh list //jff 2/26/98 maximum number of messages allowed in refresh list
#define HU_MAXMESSAGES 8 #define HU_MAXMESSAGES 8

View File

@ -1091,7 +1091,7 @@ static void HU_widget_build_monsec(void)
{ {
char hud_monsecstr[HU_MAXLINELENGTH]; char hud_monsecstr[HU_MAXLINELENGTH];
int i, playerscount; int i, playerscount;
char kills_str[60]; char kills_str[HU_MAXLINELENGTH];
int offset = 0; int offset = 0;
int kills = 0, kills_color, kills_percent, kills_percent_color; int kills = 0, kills_color, kills_percent, kills_percent_color;
@ -1105,12 +1105,12 @@ static void HU_widget_build_monsec(void)
{ {
if (playerscount == 0) if (playerscount == 0)
{ {
offset = sprintf(kills_str, offset = M_snprintf(kills_str, sizeof(kills_str),
"\x1b%c%d", color, players[i].killcount); "\x1b%c%d", color, players[i].killcount);
} }
else else
{ {
offset += sprintf(kills_str + offset, offset += M_snprintf(kills_str + offset, sizeof(kills_str) - offset,
"\x1b%c+\x1b%c%d", '0'+CR_GREEN, color, players[i].killcount); "\x1b%c+\x1b%c%d", '0'+CR_GREEN, color, players[i].killcount);
} }
@ -1129,36 +1129,40 @@ static void HU_widget_build_monsec(void)
if (playerscount > 1) if (playerscount > 1)
{ {
offset = sprintf(hud_monsecstr, offset = M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cK %s \x1b%c%d/%d", "\x1b%cK %s \x1b%c%d/%d",
'0'+CR_RED, kills_str, kills_color, kills, totalkills); '0'+CR_RED, kills_str, kills_color, kills, totalkills);
} }
else else
{ {
offset = sprintf(hud_monsecstr, offset = M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cK \x1b%c%d/%d", "\x1b%cK \x1b%c%d/%d",
'0'+CR_RED, kills_color, plr->killcount, totalkills); '0'+CR_RED, kills_color, plr->killcount, totalkills);
} }
if (extrakills) if (extrakills)
{ {
offset += sprintf(hud_monsecstr + offset, "+%d", extrakills); offset += M_snprintf(hud_monsecstr + offset, sizeof(hud_monsecstr) - offset,
"+%d", extrakills);
} }
if (hud_threelined_widgets) if (hud_threelined_widgets)
{ {
sprintf(hud_monsecstr + offset, " \x1b%c%d%%", kills_percent_color, kills_percent); M_snprintf(hud_monsecstr + offset, sizeof(hud_monsecstr) - offset,
" \x1b%c%d%%", kills_percent_color, kills_percent);
HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr); HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr);
sprintf(hud_monsecstr, "\x1b%cI \x1b%c%d/%d", ('0'+CR_RED), items_color, items, totalitems); M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cI \x1b%c%d/%d", ('0'+CR_RED), items_color, items, totalitems);
HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr); HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr);
sprintf(hud_monsecstr, "\x1b%cS \x1b%c%d/%d", ('0'+CR_RED), secrets_color, secrets, totalsecret); M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cS \x1b%c%d/%d", ('0'+CR_RED), secrets_color, secrets, totalsecret);
HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr); HUlib_add_string_to_cur_line(&w_monsec, hud_monsecstr);
} }
else else
{ {
sprintf(hud_monsecstr + offset, M_snprintf(hud_monsecstr + offset, sizeof(hud_monsecstr) - offset,
" \x1b%c%d%% \x1b%cI \x1b%c%d/%d \x1b%cS \x1b%c%d/%d", " \x1b%c%d%% \x1b%cI \x1b%c%d/%d \x1b%cS \x1b%c%d/%d",
kills_percent_color, kills_percent, kills_percent_color, kills_percent,
'0'+CR_RED, items_color, items, totalitems, '0'+CR_RED, items_color, items, totalitems,