Comment by numeromancer
12 years ago
NB: In the following function:
/* Like sdscatpritf() but gets va_list instead of being variadic. */
sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
va_list cpy;
char *buf, *t;
size_t buflen = 16;
while(1) {
buf = malloc(buflen);
if (buf == NULL) return NULL;
buf[buflen-2] = '\0';
va_copy(cpy,ap);
vsnprintf(buf, buflen, fmt, cpy);
va_end(cpy); // <--- add this ----
if (buf[buflen-2] != '\0') {
free(buf);
buflen *= 2;
continue;
}
break;
}
t = sdscat(s, buf);
free(buf);
return t;
}
From the `man va_copy` on my system:
Each invocation of va_copy() must be matched by
a corresponding invocation of va_end() in the same
function.
This is not likely to be a problem in most systems, but it can't hurt to be formally correct.
I think you meant "va_end(ap);".
Created a pull request for you: https://github.com/antirez/sds/pull/8
No, I'm pretty sure that `va_end(cpy)` is correct. `ap` is the `va_list` passed into the function. `cpy` is the local copy.
You're right. Fixed.