From 63c7eb3d86b1ec4b6cbd0d346c805bde079beab9 Mon Sep 17 00:00:00 2001 From: jessica parsons Date: Mon, 8 Sep 2025 09:50:17 -0700 Subject: [PATCH] change amalloc so it fills malloc()ed memory with a nun-null pattern, which found a WHOLE BUNCH of places where I was not null-terminating strings but instead was taking advantage of amalloc always zeroing new allocations. --- amalloc.c | 8 +++++++- css.c | 2 +- cstring.h | 2 ++ generate.c | 7 ++++--- markdown.c | 17 +++++++---------- mkdio.c | 5 ++--- theme.c | 3 +-- toc.c | 2 +- xml.c | 4 ++-- 9 files changed, 27 insertions(+), 23 deletions(-) diff --git a/amalloc.c b/amalloc.c index 922db77..9592988 100644 --- a/amalloc.c +++ b/amalloc.c @@ -63,7 +63,13 @@ acalloc(int count, int size) void* amalloc(int size) { - return acalloc(size,1); + void *ret = acalloc(1, size); + + if ( ret ) { + /* explicitally fill the mallocated memory with a nonzero character */ + memset(ret, 0x8f, size); + } + return ret; } diff --git a/css.c b/css.c index e86e54e..ecbd445 100644 --- a/css.c +++ b/css.c @@ -57,7 +57,7 @@ mkd_css(Document *d, char **res) /* null-terminate, then strdup() into a free()able memory * chunk */ - EXPAND(f) = 0; + COMPLETE(f); *res = strdup(T(f)); } DELETE(f); diff --git a/cstring.h b/cstring.h index f0b7410..f3ca6ab 100644 --- a/cstring.h +++ b/cstring.h @@ -69,6 +69,8 @@ typedef STRING(char) Cstring; +#define COMPLETE(t) (EXPAND(t) = 0),(S(t)--) + extern void Csputc(int, Cstring *); extern int Csprintf(Cstring *, char *, ...); extern int Cswrite(Cstring *, char *, int); diff --git a/generate.c b/generate.c index 448158a..10c3d9f 100644 --- a/generate.c +++ b/generate.c @@ -408,6 +408,7 @@ linkysize(MMIOT *f, Footnote *ref) EXPAND(width) = c; c = pull(f); } + COMPLETE(width); } else pull(f); @@ -424,6 +425,7 @@ linkysize(MMIOT *f, Footnote *ref) EXPAND(height) = c; c = pull(f); } + COMPLETE(height); } if ( isspace(c) ) @@ -502,7 +504,7 @@ linky_extended_attributes(MMIOT *f, struct footnote *p, int start) while ( (c = pull(f)) != '}' ) EXPAND(p->extended_attr) = c; - EXPAND(p->extended_attr) = 0; + COMPLETE(p->extended_attr); } @@ -2151,8 +2153,7 @@ mkd_document(Document *p, char **res) /* Add a null byte at the end of the generated html, * but pretend it doesn't exist. */ - EXPAND(p->ctx->out) = 0; - --S(p->ctx->out); + COMPLETE(p->ctx->out); } } diff --git a/markdown.c b/markdown.c index 667907a..ce8da8e 100644 --- a/markdown.c +++ b/markdown.c @@ -169,8 +169,7 @@ splitline(Line *t, int cutpoint) t->next = tmp; SUFFIX(tmp->text, T(t->text)+cutpoint, S(t->text)-cutpoint); - EXPAND(tmp->text) = 0; - S(tmp->text)--; + COMPLETE(tmp->text); S(t->text) = cutpoint; } @@ -1091,7 +1090,7 @@ footnote_height_and_width(char *s, struct footnote *foot) /* specialcase for % width */ if ( s[i] == '%' ) EXPAND(foot->width) = '%'; - EXPAND(foot->width) = 0; + COMPLETE(foot->width); } if ( s[i] == 'x' ) { i++; @@ -1099,7 +1098,7 @@ footnote_height_and_width(char *s, struct footnote *foot) EXPAND(foot->height) = s[i++]; if ( s[i] == '%' ) EXPAND(foot->height) = '%'; - EXPAND(foot->height) = 0; + COMPLETE(foot->height); } } #endif @@ -1129,8 +1128,7 @@ addfootnote(Line *p, MMIOT* f) /* keep the footnote label */ for (j=i=p->dle+1; T(p->text)[j] != ']'; j++) EXPAND(foot->tag) = T(p->text)[j]; - EXPAND(foot->tag) = 0; - S(foot->tag)--; + COMPLETE(foot->tag); /* consume the closing ]: */ j = nextnonblank(p, j+2); @@ -1154,8 +1152,7 @@ addfootnote(Line *p, MMIOT* f) while ( (j < S(p->text)) && !isspace(T(p->text)[j]) ) EXPAND(foot->link) = T(p->text)[j++]; - EXPAND(foot->link) = 0; - S(foot->link)--; + COMPLETE(foot->link); j = nextnonblank(p,j); #if 0 @@ -1172,6 +1169,7 @@ addfootnote(Line *p, MMIOT* f) if ( T(p->text)[i] == '}' ) { for ( j++; j < i; j++ ) EXPAND(foot->extended_attr) = T(p->text)[j]; + COMPLETE(foot->extended_attr); j++; } } @@ -1198,8 +1196,7 @@ addfootnote(Line *p, MMIOT* f) --S(foot->title); if ( S(foot->title) ) /* skip trailing quote */ --S(foot->title); - EXPAND(foot->title) = 0; - --S(foot->title); + COMPLETE(foot->title); } ___mkd_freeLine(p); diff --git a/mkdio.c b/mkdio.c index 325363f..362d13a 100644 --- a/mkdio.c +++ b/mkdio.c @@ -69,8 +69,7 @@ __mkd_enqueue(Document* a, Cstring *line) ++xp; } } - EXPAND(p->text) = 0; - S(p->text)--; + COMPLETE(p->text); p->dle = mkd_firstnonblank(p); } @@ -337,7 +336,7 @@ mkd_line(char *bfr, int size, char **res, mkd_flag_t* flags) mkd_parse_line(bfr, size, &f, flags); if ( len = S(f.out) ) { - EXPAND(f.out) = 0; + COMPLETE(f.out); /* strdup() doesn't use amalloc(), so in an amalloc()ed * build this copies the string safely out of our memory * paranoia arena. In a non-amalloc world, it's a spurious diff --git a/theme.c b/theme.c index 244e77d..2c6069b 100644 --- a/theme.c +++ b/theme.c @@ -294,8 +294,7 @@ finclude(MMIOT *doc, FILE *out, mkd_flag_t *flags, int whence) EXPAND(include) = c; if ( c != EOF ) { - EXPAND(include) = 0; - S(include)--; + COMPLETE(include); if (( f = fopen(T(include), "r") )) { while ( (c = getc(f)) != EOF ) diff --git a/toc.c b/toc.c index 2fc0a37..7f73f20 100644 --- a/toc.c +++ b/toc.c @@ -97,7 +97,7 @@ mkd_toc(Document *p, char **doc) if ( (size = S(res)) > 0 ) { /* null-terminate & strdup into a free()able memory chunk */ - EXPAND(res) = 0; + COMPLETE(res); *doc = strdup(T(res)); } DELETE(res); diff --git a/xml.c b/xml.c index 7124e87..9fe34a4 100644 --- a/xml.c +++ b/xml.c @@ -77,9 +77,9 @@ mkd_xml(char *p, int size, char **res) /* null terminate, strdup() into a free()able memory block, * and return the size of everything except the null terminator */ - EXPAND(f) = 0; + COMPLETE(f); *res = strdup(T(f)); - size = S(f)-1; + size = S(f); DELETE(f); return size; }