Prevent buffer overflows console if vertical resolution > 2048.

The stores it's text in the key_lines array which is NUM_KEY_LINES *
MAXCMDLINE chars long. The code never checked for overflows, it just
assumed that a line will never be longer then 256 chars * 8 = 2048
pixel. With modern displays we can have higher vertical resolutions,
so the array will overflow sooner or later.

Fix it by clamping the maximum line width to MAXCMDLINE - 2 chars (1
for the prompt and 1 for the terminating \0). While at it increase
MAXCMDLINE to 1024 chars * 8 = 8192 pixel, which is more then 8k
resolution and should be enough for the years to come.

This is belived tot fix at least a part of issue #368.
This commit is contained in:
Yamagi Burmeister 2019-02-16 08:57:44 +01:00
parent b49f2bda6d
commit b1629fb768
2 changed files with 13 additions and 3 deletions

View file

@ -259,7 +259,14 @@ Con_CheckResize(void)
char tbuf[CON_TEXTSIZE];
float scale = SCR_GetConsoleScale();
width = ((int)(viddef.width / scale) >> 3) - 2;
/* We need to clamp the line width to MAXCMDLINE - 2,
otherwise we may overflow the text buffer if the
vertical resultion / 8 (one char == 8 pixels) is
bigger then MAXCMDLINE.
MAXCMDLINE - 2 because 1 for the prompt and 1 for
the terminating \0. */
width = ((int)(viddef.width / scale) / 8) - 2;
width = width > MAXCMDLINE - 2 ? MAXCMDLINE - 2 : width;
if (width == con.linewidth)
{

View file

@ -29,8 +29,11 @@
#include "../../common/header/shared.h" /* for qboolean etc */
/* max length of a console command line */
#define MAXCMDLINE 256
/* Max length of a console command line. 1024
* chars allow for a vertical resolution of
* 8192 pixel which should be enough for the
* years to come. */
#define MAXCMDLINE 1024
/* number of console command lines saved in history,
* must be a power of two, because we use & (NUM_KEY_LINES-1)