Updated more things to use xfree(&foo) instead of free(foo)

This commit is contained in:
Bruce Hill 2021-01-10 00:24:24 -08:00
parent 9d1f51c483
commit f4a7b80b4f
7 changed files with 22 additions and 20 deletions

View File

@ -351,7 +351,7 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
// Parentheses
case '(': case '{': {
char closing = c == '(' ? ')' : '}';
free(op);
xfree(&op);
op = bp_simplepattern(f, str);
if (!op)
file_err(f, str, str, "There should be a valid pattern after this parenthesis.");
@ -428,8 +428,8 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
op->args.s = strndup(&c, 1);
}
if (matchchar(&str, ':')) { // Don't match definitions
free((char*)op->args.s);
free(op);
xfree(&op->args.s);
xfree(&op);
return NULL;
}
op->op = VM_REF;
@ -442,14 +442,14 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
const char *refname = str;
str = after_name(str);
if (matchchar(&str, ':')) { // Don't match definitions
free(op);
xfree(&op);
return NULL;
}
op->op = VM_REF;
op->args.s = strndup(refname, (size_t)(str - refname));
break;
} else {
free(op);
xfree(&op);
return NULL;
}
}
@ -535,8 +535,7 @@ vm_op_t *bp_stringpattern(file_t *f, const char *str)
strop->end = str;
if (strop->len == 0) {
free(strop);
strop = NULL;
xfree(&strop);
} else {
ret = chain_together(ret, strop);
}

View File

@ -103,30 +103,29 @@ void intern_file(file_t *f)
f->contents = buf;
f->end = buf + size;
f->mmapped = 0;
free(f->lines);
xfree(&f->lines);
populate_lines(f);
}
void destroy_file(file_t **f)
{
if ((*f)->filename) {
free((char*)(*f)->filename);
xfree(&((*f)->filename));
(*f)->filename = NULL;
}
if ((*f)->lines) {
free((*f)->lines);
xfree(&((*f)->lines));
(*f)->lines = NULL;
}
if ((*f)->contents) {
if ((*f)->mmapped) {
munmap((*f)->contents, (size_t)((*f)->end - (*f)->contents));
} else {
free((*f)->contents);
xfree(&((*f)->contents));
}
(*f)->contents = NULL;
}
free(*f);
*f = NULL;
xfree(f);
}
size_t get_line_number(file_t *f, const char *p)

View File

@ -119,8 +119,8 @@ void pop_backrefs(grammar_t *g, size_t count)
backref_t *b = g->firstbackref;
g->firstbackref = b->next;
check(b, "Attempt to pop %ld more backrefs than there are", count);
xfree((void**)&b->op);
xfree((void**)&b);
xfree(&b->op);
xfree(&b);
}
}

View File

@ -132,7 +132,7 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char *
for (match_node_t *c = children, *next = NULL; c; c = next) {
next = c->next;
free(c);
xfree(&c);
}
}

View File

@ -214,9 +214,13 @@ int memicmp(const void *v1, const void *v2, size_t n)
/*
* Free memory, but also set the pointer to NULL for safety
*/
void xfree(void **p)
void xfree(void *p)
{
free(*p);
if (*(void**)p == NULL) {
fprintf(stderr, "Attempt to free(NULL)\n");
_exit(1);
}
free(*(void**)p);
p = NULL;
}

View File

@ -34,7 +34,7 @@ void *memcheck(void *p);
__attribute__((nonnull))
int memicmp(const void *s1, const void *s2, size_t n);
__attribute__((nonnull))
void xfree(void **p);
void xfree(void *p);
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

2
vm.c
View File

@ -538,7 +538,7 @@ match_t *get_capture(match_t *m, const char **r)
if (end == *r) return NULL;
char *name = strndup(*r, (size_t)(end-*r));
match_t *cap = get_capture_by_name(m, name);
free(name);
xfree(&name);
*r = end;
if (**r == ';') ++(*r);
return cap;