# HG changeset patch # User David A. Holland # Date 1364692667 14400 # Node ID 337110e7240ab1d7d52ba5185fdb8ecb55c78154 # Parent b156910b59b2f55424293c4e44eb5f0828ea6457 Pass the size to free; it makes debug checking easier. diff -r b156910b59b2 -r 337110e7240a array.c --- a/array.c Sat Mar 30 21:02:25 2013 -0400 +++ b/array.c Sat Mar 30 21:17:47 2013 -0400 @@ -47,7 +47,7 @@ array_destroy(struct array *a) { array_cleanup(a); - dofree(a); + dofree(a, sizeof(*a)); } void @@ -61,7 +61,7 @@ array_cleanup(struct array *a) { arrayassert(a->num == 0); - dofree(a->v); + dofree(a->v, a->max * sizeof(a->v[0])); #ifdef ARRAYS_CHECKED a->v = NULL; #endif @@ -78,7 +78,8 @@ while (num > newmax) { newmax = newmax ? newmax*2 : 4; } - newptr = dorealloc(a->v, newmax*sizeof(*a->v)); + newptr = dorealloc(a->v, a->max * sizeof(a->v[0]), + newmax * sizeof(a->v[0])); a->v = newptr; a->max = newmax; } diff -r b156910b59b2 -r 337110e7240a array.h --- a/array.h Sat Mar 30 21:02:25 2013 -0400 +++ b/array.h Sat Mar 30 21:17:47 2013 -0400 @@ -179,7 +179,7 @@ ARRAY##_destroy(struct ARRAY *a) \ { \ ARRAY##_cleanup(a); \ - dofree(a); \ + dofree(a, sizeof(*a)); \ } \ \ INLINE unsigned \ diff -r b156910b59b2 -r 337110e7240a directive.c --- a/directive.c Sat Mar 30 21:02:25 2013 -0400 +++ b/directive.c Sat Mar 30 21:17:47 2013 -0400 @@ -94,7 +94,7 @@ void ifstate_destroy(struct ifstate *is) { - dofree(is); + dofree(is, sizeof(*is)); } static @@ -126,7 +126,7 @@ expr = macroexpand(p2, line, len, true); val = eval(&p3, expr); ifstate_push(p, val); - dofree(expr); + dostrfree(expr); } static @@ -163,7 +163,7 @@ expr = macroexpand(p2, line, len, true); ifstate->curtrue = eval(&p3, expr); ifstate->evertrue = ifstate->curtrue; - dofree(expr); + dostrfree(expr); } } @@ -298,10 +298,10 @@ } text = macroexpand(p2, line, len, false); if (tryinclude(p, text, strlen(text))) { - dofree(text); + dostrfree(text); return; } - dofree(text); + dostrfree(text); complain(p, "Illegal #include directive"); complain_fail(); } @@ -328,7 +328,7 @@ if (mode.werror) { complain_fail(); } - dofree(msg); + dostrfree(msg); } static @@ -340,7 +340,7 @@ msg = macroexpand(p2, line, len, false); complain(p, "#error: %s", msg); complain_fail(); - dofree(msg); + dostrfree(msg); } //////////////////////////////////////////////////////////// diff -r b156910b59b2 -r 337110e7240a eval.c --- a/eval.c Sat Mar 30 21:02:25 2013 -0400 +++ b/eval.c Sat Mar 30 21:17:47 2013 -0400 @@ -149,7 +149,7 @@ void token_destroy(struct token *t) { - dofree(t); + dofree(t, sizeof(*t)); } DESTROYALL_ARRAY(token, ); diff -r b156910b59b2 -r 337110e7240a files.c --- a/files.c Sat Mar 30 21:02:25 2013 -0400 +++ b/files.c Sat Mar 30 21:17:47 2013 -0400 @@ -70,7 +70,7 @@ void incdir_destroy(struct incdir *id) { - dofree(id); + dofree(id, sizeof(*id)); } void @@ -160,8 +160,8 @@ } if (bufend >= bufmax) { /* need bigger buffer */ - bufmax *= 2; - buf = dorealloc(buf, bufmax); + buf = dorealloc(buf, bufmax, bufmax*2); + bufmax = bufmax*2; } if (ateof) { @@ -242,7 +242,7 @@ if (toplevel) { directive_goteof(&linestartplace); } - dofree(buf); + dofree(buf, bufmax); } //////////////////////////////////////////////////////////// @@ -308,11 +308,11 @@ if (fd >= 0) { pf = place_addfile(place, file, id->issystem); file_read(pf, fd, file, false); - dofree(file); + dostrfree(file); close(fd); return; } - dofree(file); + dostrfree(file); } complain(place, "Include file %s not found", name); complain_fail(); diff -r b156910b59b2 -r 337110e7240a macro.c --- a/macro.c Sat Mar 30 21:02:25 2013 -0400 +++ b/macro.c Sat Mar 30 21:17:47 2013 -0400 @@ -110,9 +110,9 @@ expansionitem_destroy(struct expansionitem *ei) { if (ei->isstring) { - dofree(ei->string); + dostrfree(ei->string); } - dofree(ei); + dofree(ei, sizeof(*ei)); } static @@ -162,8 +162,8 @@ { expansionitemarray_destroyall(&m->expansion); expansionitemarray_cleanup(&m->expansion); - dofree(m->name); - dofree(m); + dostrfree(m->name); + dofree(m, sizeof(*m)); } static @@ -634,7 +634,7 @@ assert(es->state == ES_NORMAL); stringarray_cleanup(&es->args); if (es->buf) { - dofree(es->buf); + dofree(es->buf, es->bufmax); } } @@ -646,7 +646,7 @@ num = stringarray_num(&es->args); for (i=0; iargs, i)); + dostrfree(stringarray_get(&es->args, i)); } stringarray_setsize(&es->args, 0); } @@ -655,16 +655,19 @@ void expand_send(struct expstate *es, struct place *p, const char *buf, size_t len) { + size_t oldmax; + if (es->tobuf) { assert(es->bufpos <= es->bufmax); if (es->bufpos + len > es->bufmax) { + oldmax = es->bufmax; if (es->bufmax == 0) { es->bufmax = 64; } while (es->bufpos + len > es->bufmax) { es->bufmax *= 2; } - es->buf = dorealloc(es->buf, es->bufmax); + es->buf = dorealloc(es->buf, oldmax, es->bufmax); } memcpy(es->buf + es->bufpos, buf, len); es->bufpos += len; @@ -709,7 +712,7 @@ text = stringarray_get(&es->args, num - 1); oldlen = strlen(text); - text = dorealloc(text, oldlen + len + 1); + text = dorealloc(text, oldlen + 1, oldlen + len + 1); memcpy(text + oldlen, buf, len); text[oldlen+len] = '\0'; stringarray_set(&es->args, num - 1, text); @@ -798,10 +801,10 @@ newbuf = expand_substitute(p, es); newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false); - dofree(newbuf); + dostrfree(newbuf); expstate_destroyargs(es); doexpand(es, p, newbuf2, strlen(newbuf2)); - dofree(newbuf2); + dostrfree(newbuf2); es->curmacro->inuse = false; } @@ -851,7 +854,7 @@ newbuf = macroexpand(p, ei->string, strlen(ei->string), false); doexpand(es, p, newbuf, strlen(newbuf)); - dofree(newbuf); + dostrfree(newbuf); m->inuse = false; } else { es->curmacro = m; @@ -1101,6 +1104,9 @@ expand_got_eof(&es, p); ret = es.buf; es.buf = NULL; + /* trim to fit, so the malloc debugging won't complain */ + ret = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1); + es.bufpos = es.bufmax = 0; expstate_cleanup(&es); return ret; diff -r b156910b59b2 -r 337110e7240a main.c --- a/main.c Sat Mar 30 21:02:25 2013 -0400 +++ b/main.c Sat Mar 30 21:17:47 2013 -0400 @@ -167,7 +167,7 @@ } else { macro_undef(cm->macro); } - dofree(cm); + dofree(cm, sizeof(*cm)); } array_setsize(&commandline_macros, 0); } @@ -287,7 +287,7 @@ } else { file_readquote(&cf->where, cf->name); } - dofree(cf); + dofree(cf, sizeof(*cf)); } array_setsize(&commandline_files, 0); } @@ -926,7 +926,7 @@ num = stringarray_num(&freestrings); for (i=0; i linebufmax) { + oldmax = linebufmax; if (linebufmax == 0) { linebufmax = 64; } while (linebufpos + len > linebufmax) { linebufmax *= 2; } - linebuf = dorealloc(linebuf, linebufmax); + linebuf = dorealloc(linebuf, oldmax, linebufmax); } if (linebufpos == 0) { linebufplace = *p; diff -r b156910b59b2 -r 337110e7240a place.c --- a/place.c Sat Mar 30 21:02:25 2013 -0400 +++ b/place.c Sat Mar 30 21:17:47 2013 -0400 @@ -74,8 +74,8 @@ void placefile_destroy(struct placefile *pf) { - dofree(pf->name); - dofree(pf); + dostrfree(pf->name); + dofree(pf, sizeof(*pf)); } DESTROYALL_ARRAY(placefile, ); diff -r b156910b59b2 -r 337110e7240a utils.c --- a/utils.c Sat Mar 30 21:02:25 2013 -0400 +++ b/utils.c Sat Mar 30 21:17:47 2013 -0400 @@ -57,11 +57,12 @@ } void * -dorealloc(void *ptr, size_t len) +dorealloc(void *ptr, size_t oldlen, size_t newlen) { void *ret; - ret = realloc(ptr, len); + (void)oldlen; + ret = realloc(ptr, newlen); if (ret == NULL) { warnx("Out of memory"); die(); @@ -70,8 +71,9 @@ } void -dofree(void *ptr) +dofree(void *ptr, size_t len) { + (void)len; free(ptr); } @@ -125,6 +127,12 @@ return ret; } +void +dostrfree(char *s) +{ + dofree(s, strlen(s)+1); +} + size_t notrailingws(char *buf, size_t len) { diff -r b156910b59b2 -r 337110e7240a utils.h --- a/utils.h Sat Mar 30 21:02:25 2013 -0400 +++ b/utils.h Sat Mar 30 21:17:47 2013 -0400 @@ -40,13 +40,14 @@ void *domalloc(size_t len); -void *dorealloc(void *ptr, size_t len); -void dofree(void *ptr); +void *dorealloc(void *ptr, size_t oldlen, size_t newlen); +void dofree(void *ptr, size_t len); char *dostrdup(const char *s); char *dostrdup2(const char *s, const char *t); char *dostrdup3(const char *s, const char *t, const char *u); char *dostrndup(const char *s, size_t len); +void dostrfree(char *s); size_t notrailingws(char *buf, size_t len); bool is_identifier(const char *str);