Skip to content

Commit fb956c1

Browse files
authored
Merge pull request #243 from sysprog21/fixes
Correctness fixes
2 parents d2e1d20 + ac5420f commit fb956c1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/globals.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,8 @@ hashmap_node_t *hashmap_node_new(char *key, void *val)
420420
}
421421

422422
node->key = arena_alloc(HASHMAP_ARENA, len + 1);
423-
424423
if (!node->key) {
425424
printf("Failed to allocate hashmap_node_t key with size %d\n", len + 1);
426-
free(node);
427425
return NULL;
428426
}
429427

src/lexer.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ token_t lex_token_internal(bool aliasing)
277277
int i = 0;
278278

279279
do {
280+
if (i >= MAX_TOKEN_LEN - 1)
281+
error("Token too long");
280282
token_str[i++] = next_char;
281283
} while (is_alnum(read_char(false)));
282284
token_str[i] = 0;
@@ -328,30 +330,40 @@ token_t lex_token_internal(bool aliasing)
328330

329331
if (is_digit(next_char)) {
330332
int i = 0;
333+
if (i >= MAX_TOKEN_LEN - 1)
334+
error("Token too long");
331335
token_str[i++] = next_char;
332336
read_char(false);
333337

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

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

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

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

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

354364
do {
365+
if (i >= MAX_TOKEN_LEN - 1)
366+
error("Token too long");
355367
token_str[i++] = next_char;
356368
read_char(false);
357369
} while (next_char == '0' || next_char == '1');
@@ -361,13 +373,17 @@ token_t lex_token_internal(bool aliasing)
361373
while (is_digit(next_char)) {
362374
if (next_char >= '8')
363375
error("Invalid octal digit: must be in range 0-7");
376+
if (i >= MAX_TOKEN_LEN - 1)
377+
error("Token too long");
364378
token_str[i++] = next_char;
365379
read_char(false);
366380
}
367381

368382
} else {
369383
/* Decimal */
370384
while (is_digit(next_char)) {
385+
if (i >= MAX_TOKEN_LEN - 1)
386+
error("Token too long");
371387
token_str[i++] = next_char;
372388
read_char(false);
373389
}
@@ -492,6 +508,8 @@ token_t lex_token_internal(bool aliasing)
492508
token_str[i - 1] = next_char;
493509
}
494510
} else {
511+
if (i >= MAX_TOKEN_LEN - 1)
512+
error("String literal too long");
495513
token_str[i++] = next_char;
496514
}
497515
if (next_char == '\\')
@@ -744,6 +762,8 @@ token_t lex_token_internal(bool aliasing)
744762
char *alias;
745763
int i = 0;
746764
do {
765+
if (i >= MAX_TOKEN_LEN - 1)
766+
error("Token too long");
747767
token_str[i++] = next_char;
748768
} while (is_alnum(read_char(false)));
749769
token_str[i] = 0;

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)