summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2012-06-28 09:28:54 +0100
committerCaolán McNamara <caolanm@redhat.com>2012-06-28 09:28:54 +0100
commitc722db62f63a7619408cbea699d019c8bd9a283f (patch)
tree16794137813a86005dc804fa16dffac866ff32f6 /src
parent9b8c553e7206f3bfd52ca501990f82afd00f252b (diff)
If realloc() fails the original block is left untouched but NULL is returned
Diffstat (limited to 'src')
-rw-r--r--src/createfp.c20
-rw-r--r--src/testtextcat.c20
2 files changed, 28 insertions, 12 deletions
diff --git a/src/createfp.c b/src/createfp.c
index 24166ed..fabc9cd 100644
--- a/src/createfp.c
+++ b/src/createfp.c
@@ -48,7 +48,7 @@
char *myread(FILE * fp)
{
- char *buf;
+ char *buf, *newbuf;
size_t size = 0;
size_t maxsize = BLOCKSIZE * 2;
@@ -60,19 +60,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 **args)
{
void *h;
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;