Skip to content

Commit 9783ca9

Browse files
authored
Sanity checks for ctype functions (#342)
* fixup: sanity checks for ctype functions * format * more grep fixes * don't check if constrained by type
1 parent e996437 commit 9783ca9

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/pcre2_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,11 @@ if (c == CHAR_LEFT_CURLY_BRACKET)
21942194
{
21952195
if (ptr >= cb->end_pattern) goto ERROR_RETURN;
21962196
c = *ptr++;
2197+
#if PCRE2_CODE_UNIT_WIDTH != 8
2198+
while (c == '_' || c == '-' || (c <= 0xff && isspace(c)))
2199+
#else
21972200
while (c == '_' || c == '-' || isspace(c))
2201+
#endif
21982202
{
21992203
if (ptr >= cb->end_pattern) goto ERROR_RETURN;
22002204
c = *ptr++;

src/pcre2_convert.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@ Returns: !0 => character is found in the class
540540
static BOOL
541541
convert_glob_char_in_class(int class_index, PCRE2_UCHAR c)
542542
{
543+
#if PCRE2_CODE_UNIT_WIDTH != 8
544+
if (c > 0xff)
545+
{
546+
/* ctype functions are not sane for c > 0xff */
547+
return 0;
548+
}
549+
#endif
550+
543551
switch (class_index)
544552
{
545553
case 1: return isalnum(c);

src/pcre2grep.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ decode_ANSI_colour(const char *cs)
796796
WORD result = csbi.wAttributes;
797797
while (*cs)
798798
{
799-
if (isdigit(*cs))
799+
if (isdigit((unsigned char)(*cs)))
800800
{
801801
int code = atoi(cs);
802802
if (code == 1) result |= 0x08;
@@ -810,7 +810,7 @@ while (*cs)
810810
else if (code >= 90 && code <= 97) result = (result & 0xF0) | BGR_RGB(code - 90) | 0x08;
811811
else if (code >= 100 && code <= 107) result = (result & 0x0F) | (BGR_RGB(code - 100) << 4) | 0x80;
812812

813-
while (isdigit(*cs)) cs++;
813+
while (isdigit((unsigned char)(*cs))) cs++;
814814
}
815815
if (*cs) cs++;
816816
}
@@ -1989,7 +1989,7 @@ switch (*(++string))
19891989
case '{':
19901990
brace = TRUE;
19911991
string++;
1992-
if (!isdigit(*string)) /* Syntax error: a decimal number required. */
1992+
if (!isdigit((unsigned char)(*string))) /* Syntax error: a decimal number required. */
19931993
{
19941994
if (!callout)
19951995
fprintf(stderr, "pcre2grep: Error in output text at offset %d: %s\n",
@@ -4036,7 +4036,7 @@ for (i = 1; i < argc; i++)
40364036

40374037
if (op->type == OP_OP_NUMBER || op->type == OP_OP_NUMBERS)
40384038
{
4039-
if (isdigit((unsigned char)s[1])) break;
4039+
if (isdigit((unsigned char)(s[1]))) break;
40404040
}
40414041
else /* Check for an option with data */
40424042
{
@@ -4520,7 +4520,7 @@ for (fn = file_lists; fn != NULL; fn = fn->next)
45204520
{
45214521
int frc;
45224522
char *end = buffer + (int)strlen(buffer);
4523-
while (end > buffer && isspace(end[-1])) end--;
4523+
while (end > buffer && isspace((unsigned char)(end[-1]))) end--;
45244524
*end = 0;
45254525
if (*buffer != 0)
45264526
{

0 commit comments

Comments
 (0)