continue;
opt->nr_choices++;
- opt = realloc(opt, sizeof(*opt) +
- opt->nr_choices * sizeof(*choice));
+ realloc_inplace(opt, sizeof(*opt) +
+ opt->nr_choices * sizeof(*choice));
if (!opt)
return -ENOMEM;
break;
*n *= 2;
- *lineptr = realloc(*lineptr, *n);
+ realloc_inplace(*lineptr, *n);
if (!*lineptr)
return -1;
}
break;
}
- buf->data = realloc(buf->data, new_buf_len);
+ realloc_inplace(buf->data, new_buf_len);
if (!buf->data) {
buf->error = -ENOMEM;
break;
lastchunk = 1;
goto skip;
}
- body = realloc(body, done + chunklen + 1);
+ realloc_inplace(body, done + chunklen + 1);
if (!body)
return -ENOMEM;
while (chunklen) {
/* HTTP 1.0 response. Just eat all we can in 16KiB chunks */
while (1) {
- body = realloc(body, done + 16384);
+ realloc_inplace(body, done + 16384);
if (!body)
return -ENOMEM;
i = openconnect_SSL_read(vpninfo, body + done, 16384);
return i;
} else {
/* Connection closed. Reduce allocation to just what we need */
- body = realloc(body, done + 1);
+ realloc_inplace(body, done + 1);
if (!body)
return -ENOMEM;
break;
char *openconnect__strcasestr(const char *haystack, const char *needle);
#endif
+/* I always coded as if it worked like this. Now it does. */
+#define realloc_inplace(p, size) do { \
+ void *__realloc_old = p; \
+ p = realloc(p, size); \
+ if (size && !p) \
+ free(__realloc_old); \
+ } while (0)
+
/****************************************************************************/
/* tun.c */