forked from ground/ground
Fix use segfault
This commit is contained in:
@@ -15,6 +15,8 @@ int currentInstruction = 0;
|
||||
|
||||
bool isMainScopeGlobal = true;
|
||||
|
||||
char* getFileContents(const char* filename);
|
||||
|
||||
[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
printGroundArg(&type);
|
||||
@@ -1886,17 +1888,17 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
char* envPath = getenv("GROUND_LIBS");
|
||||
|
||||
if (envPath) {
|
||||
snprintf(path, sizeof(path), "%s/%s", envPath, libName);
|
||||
snprintf(path, sizeof(path), "%s/%s.grnd", envPath, libName);
|
||||
if (access(path, F_OK) == 0) found = 1;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
snprintf(path, sizeof(path), "/usr/lib/ground/%s", libName);
|
||||
snprintf(path, sizeof(path), "/usr/lib/ground/%s.grnd", libName);
|
||||
if (access(path, F_OK) == 0) found = 1;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
snprintf(path, sizeof(path), "%s", libName);
|
||||
snprintf(path, sizeof(path), "%s.grnd", libName);
|
||||
if (access(path, F_OK) == 0) found = 1;
|
||||
}
|
||||
|
||||
@@ -1905,29 +1907,22 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
snprintf(errorMsg, sizeof(errorMsg), "Could not find library: %s", libName);
|
||||
runtimeError(FIXME, errorMsg, in, currentInstruction);
|
||||
}
|
||||
|
||||
char* fileContents = getFileContents(path);
|
||||
|
||||
GroundProgram program = parseFile(fileContents);
|
||||
free(fileContents);
|
||||
|
||||
GroundScope newScope = {
|
||||
.variables = scope->variables,
|
||||
.labels = malloc(sizeof(GroundLabel*)),
|
||||
.isMainScope = false
|
||||
};
|
||||
|
||||
*newScope.labels = NULL;
|
||||
|
||||
FILE* f = fopen(path, "r");
|
||||
if (!f) {
|
||||
runtimeError(FIXME, "Failed to open file", in, currentInstruction);
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
char* content = malloc(fsize + 1);
|
||||
if (!content) {
|
||||
fclose(f);
|
||||
runtimeError(FIXME, "Failed to allocate memory for file content", in, currentInstruction);
|
||||
}
|
||||
fread(content, 1, fsize, f);
|
||||
fclose(f);
|
||||
content[fsize] = 0;
|
||||
|
||||
GroundProgram program = parseFile(content);
|
||||
free(content);
|
||||
|
||||
interpretGroundProgram(&program, scope);
|
||||
freeGroundProgram(&program);
|
||||
interpretGroundProgram(&program, &newScope);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user