Skip to content

Commit

Permalink
Prevent Q_IsColorString from asserting on negative ascii chars
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman authored and timangus committed Dec 14, 2018
1 parent 09166ba commit a6df505
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions code/qcommon/q_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// q_shared.c -- stateless support routines that are included in each code dll
#include "q_shared.h"

// ^[0-9a-zA-Z]
qboolean Q_IsColorString(const char *p) {
if (!p)
return qfalse;

if (p[0] != Q_COLOR_ESCAPE)
return qfalse;

if (p[1] == 0)
return qfalse;

// isalnum expects a signed integer in the range -1 (EOF) to 255, or it might assert on undefined behaviour
// a dereferenced char pointer has the range -128 to 127, so we just need to rangecheck the negative part
if (p[1] < 0)
return qfalse;

if (isalnum(p[1]) == 0)
return qfalse;

return qtrue;
}

float Com_Clamp( float min, float max, float value ) {
if ( value < min ) {
return min;
Expand Down
2 changes: 1 addition & 1 deletion code/qcommon/q_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ extern vec4_t colorMdGrey;
extern vec4_t colorDkGrey;

#define Q_COLOR_ESCAPE '^'
#define Q_IsColorString(p) ((p) && *(p) == Q_COLOR_ESCAPE && *((p)+1) && isalnum(*((p)+1))) // ^[0-9a-zA-Z]
qboolean Q_IsColorString(const char *p); // ^[0-9a-zA-Z]

#define COLOR_BLACK '0'
#define COLOR_RED '1'
Expand Down

0 comments on commit a6df505

Please sign in to comment.