aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorc2024-01-28 21:43:37 -0500
committerc2024-01-28 21:43:37 -0500
commitca20ff4f4d0ac63856e538f9f4cef97197bf6465 (patch)
treeba8886c8832e082332a284e3b18fb0e4bbce3d05
parent422606d8b3cc6fba74f97af46f0378fc274d60ad (diff)
Fixed buffer-overflow.
-rw-r--r--Makefile6
-rw-r--r--src/include/lexer.h2
-rw-r--r--src/include/util.h7
-rw-r--r--src/lexer.c4
-rw-r--r--src/parser.c2
-rw-r--r--src/source.c15
-rw-r--r--src/token.c2
-rw-r--r--src/tree.c11
8 files changed, 33 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 0e61c64..3bd2c39 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ PREFIX := /usr/local/bin
CC := gcc
REG_CFLAGS := -std=c99 -O3 -s
DBG_CFLAGS := -std=c99 -Og -ggdb -pedantic
-DBG_CFLAGS += -Wall -Wextra -Wformat -Werror -Wpedantic
+DBG_CFLAGS += -Wall -Wextra -Wformat -Wpedantic
DBG_CFLAGS += -fsanitize=leak,address,undefined -fno-omit-frame-pointer
CFLAGS := $(REG_CFLAGS)
SRCS := $(wildcard src/*.c)
@@ -40,10 +40,10 @@ test: clean dbg $(TEST_OUTS)
for f in $(TEST_OUTS); do ./$$f; done
%.o: %.c
- $(CC) -c $< -o $@
+ $(CC) -c $< -o $@ $(CFLAGS)
%.out: %.c
- $(CC) $< $(filter-out %main.o,$(OBJS)) -o $@
+ $(CC) $< $(filter-out %main.o,$(OBJS)) -o $@ $(CFLAGS)
install: all
mkdir -p $(PREFIX)
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 <string.h>
#include <stdio.h>
+#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 <unistd.h>
+
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"