Skip to content

Commit ac5420f

Browse files
committed
Guard string literal token assembly
This prevents overflow during lexing and extends robust string-escape cases.
1 parent 3e4189a commit ac5420f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lexer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ token_t lex_token_internal(bool aliasing)
508508
token_str[i - 1] = next_char;
509509
}
510510
} else {
511+
if (i >= MAX_TOKEN_LEN - 1)
512+
error("String literal too long");
511513
token_str[i++] = next_char;
512514
}
513515
if (next_char == '\\')

tests/driver.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,6 +3659,21 @@ int main() {
36593659
}
36603660
EOF
36613661

3662+
# String literal and escape coverage (additional)
3663+
try_output 0 "AZ" << 'EOF'
3664+
int main() {
3665+
printf("%s", "\\x41Z"); /* hex escape then normal char */
3666+
return 0;
3667+
}
3668+
EOF
3669+
3670+
try_output 0 "AZ" << 'EOF'
3671+
int main() {
3672+
printf("%s", "A\\132"); /* octal escape for 'Z' */
3673+
return 0;
3674+
}
3675+
EOF
3676+
36623677
# Cast zero value
36633678
try_ 0 << EOF
36643679
int main() {

0 commit comments

Comments
 (0)