xfs
[Top] [All Lists]

[PATCH 1/5] xfsprogs: libxcmd: don't clobber fs_table on realloc()

To: xfs@xxxxxxxxxxx
Subject: [PATCH 1/5] xfsprogs: libxcmd: don't clobber fs_table on realloc()
From: Alex Elder <aelder@xxxxxxx>
Date: Wed, 28 Sep 2011 05:57:08 -0500
Cc: Alex Elder <aelder@xxxxxxx>
In-reply-to: <1317207432-8464-1-git-send-email-aelder@xxxxxxx>
References: <1317207432-8464-1-git-send-email-aelder@xxxxxxx>
In fs_table_insert(), realloc() is called to resize the global
fs_table.  If it fails, it overwrites a previously valid fs_table
pointer with NULL.

Instead, assign the return value to a local temporary and overwrite
fs_table only if the realloc() call succeeds.  The only defined
errno value for a realloc() failure is ENOMEM, so return that
explicitly in the event it fails.

Signed-off-by: Alex Elder <aelder@xxxxxxx>
---
 libxcmd/paths.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/libxcmd/paths.c b/libxcmd/paths.c
index 1dbe0be..0e02eb7 100644
--- a/libxcmd/paths.c
+++ b/libxcmd/paths.c
@@ -102,6 +102,7 @@ fs_table_insert(
        char            *fsrt)
 {
        dev_t           datadev, logdev, rtdev;
+       struct fs_path  *tmp_fs_table;
 
        if (!dir || !fsname)
                return EINVAL;
@@ -114,9 +115,10 @@ fs_table_insert(
        if (fsrt && (fsrt = fs_device_number(fsrt, &rtdev, 1)) == NULL)
                return errno;
 
-       fs_table = realloc(fs_table, sizeof(fs_path_t) * (fs_count + 1));
-       if (!fs_table)
-               return errno;
+       tmp_fs_table = realloc(fs_table, sizeof(fs_path_t) * (fs_count + 1));
+       if (!tmp_fs_table)
+               return ENOMEM;
+       fs_table = tmp_fs_table;
 
        fs_path = &fs_table[fs_count];
        fs_path->fs_dir = dir;
-- 
1.7.6.2

<Prev in Thread] Current Thread [Next in Thread>