Skip to content

Commit 3e4189a

Browse files
committed
Add bounds checks when assembling tokens
This commit prevents token_str overflow by checking bounds when building preprocessor directives, identifiers, and numeric literals.
1 parent 00f8209 commit 3e4189a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/lexer.c

Lines changed: 18 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
}
@@ -744,6 +760,8 @@ token_t lex_token_internal(bool aliasing)
744760
char *alias;
745761
int i = 0;
746762
do {
763+
if (i >= MAX_TOKEN_LEN - 1)
764+
error("Token too long");
747765
token_str[i++] = next_char;
748766
} while (is_alnum(read_char(false)));
749767
token_str[i] = 0;

0 commit comments

Comments
 (0)