]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
handle realloc failure [RT #32105]
authorMark Andrews <marka@isc.org>
Tue, 11 Jun 2013 06:08:16 +0000 (16:08 +1000)
committerMark Andrews <marka@isc.org>
Tue, 11 Jun 2013 06:08:16 +0000 (16:08 +1000)
tests/t_api.c

index 23a4312ab9cbc707766013b26f197ab654981e29..e6162264604e33033f65dddc42e245968d0557f6 100644 (file)
@@ -537,12 +537,12 @@ t_fgetbs(FILE *fp) {
        int     c;
        size_t  n;
        size_t  size;
-       char    *buf;
+       char    *buf, *old;
        char    *p;
 
-       n       = 0;
-       size    = T_BUFSIZ;
-       buf     = (char *) malloc(T_BUFSIZ * sizeof(char));
+       n = 0;
+       size = T_BUFSIZ;
+       old = buf = (char *) malloc(T_BUFSIZ * sizeof(char));
 
        if (buf != NULL) {
                p = buf;
@@ -558,7 +558,8 @@ t_fgetbs(FILE *fp) {
                                buf = (char *)realloc(buf,
                                                      size * sizeof(char));
                                if (buf == NULL)
-                                       break;
+                                       goto err;
+                               old = buf;
                                p = buf + n;
                        }
                }
@@ -569,7 +570,10 @@ t_fgetbs(FILE *fp) {
                }
                return (buf);
        } else {
-               fprintf(stderr, "malloc failed %d", errno);
+ err:
+               if (old != NULL)
+                       free(old);
+               fprintf(stderr, "malloc/realloc failed %d", errno);
                return(NULL);
        }
 }