catch "can't happen" int overflow in stralloc_catm_interal

add comment in stralloc_ready
master
leitner 3 years ago
parent ea0c6b8168
commit 58283caf58

@ -7,8 +7,13 @@ int stralloc_catm_internal(stralloc* sa, ...) {
const char* s; const char* s;
size_t n=0; size_t n=0;
va_start(a,sa); va_start(a,sa);
while ((s=va_arg(a,const char*))) while ((s=va_arg(a,const char*))) {
n += strlen(s); size_t tmp = strlen(s);
if (n + tmp < n) return 0; // integer overflow
// integer overflow should not be possible, but someone could pass
// the same string twice to provoke it. Better check than sorry.
n += tmp;
}
va_end(a); va_end(a);
stralloc_readyplus(sa,n); stralloc_readyplus(sa,n);

@ -9,7 +9,7 @@
* old space, and returns 1. Note that this changes sa.s. */ * old space, and returns 1. Note that this changes sa.s. */
int stralloc_ready(stralloc *sa,size_t len) { int stralloc_ready(stralloc *sa,size_t len) {
register size_t wanted=len+(len>>3)+30; /* heuristic from djb */ register size_t wanted=len+(len>>3)+30; /* heuristic from djb */
if (wanted<len) wanted=len; if (wanted<len) wanted=len; // in case of integer overflow
if (!sa->s || sa->a<len) { if (!sa->s || sa->a<len) {
register char* tmp; register char* tmp;
if (!(tmp=realloc(sa->s,wanted))) if (!(tmp=realloc(sa->s,wanted)))

Loading…
Cancel
Save