diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-03-08 11:06:46 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-03-08 13:21:52 +0000 |
commit | 35d5be22b1c09c3a99b445f65453601ec25b9f60 (patch) | |
tree | 75e2009377b6d759987ad7140a00290ebdb49962 /src | |
parent | 0ffa223a75fe4b5b077e4ec68534155039772c3a (diff) | |
download | libguestfs-35d5be22b1c09c3a99b445f65453601ec25b9f60.tar.gz libguestfs-35d5be22b1c09c3a99b445f65453601ec25b9f60.tar.xz libguestfs-35d5be22b1c09c3a99b445f65453601ec25b9f60.zip |
Check return values from calloc (found by Coverity).
Error: NULL_RETURNS:
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:417: returned_null: Function "calloc" returns null (checked 67 out of 81 times).
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:417: var_assigned: Assigning: "ret" = null return value from "calloc".
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:418: dereference: Dereferencing a null pointer "ret".
[...]
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:374: returned_null: Function "calloc" returns null (checked 67 out of 81 times).
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:374: var_assigned: Assigning: "ret" = null return value from "calloc".
/builddir/build/BUILD/libguestfs-1.16.5/src/inspect.c:375: dereference: Dereferencing a null pointer "ret".
Diffstat (limited to 'src')
-rw-r--r-- | src/inspect.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/inspect.c b/src/inspect.c index d3bb76a4..4a4871f4 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -371,23 +371,20 @@ guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root) if (!fs) return NULL; +#define CRITERION(fs, i) fs->fstab[i].mountpoint[0] == '/' + char **ret; + size_t i, count, nr = fs->nr_fstab; - /* If no fstab information (Windows) return just the root. */ - if (fs->nr_fstab == 0) { - ret = calloc (3, sizeof (char *)); - ret[0] = safe_strdup (g, "/"); - ret[1] = safe_strdup (g, root); - ret[2] = NULL; - return ret; + if (nr == 0) + count = 1; + else { + count = 0; + for (i = 0; i < nr; ++i) + if (CRITERION (fs, i)) + count++; } -#define CRITERION fs->fstab[i].mountpoint[0] == '/' - size_t i, count = 0; - for (i = 0; i < fs->nr_fstab; ++i) - if (CRITERION) - count++; - /* Hashtables have 2N+1 entries. */ ret = calloc (2*count+1, sizeof (char *)); if (ret == NULL) { @@ -395,9 +392,17 @@ guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root) return NULL; } + /* If no fstab information (Windows) return just the root. */ + if (nr == 0) { + ret[0] = safe_strdup (g, "/"); + ret[1] = safe_strdup (g, root); + ret[2] = NULL; + return ret; + } + count = 0; - for (i = 0; i < fs->nr_fstab; ++i) - if (CRITERION) { + for (i = 0; i < nr; ++i) + if (CRITERION (fs, i)) { ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint); ret[2*count+1] = safe_strdup (g, fs->fstab[i].device); count++; @@ -415,23 +420,22 @@ guestfs__inspect_get_filesystems (guestfs_h *g, const char *root) return NULL; char **ret; + size_t i, nr = fs->nr_fstab; + + ret = calloc (nr == 0 ? 2 : nr+1, sizeof (char *)); + if (ret == NULL) { + perrorf (g, "calloc"); + return NULL; + } /* If no fstab information (Windows) return just the root. */ - if (fs->nr_fstab == 0) { - ret = calloc (2, sizeof (char *)); + if (nr == 0) { ret[0] = safe_strdup (g, root); ret[1] = NULL; return ret; } - ret = calloc (fs->nr_fstab + 1, sizeof (char *)); - if (ret == NULL) { - perrorf (g, "calloc"); - return NULL; - } - - size_t i; - for (i = 0; i < fs->nr_fstab; ++i) + for (i = 0; i < nr; ++i) ret[i] = safe_strdup (g, fs->fstab[i].device); return ret; |