The current overflow checking is broken in the corner case of the strings'
combined length being exactly SIZE_MAX: After the loop, l would be SIZE_MAX,
but we're not testing whether the l+1 expression overflows.
Fix it by simply pre-accounting for the final '\0': initialize l to 1 instead
of 0.
char *strjoin_real(const char *x, ...) {
va_list ap;
- size_t l = 0;
+ size_t l = 1;
char *r, *p;
va_start(ap, x);
}
va_end(ap);
- p = r = new(char, l+1);
+ p = r = new(char, l);
if (!r)
return NULL;