summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-10 13:03:25 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-10 13:03:25 +0330
commit6d127edfa0162219c54b10e3496a3e9530f62792 (patch)
treec70579a2f218f8d233f0f2ef2a1867908a0594f7
parent302cc65d3e59937742c18475d63e22c482176fa7 (diff)
fix double free of code
some clean up
-rw-r--r--src/compiler/lexer.c5
-rw-r--r--src/compiler/lexer.h4
-rw-r--r--src/main.c49
-rw-r--r--src/utils/file.c28
-rw-r--r--src/utils/file.h2
-rw-r--r--src/utils/string.c1
6 files changed, 46 insertions, 43 deletions
diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c
index 70cd1fb..e69afef 100644
--- a/src/compiler/lexer.c
+++ b/src/compiler/lexer.c
@@ -4,6 +4,7 @@
#include "utils/memory.h"
#include "utils/string.h"
+#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -75,6 +76,10 @@ const LexerToken LEXER_KEYWORD_TOKENS[] = {
const size_t LEXER_KEYWORD_SIZE =
sizeof(LEXER_KEYWORD_TOKENS) / sizeof(*LEXER_KEYWORD_TOKENS);
+const LexerNodeArray LEXER_NODE_ARRAY_ERROR = {
+ .size = SIZE_MAX,
+};
+
bool lexerNodeArrayIsError(LexerNodeArray array) {
return LEXER_NODE_ARRAY_ERROR.size == array.size;
}
diff --git a/src/compiler/lexer.h b/src/compiler/lexer.h
index 6723cc9..656084e 100644
--- a/src/compiler/lexer.h
+++ b/src/compiler/lexer.h
@@ -59,9 +59,7 @@ typedef struct LexerNodeArray {
size_t size;
} LexerNodeArray;
-constexpr LexerNodeArray LEXER_NODE_ARRAY_ERROR = {
- .size = -1ULL,
-};
+extern const LexerNodeArray LEXER_NODE_ARRAY_ERROR;
extern bool lexerNodeArrayIsError(LexerNodeArray array);
extern void lexerNodeArrayPrint(LexerNodeArray array);
diff --git a/src/main.c b/src/main.c
index 6a6fbab..8a61324 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,4 @@
-#include "compiler/ast-tree.h"
#include "compiler/code-generator.h"
-#include "compiler/lexer.h"
-#include "compiler/parser.h"
#include "runner/runner.h"
#include "utils/file.h"
#include "utils/log.h"
@@ -18,7 +15,7 @@ static int compileRun(const char *filePath, const char *outFilePath,
LexerNodeArray lexed = lexer(code);
if (lexerNodeArrayIsError(lexed)) {
- goto RETURN_ERROR;
+ return 1;
}
if (print)
lexerNodeArrayPrint(lexed);
@@ -26,7 +23,7 @@ static int compileRun(const char *filePath, const char *outFilePath,
ParserNode *parsedRoot = parser(lexed);
lexerNodeArrayDestroy(lexed);
if (parsedRoot == NULL) {
- goto RETURN_ERROR;
+ return 1;
}
if (print)
parserNodePrint(parsedRoot, 0);
@@ -34,7 +31,7 @@ static int compileRun(const char *filePath, const char *outFilePath,
AstTreeRoot *astTree = makeAstTree(parsedRoot);
parserNodeDelete(parsedRoot);
if (astTree == NULL) {
- goto RETURN_ERROR;
+ return 1;
}
if (print)
astTreeRootPrint(astTree);
@@ -42,7 +39,7 @@ static int compileRun(const char *filePath, const char *outFilePath,
CodeGeneratorCodes *codes = codeGenerator(astTree);
astTreeRootDelete(astTree);
if (codes == NULL) {
- goto RETURN_ERROR;
+ return 1;
}
char *fasm = codeGeneratorToFlatASM(codes);
@@ -58,40 +55,30 @@ static int compileRun(const char *filePath, const char *outFilePath,
}
return 1;
-
-RETURN_ERROR:
- free(code);
- return 1;
}
-static int run(const char *filePath, bool print) {
- char *code = readWholeFile(filePath);
-
- if (code == NULL) {
- return 1;
- }
-
+int runWithoutRead(char *code, bool shouldPrint) {
LexerNodeArray lexed = lexer(code);
if (lexerNodeArrayIsError(lexed)) {
- goto RETURN_ERROR;
+ return 1;
}
- if (print)
+ if (shouldPrint)
lexerNodeArrayPrint(lexed);
ParserNode *parsedRoot = parser(lexed);
lexerNodeArrayDestroy(lexed);
if (parsedRoot == NULL) {
- goto RETURN_ERROR;
+ return 1;
}
- if (print)
+ if (shouldPrint)
parserNodePrint(parsedRoot, 0);
AstTreeRoot *astTree = makeAstTree(parsedRoot);
parserNodeDelete(parsedRoot);
if (astTree == NULL) {
- goto RETURN_ERROR;
+ return 1;
}
- if (print)
+ if (shouldPrint)
astTreeRootPrint(astTree);
int ret;
@@ -103,10 +90,16 @@ static int run(const char *filePath, bool print) {
astTreeRootDelete(astTree);
return ret;
+}
-RETURN_ERROR:
- free(code);
- return 1;
+static int run(const char *filePath, bool shouldPrint) {
+ char *code = readWholeFile(filePath);
+
+ if (code == NULL) {
+ return 1;
+ }
+
+ return runWithoutRead(code, shouldPrint);
}
int main(int argc, char *argv[]) {
@@ -118,7 +111,7 @@ int main(int argc, char *argv[]) {
return 1;
}
- const int ret = run(argv[1], true);
+ const int ret = run(argv[1], false);
fileDelete();
return ret;
}
diff --git a/src/utils/file.c b/src/utils/file.c
index 89ccfa8..921befb 100644
--- a/src/utils/file.c
+++ b/src/utils/file.c
@@ -30,6 +30,21 @@ void fileDelete() {
fileCodes_length = 0;
}
+void filePush(const char *filePath, char *code) {
+ if (fileCodes_capacity == fileCodes_length) {
+ fileCodes_capacity += fileCodes_capacity / 2 + 1;
+ fileCodes =
+ a404m_realloc(fileCodes, fileCodes_capacity * sizeof(*fileCodes));
+ fileCodes_names = a404m_realloc(
+ fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names));
+ }
+ fileCodes[fileCodes_length] = code;
+ fileCodes_names[fileCodes_length] =
+ a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names));
+ strcpy(fileCodes_names[fileCodes_length], filePath);
+ fileCodes_length += 1;
+}
+
char *readWholeFile(const char *filePath) {
FILE *file = fopen(filePath, "r");
@@ -48,18 +63,7 @@ char *readWholeFile(const char *filePath) {
fclose(file);
- if (fileCodes_capacity == fileCodes_length) {
- fileCodes_capacity += fileCodes_capacity / 2 + 1;
- fileCodes =
- a404m_realloc(fileCodes, fileCodes_capacity * sizeof(*fileCodes));
- fileCodes_names = a404m_realloc(
- fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names));
- }
- fileCodes[fileCodes_length] = str;
- fileCodes_names[fileCodes_length] =
- a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names));
- strcpy(fileCodes_names[fileCodes_length], filePath);
- fileCodes_length += 1;
+ filePush(filePath, str);
return str;
}
diff --git a/src/utils/file.h b/src/utils/file.h
index 4a904e4..31e6503 100644
--- a/src/utils/file.h
+++ b/src/utils/file.h
@@ -10,4 +10,6 @@ extern size_t fileCodes_length;
void fileInit();
void fileDelete();
+void filePush(const char *filePath,char *code);
+
char *readWholeFile(const char *filePath);
diff --git a/src/utils/string.c b/src/utils/string.c
index 5f9b7de..a5baab4 100644
--- a/src/utils/string.c
+++ b/src/utils/string.c
@@ -1,5 +1,6 @@
#include "string.h"
#include "memory.h"
+#include "utils/log.h"
#include <stdint.h>
#include <string.h>