This fixes two unchecked realloc calls.

I went ahead and preserved errno (usu ENOMEM) in the
failing case even though callers ignore it (which is no big deal,
considering what they're doing).

>From df09d9a0bc1b0a213031f14ade94379fb7028d09 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Sat, 7 Nov 2009 17:02:50 +0100
Subject: [PATCH] libparted: linux: don't deref NULL upon failed malloc or 
realloc

* 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

_______________________________________________
parted-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/parted-devel

Reply via email to