Skip to content

Commit

Permalink
fix level stats widget string overflow (#1228)
Browse files Browse the repository at this point in the history
* set HU_MAXLINELENGTH to 120
  • Loading branch information
rfomin authored Oct 17, 2023
1 parent f69d621 commit 3c4823a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/hu_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
if (t->len == HU_MAXLINELENGTH)
if (t->len == HU_MAXLINELENGTH - 1)
return false;
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/hu_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern patch_t **hu_font;

#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
#define HU_MAXMESSAGES 8
Expand Down
24 changes: 14 additions & 10 deletions src/hu_stuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ static void HU_widget_build_monsec(void)
{
char hud_monsecstr[HU_MAXLINELENGTH];
int i, playerscount;
char kills_str[60];
char kills_str[HU_MAXLINELENGTH];
int offset = 0;

int kills = 0, kills_color, kills_percent, kills_percent_color;
Expand All @@ -1105,12 +1105,12 @@ static void HU_widget_build_monsec(void)
{
if (playerscount == 0)
{
offset = sprintf(kills_str,
offset = M_snprintf(kills_str, sizeof(kills_str),
"\x1b%c%d", color, players[i].killcount);
}
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);
}

Expand All @@ -1129,36 +1129,40 @@ static void HU_widget_build_monsec(void)

if (playerscount > 1)
{
offset = sprintf(hud_monsecstr,
offset = M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cK %s \x1b%c%d/%d",
'0'+CR_RED, kills_str, kills_color, kills, totalkills);
}
else
{
offset = sprintf(hud_monsecstr,
offset = M_snprintf(hud_monsecstr, sizeof(hud_monsecstr),
"\x1b%cK \x1b%c%d/%d",
'0'+CR_RED, kills_color, plr->killcount, totalkills);
}

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)
{
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);

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);

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);
}
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",
kills_percent_color, kills_percent,
'0'+CR_RED, items_color, items, totalitems,
Expand Down

0 comments on commit 3c4823a

Please sign in to comment.