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.

This commit is contained in:
jessica parsons
2025-09-08 09:50:17 -07:00
parent 985091d673
commit 63c7eb3d86
9 changed files with 27 additions and 23 deletions
+7 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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);
+2
View File
@@ -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);
+4 -3
View File
@@ -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);
}
}
+7 -10
View File
@@ -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);
+2 -3
View File
@@ -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
+1 -2
View File
@@ -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 )
+1 -1
View File
@@ -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);
+2 -2
View File
@@ -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;
}