Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,8 @@ hashmap_node_t *hashmap_node_new(char *key, void *val)
}

node->key = arena_alloc(HASHMAP_ARENA, len + 1);

if (!node->key) {
printf("Failed to allocate hashmap_node_t key with size %d\n", len + 1);
free(node);
return NULL;
}

Expand Down
20 changes: 20 additions & 0 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ token_t lex_token_internal(bool aliasing)
int i = 0;

do {
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
} while (is_alnum(read_char(false)));
token_str[i] = 0;
Expand Down Expand Up @@ -328,30 +330,40 @@ token_t lex_token_internal(bool aliasing)

if (is_digit(next_char)) {
int i = 0;
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
read_char(false);

if (token_str[0] == '0' && ((next_char | 32) == 'x')) {
/* Hexadecimal: starts with 0x or 0X */
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;

read_char(false);
if (!is_hex(next_char))
error("Invalid hex literal: expected hex digit after 0x");

do {
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
} while (is_hex(read_char(false)));

} else if (token_str[0] == '0' && ((next_char | 32) == 'b')) {
/* Binary: starts with 0b or 0B */
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;

read_char(false);
if (next_char != '0' && next_char != '1')
error("Invalid binary literal: expected 0 or 1 after 0b");

do {
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
read_char(false);
} while (next_char == '0' || next_char == '1');
Expand All @@ -361,13 +373,17 @@ token_t lex_token_internal(bool aliasing)
while (is_digit(next_char)) {
if (next_char >= '8')
error("Invalid octal digit: must be in range 0-7");
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
read_char(false);
}

} else {
/* Decimal */
while (is_digit(next_char)) {
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
read_char(false);
}
Expand Down Expand Up @@ -492,6 +508,8 @@ token_t lex_token_internal(bool aliasing)
token_str[i - 1] = next_char;
}
} else {
if (i >= MAX_TOKEN_LEN - 1)
error("String literal too long");
token_str[i++] = next_char;
}
if (next_char == '\\')
Expand Down Expand Up @@ -744,6 +762,8 @@ token_t lex_token_internal(bool aliasing)
char *alias;
int i = 0;
do {
if (i >= MAX_TOKEN_LEN - 1)
error("Token too long");
token_str[i++] = next_char;
} while (is_alnum(read_char(false)));
token_str[i] = 0;
Expand Down
15 changes: 15 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,21 @@ int main() {
}
EOF

# String literal and escape coverage (additional)
try_output 0 "AZ" << 'EOF'
int main() {
printf("%s", "\\x41Z"); /* hex escape then normal char */
return 0;
}
EOF

try_output 0 "AZ" << 'EOF'
int main() {
printf("%s", "A\\132"); /* octal escape for 'Z' */
return 0;
}
EOF

# Cast zero value
try_ 0 << EOF
int main() {
Expand Down