[PATCH] libparted: linux: don't deref NULL upon failed malloc or realloc
Jim Meyering
meyering at redhat.com
Sat Nov 7 16:02:50 UTC 2009
* libparted/arch/linux.c (_read_fd): Handle allocation failure.
---
libparted/arch/linux.c | 19 ++++++++++++++++---
1 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index f7e6a5e..bf050c4 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -283,7 +283,14 @@ _read_fd (int fd, char **buf)
break;
filesize += s;
size += s;
- *buf = realloc (*buf, size);
+ char *new_buf = realloc (*buf, size);
+ if (new_buf == NULL) {
+ int saved_errno = errno;
+ free (*buf);
+ errno = saved_errno;
+ return -1;
+ }
+ *buf = new_buf;
} while (1);
if (filesize == 0 && s < 0) {
@@ -291,8 +298,14 @@ _read_fd (int fd, char **buf)
*buf = NULL;
return -1;
} else {
- /* there is always some excess memory left unused */
- *buf = realloc (*buf, filesize+1);
+ char *new_buf = realloc (*buf, filesize + 1);
+ if (new_buf == NULL) {
+ int saved_errno = errno;
+ free (*buf);
+ errno = saved_errno;
+ return -1;
+ }
+ *buf = new_buf;
(*buf)[filesize] = '\0';
}
--
1.6.5.2.303.g13162
More information about the parted-devel
mailing list