diff options
Diffstat (limited to 'src/testtextcat.c')
-rw-r--r-- | src/testtextcat.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/testtextcat.c b/src/testtextcat.c index dd0c464..2ac82fe 100644 --- a/src/testtextcat.c +++ b/src/testtextcat.c @@ -49,7 +49,7 @@ char *myread(FILE * fp) { - char *buf; + char *buf, *newbuf; size_t size = 0; size_t maxsize = BLOCKSIZE * 2; @@ -61,19 +61,27 @@ char *myread(FILE * fp) if (size + BLOCKSIZE > maxsize) { maxsize *= 2; - buf = (char *)realloc(buf, maxsize); + newbuf = (char *)realloc(buf, maxsize); + if (!newbuf) + free(buf); + buf = newbuf; } } - while (!feof(stdin)); + while (!feof(stdin) && buf); - buf[size] = '\0'; - buf = (char *)realloc(buf, size + 1); + if (buf) + { + buf[size] = '\0'; + newbuf = (char *)realloc(buf, size + 1); + if (!newbuf) + free(buf); + buf = newbuf; + } return buf; } - int main(int argc, char **argv) { void *h; |