Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit a701332

Browse files
committed
Various macros assume the argument is not negative
This cuts the ASAN failures in half. From 42 to 20 on my macOS server. Macros like `isaletter()` assume the argument is not negative. But there is at least one place where it is called with a value of -1 from fcget(). Specifically, in `sh_lex()`: case S_DOT: { // Make sure next character is alpha. n = fcgetc(); if (n > 0) { if (n == '.') n = fcgetc(); if (n > 0) fcseek(-LEN); } if (isaletter(n) || n == LBRACT) continue; In the rksh.sh unit test `n` is -1 thus causing a reference to the element before one of the lexer state tables. Harden those macros against that possibility so they don't use some random data. Related #971
1 parent 788759f commit a701332

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/cmd/ksh93/include/lexstates.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@
9191

9292
#undef LEN
9393
#define LEN _Fcin.fclen
94-
#define isaname(c) ((c) > 0x7f ? isalpha(c) : sh_lexstates[ST_NAME][(c)] == S_NOP)
95-
#define isaletter(c) ((c) > 0x7f ? isalpha(c) : sh_lexstates[ST_DOL][(c)] == S_ALP && (c) != '.')
9694
#define STATE(s, c) (s[mbwide() ? ((c = fcmbget(&LEN)), LEN > 1 ? 'a' : c) : (c = fcget())])
97-
#define isadigit(c) (sh_lexstates[ST_DOL][c] == S_DIG)
95+
#define isaname(c) ((c) < 0 ? 0 : ((c) > 0x7f ? isalpha(c) : sh_lexstates[ST_NAME][(c)] == S_NOP))
96+
#define isaletter(c) ((c) < 0 ? 0 : ((c) > 0x7f ? isalpha(c) : sh_lexstates[ST_DOL][(c)] == S_ALP && (c) != '.'))
97+
#define isadigit(c) ((c) < 0 ? 0 : sh_lexstates[ST_DOL][c] == S_DIG)
9898
#define isastchar(c) ((c) == '@' || (c) == '*')
99-
#define isexp(c) (sh_lexstates[ST_MACRO][c] == S_PAT || (c) == '$' || (c) == '`')
100-
#define ismeta(c) (sh_lexstates[ST_NAME][c] == S_BREAK)
99+
#define isexp(c) ((c) < 0 ? 0 : (sh_lexstates[ST_MACRO][c] == S_PAT || (c) == '$' || (c) == '`'))
100+
#define ismeta(c) ((c) < 0 ? 0 : sh_lexstates[ST_NAME][c] == S_BREAK)
101101

102102
extern const char *sh_lexstates[ST_NONE];
103103
extern const char e_lexversion[];

0 commit comments

Comments
 (0)