trace bug in delete_lispval!

This commit is contained in:
NunoSempere 2023-05-02 20:19:31 -04:00
parent f7f350f493
commit 35650bc85d
3 changed files with 22 additions and 13 deletions

View File

@ -6,8 +6,8 @@
# make uninstall # make uninstall
## C compiler ## C compiler
CC=gcc # much faster compilation than gcc CC=tcc # much faster compilation than gcc
COMPILER_FLAGS=-g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined COMPILER_FLAGS=#-g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined
# exclude: -fsanitize-trap, because I'm using an old version of gcc and couldn't bother getting a new one. # exclude: -fsanitize-trap, because I'm using an old version of gcc and couldn't bother getting a new one.
## ^ from <https://nullprogram.com/blog/2023/04/29/> ## ^ from <https://nullprogram.com/blog/2023/04/29/>
## <https://news.ycombinator.com/item?id=35758898> ## <https://news.ycombinator.com/item?id=35758898>

BIN
mumble

Binary file not shown.

View File

@ -9,7 +9,7 @@
if (!(cond)) { \ if (!(cond)) { \
return lispval_err(err); \ return lispval_err(err); \
} }
int VERBOSE = 0; int VERBOSE = 2;
#define printfln(...) do { \ #define printfln(...) do { \
if(VERBOSE == 2) { \ if(VERBOSE == 2) { \
printf ("\n@ %s (%d): ", __FILE__, __LINE__); \ printf ("\n@ %s (%d): ", __FILE__, __LINE__); \
@ -136,9 +136,11 @@ void delete_lispval(lispval* v)
{ {
if(v == NULL) return; if(v == NULL) return;
// print_lispval_tree(v, 0); // print_lispval_tree(v, 0);
if(VERBOSE) printfln("\nDeleting object of type %i",v->type);
switch (v->type) { switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
if(VERBOSE) printfln("Freeing num"); if(VERBOSE) printfln("Freeing num");
if (v != NULL) free(v);
if(VERBOSE) printfln("Freed num"); if(VERBOSE) printfln("Freed num");
break; break;
case LISPVAL_ERR: case LISPVAL_ERR:
@ -146,6 +148,7 @@ void delete_lispval(lispval* v)
if (v->err != NULL) if (v->err != NULL)
free(v->err); free(v->err);
v->err = NULL; v->err = NULL;
if (v != NULL) free(v);
if(VERBOSE) printfln("Freed err"); if(VERBOSE) printfln("Freed err");
break; break;
case LISPVAL_SYM: case LISPVAL_SYM:
@ -153,6 +156,7 @@ void delete_lispval(lispval* v)
if (v->sym != NULL) if (v->sym != NULL)
free(v->sym); free(v->sym);
v->sym = NULL; v->sym = NULL;
if (v != NULL) free(v);
if(VERBOSE) printfln("Freed sym"); if(VERBOSE) printfln("Freed sym");
break; break;
case LISPVAL_FUNC: case LISPVAL_FUNC:
@ -160,6 +164,7 @@ void delete_lispval(lispval* v)
if (v->funcname != NULL) if (v->funcname != NULL)
free(v->funcname); free(v->funcname);
v->funcname = NULL; v->funcname = NULL;
if (v != NULL) free(v);
if(VERBOSE) printfln("Freed func"); if(VERBOSE) printfln("Freed func");
// Don't do anything with v->func for now // Don't do anything with v->func for now
// Though we could delete the pointer to the function later // Though we could delete the pointer to the function later
@ -168,22 +173,23 @@ void delete_lispval(lispval* v)
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
case LISPVAL_QEXPR: case LISPVAL_QEXPR:
if(VERBOSE) printfln("Freeing sexpr|qexpr"); if(VERBOSE) printfln("Freeing sexpr|qexpr");
if(v==NULL || v->count !=0) return;
for (int i = 0; i < v->count; i++) { for (int i = 0; i < v->count; i++) {
if (v->cell[i] != NULL) if (v->cell[i] != NULL)
delete_lispval(v->cell[i]); delete_lispval(v->cell[i]);
v->cell[i] = NULL; v->cell[i] = NULL;
} }
v->count = 0;
if (v->cell != NULL) if (v->cell != NULL)
free(v->cell); free(v->cell);
v->cell = NULL; v->cell = NULL;
if (v != NULL) free(v);
if(VERBOSE) printfln("Freed sexpr|qexpr"); if(VERBOSE) printfln("Freed sexpr|qexpr");
break; break;
default: default:
if(VERBOSE) printfln("Error: Unknown expression type."); if(VERBOSE) printfln("Error: Unknown expression type for pointer %p of type %i", v, v->type);
} }
if (v != NULL) // v = NULL; this is only our local pointer, sadly.
free(v);
v = NULL;
} }
// Environment // Environment
@ -718,7 +724,7 @@ void lispenv_add_builtin(char* funcname, lispbuiltin func, lispenv* env ){
lispval* f = lispval_func(func, funcname); lispval* f = lispval_func(func, funcname);
if(VERBOSE) print_lispval_tree(f, 0); if(VERBOSE) print_lispval_tree(f, 0);
insert_in_lispenv(funcname, f,env); insert_in_lispenv(funcname, f,env);
// delete_lispval(f); delete_lispval(f);
} }
void lispenv_add_builtins(lispenv* env){ void lispenv_add_builtins(lispenv* env){
// Math functions // Math functions
@ -907,13 +913,18 @@ int main(int argc, char** argv)
if(VERBOSE) print_lispval_tree(answer, 0); if(VERBOSE) print_lispval_tree(answer, 0);
printf("\n"); printf("\n");
} }
// ^ I do not understand how the memory in l is freed.
delete_lispval(answer); delete_lispval(answer);
// delete_lispval(l); if(VERBOSE > 1) printfln("Answer after deletion: %p", answer);
// delete_lispval(answer); // do this twice, just to see.
//if(VERBOSE) printfln("Deleting this lispval:");
// if(VERBOSE) print_lispval_tree(l,2);
delete_lispval(l);
// if(VERBOSE) printfln("Deleted that ^ lispval");
// ^ I do not understand how the memory in l is freed.
} else { } else {
/* Otherwise Print the Error */ /* Otherwise Print the Error */
mpc_err_print(result.error); mpc_err_print(result.error);
mpc_err_delete(result.error); // mpc_err_delete(result.error);
} }
add_history(input); add_history(input);
// can't add if input is NULL // can't add if input is NULL
@ -925,8 +936,6 @@ int main(int argc, char** argv)
// Clean up environment // Clean up environment
destroy_lispenv(env); destroy_lispenv(env);
// Clean up environment
destroy_lispenv(env);
/* Undefine and Delete our Parsers */ /* Undefine and Delete our Parsers */
mpc_cleanup(6, Number, Symbol, Sexpr, Qexpr, Expr, Mumble); mpc_cleanup(6, Number, Symbol, Sexpr, Qexpr, Expr, Mumble);