From: Mark Andrews Date: Tue, 11 Jun 2013 06:08:16 +0000 (+1000) Subject: handle realloc failure [RT #32105] X-Git-Tag: v4_3_0a1~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d210be23379eed71f096f80d7859cbbeb347596;p=thirdparty%2Fdhcp.git handle realloc failure [RT #32105] --- diff --git a/tests/t_api.c b/tests/t_api.c index 23a4312ab..e61622646 100644 --- a/tests/t_api.c +++ b/tests/t_api.c @@ -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); } }