From ca20ff4f4d0ac63856e538f9f4cef97197bf6465 Mon Sep 17 00:00:00 2001 From: c Date: Sun, 28 Jan 2024 21:43:37 -0500 Subject: Fixed buffer-overflow. --- src/include/lexer.h | 2 +- src/include/util.h | 7 +++++++ src/lexer.c | 4 +--- src/parser.c | 2 +- src/source.c | 15 ++++++++------- src/token.c | 2 ++ src/tree.c | 11 ++++++++++- 7 files changed, 30 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/include/lexer.h b/src/include/lexer.h index 5235f3b..79a3cb6 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -53,7 +53,7 @@ void lexer_add_current_char(lexer_t* lexer, int type); Add first character of given lexer's `src` to the value of the last token in `tokenl`, if it exists. Otherwise, create new token and add it. */ -void lexer_add_current_char_to_last_token(lexer_t* lexer, int type); +void lexer_add_current_char_to_last_token(lexer_t* lexer, token_type_t type); /* Handle regular state. */ void lexer_do_reg(lexer_t* lexer); diff --git a/src/include/util.h b/src/include/util.h index 839d3e4..9f8a2b0 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -6,6 +6,13 @@ #include #include +#define MIN(a, b) (a < b ? a : b) +#define MAX(a, b) (a > b ? a : b) + +/* + TODO: Make these macros, to allow for better logging (printing __FILE__, + __func__, etc.) +*/ /* Log some debug information. */ void log_dbg(const char*, ...); /* c: */ diff --git a/src/lexer.c b/src/lexer.c index a89c9ad..a93828a 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -26,8 +26,6 @@ void lexer_destroy(lexer_t* lexer) { } void lexer_add_token(lexer_t* lexer, token_t* token) { - token_t* t; - if (lexer->tokenl) { lexer->tokenl_last->nxt = token; lexer->tokenl_last = token; @@ -47,7 +45,7 @@ void lexer_add_current_char(lexer_t* lexer, int type) { lexer_add_token(lexer, t); } -void lexer_add_current_char_to_last_token(lexer_t* lexer, int type) { +void lexer_add_current_char_to_last_token(lexer_t* lexer, token_type_t type) { if (lexer->tokenl_last && lexer->tokenl_last->type == type) { token_add_char(lexer->tokenl_last, *lexer->src); } else { diff --git a/src/parser.c b/src/parser.c index f94fe53..9da3da0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -43,7 +43,7 @@ tree_t* parser_parse_init(parser_t* parser) { tree_t* parser_parse_block(parser_t* parser) { /* There is nothing to do. */ if (!parser->token || parser->token->type == TOKEN_TYPE_RBLOCK) { - parser_nxt_token(parser); /* Skip over closing bracket. */ + (void) parser_nxt_token(parser); /* Skip over closing bracket. */ return NULL; } tree_t* block; diff --git a/src/source.c b/src/source.c index f7f7063..813e9f5 100644 --- a/src/source.c +++ b/src/source.c @@ -1,5 +1,5 @@ #include "include/source.h" -#include + char* source_get(char* arg) { return arg? source_get_from_fpath(arg): @@ -30,19 +30,20 @@ char* source_get_from_fpath(char* path) { } char* source_get_from_stdin() { + char* s; char* src; size_t l; - src = ecalloc(256, sizeof(char)); l = 0; - while (fgets(src + l, 20, stdin) != NULL) { - l += strlen(src + l); - } + src = ecalloc(16, sizeof(char)); - (src[l - 1] == '\n') && (src[l - 1] = '\0'); + while ((s = fgets(src + l, 20, stdin))) { + l = MIN(16, l + strlen(src + l)); + } -// src = fgets(src, 256, stdin); + /* This works, I guess. */ + s && src[l - 1] == '\n' && (src[l - 1] = '\0'); return src; } diff --git a/src/token.c b/src/token.c index bcb4c18..3479a86 100644 --- a/src/token.c +++ b/src/token.c @@ -28,6 +28,8 @@ void token_destroy(token_t* token) { token_t* token_last(token_t* token) { token_t* t; + t = token; + while (t->nxt) { t = t->nxt; } diff --git a/src/tree.c b/src/tree.c index 62c8218..8398598 100644 --- a/src/tree.c +++ b/src/tree.c @@ -130,13 +130,20 @@ int tree_cmp(tree_t* tree_0, tree_t* tree_1) { return (strcmp(tree_0->data.call.target, tree_1->data.call.target) == 0) && tree_cmp(tree_0->data.call.arg, tree_1->data.call.arg); break; + default: + log_war("Unknown tree type."); } + + return 0; } /* Every time I think there's a problem with the parser, it turns out it's - just this stupid tree print function. + just this stupid tree print function. Now it works. Never touching it + again. Ever. */ +#pragma GCC diagnostic ignored "-Wunused-value" +#pragma GCC diagnostic ignored "-Wmisleading-indentation" void tree_print(tree_t* tree, int nest) { char*sp;int nc, i;char*c;char*bc;for(i =0,sp=ecalloc(nest+1,sizeof(char)),sp[ @@ -226,3 +233,5 @@ void tree_print(tree_t* tree, int nest) { free(bc); return; } +#pragma GCC diagnostic warning "-Wunused-value" +#pragma GCC diagnostic warning "-Wmisleading-indentation" -- cgit v1.2.3