From: Mark Andrews Date: Tue, 11 Jun 2013 06:03:22 +0000 (+1000) Subject: handle realloc failure [RT #32105] X-Git-Tag: v9.10.0a1~293 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=0a6bfbc939dff0ad039e8ff53dd6a4b14565b459;p=thirdparty%2Fbind9.git handle realloc failure [RT #32105] --- diff --git a/lib/tests/t_api.c b/lib/tests/t_api.c index 4011375189b..26406315294 100644 --- a/lib/tests/t_api.c +++ b/lib/tests/t_api.c @@ -514,12 +514,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; @@ -535,7 +535,8 @@ t_fgetbs(FILE *fp) { buf = (char *)realloc(buf, size * sizeof(char)); if (buf == NULL) - break; + goto err; + old = buf; p = buf + n; } } @@ -546,7 +547,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); } }