diff options
author | David Phillips <david@sighup.nz> | 2019-08-11 17:22:20 +1200 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2019-08-11 17:22:20 +1200 |
commit | 2d2628df0001d2b643811dc69717c4afe70c4e31 (patch) | |
tree | f5d0da23c47c3e677ca423a91838c5ad5467a5d9 /lex.c | |
parent | 0e6f47211a0516cf2c96d2b9e89c0d841978c143 (diff) | |
download | toy-cpu-assembler-2d2628df0001d2b643811dc69717c4afe70c4e31.tar.xz |
Fix and add tests for NULL deref cases found by scan-build
Diffstat (limited to 'lex.c')
-rw-r--r-- | lex.c | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -296,6 +296,13 @@ static int lex_eol(struct token *t) { return 0; } +static int lex_eof(struct token *t) { + t->type = TOKEN_EOF; + t->span = 1; + store_location(t); + return 0; +} + int lex_line(void) { int ret = 0; size_t len = strlen(buffer); @@ -424,12 +431,11 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len) return NULL; } - /* no tokens? just an EOL then */ - if (tokens_count == 0) { - struct token eol = {0}; - lex_eol(&eol); - add_token(eol); - } + /* tack on EOF */ + line--; column--; + struct token eof = {0}; + lex_eof(&eof); + add_token(eof); *len = tokens_count; return tokens; |