[Libguestfs] [PATCH] Use safe_realloc() in favor of realloc overall.

Nikos Skalkotos skalkoto at grnet.gr
Thu Jun 4 08:56:02 UTC 2015


---
 src/inspect-fs-unix.c | 22 +++++++---------------
 src/inspect-fs.c      | 21 ++++++---------------
 2 files changed, 13 insertions(+), 30 deletions(-)

diff --git a/src/inspect-fs-unix.c b/src/inspect-fs-unix.c
index 8ffd85d..c9bbad9 100644
--- a/src/inspect-fs-unix.c
+++ b/src/inspect-fs-unix.c
@@ -90,8 +90,8 @@ static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
-static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
-                            const char *mountable, const char *mp);
+static void add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
+                             const char *mountable, const char *mp);
 static char *resolve_fstab_device (guestfs_h *g, const char *spec,
                                    Hash_table *md_map,
                                    enum inspect_os_type os_type);
@@ -850,8 +850,7 @@ guestfs_int_check_coreos_root (guestfs_h *g, struct inspect_fs *fs)
   /* CoreOS does not contain /etc/fstab to determine the mount points.
    * Associate this filesystem with the "/" mount point.
    */
-  if (add_fstab_entry (g, fs, fs->mountable, "/") == -1)
-    return -1;
+  add_fstab_entry (g, fs, fs->mountable, "/");
 
   return 0;
 }
@@ -880,8 +879,7 @@ guestfs_int_check_coreos_usr (guestfs_h *g, struct inspect_fs *fs)
   /* CoreOS does not contain /etc/fstab to determine the mount points.
    * Associate this filesystem with the "/usr" mount point.
    */
-  if (add_fstab_entry (g, fs, fs->mountable, "/usr") == -1)
-    return -1;
+  add_fstab_entry (g, fs, fs->mountable, "/usr");
 
   return 0;
 }
@@ -1197,7 +1195,7 @@ check_fstab (guestfs_h *g, struct inspect_fs *fs)
       }
     }
 
-    if  (add_fstab_entry (g, fs, mountable, mp) == -1) return -1;
+    add_fstab_entry (g, fs, mountable, mp);
   }
 
   return 0;
@@ -1211,7 +1209,7 @@ check_fstab (guestfs_h *g, struct inspect_fs *fs)
  *
  * 'mp' is the mount point, which could also be 'swap' or 'none'.
  */
-static int
+static void
 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
                  const char *mountable, const char *mountpoint)
 {
@@ -1222,11 +1220,7 @@ add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
   size_t n = fs->nr_fstab + 1;
   struct inspect_fstab_entry *p;
 
-  p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
-  if (p == NULL) {
-    perrorf (g, "realloc");
-    return -1;
-  }
+  p = safe_realloc (g, fs->fstab, n * sizeof (struct inspect_fstab_entry));
 
   fs->fstab = p;
   fs->nr_fstab = n;
@@ -1236,8 +1230,6 @@ add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
   fs->fstab[n-1].mountpoint = safe_strdup (g, mountpoint);
 
   debug (g, "fstab: mountable=%s mountpoint=%s", mountable, mountpoint);
-
-  return 0;
 }
 
 /* Compute a uuid hash as a simple xor of of its 4 32bit components */
diff --git a/src/inspect-fs.c b/src/inspect-fs.c
index 09a7005..073ae57 100644
--- a/src/inspect-fs.c
+++ b/src/inspect-fs.c
@@ -47,7 +47,7 @@ COMPILE_REGEXP (re_major_minor, "(\\d+)\\.(\\d+)", 0)
 static int check_filesystem (guestfs_h *g, const char *mountable,
                              const struct guestfs_internal_mountable *m,
                              int whole_device);
-static int extend_fses (guestfs_h *g);
+static void extend_fses (guestfs_h *g);
 static int get_partition_context (guestfs_h *g, const char *partition, int *partnum_ret, int *nr_partitions_ret);
 
 /* Find out if 'device' contains a filesystem.  If it does, add
@@ -75,8 +75,7 @@ guestfs_int_check_for_filesystem_on (guestfs_h *g, const char *mountable)
          mountable, vfs_type ? vfs_type : "failed to get vfs type");
 
   if (is_swap) {
-    if (extend_fses (g) == -1)
-      return -1;
+    extend_fses (g);
     fs = &g->fses[g->nr_fses-1];
     fs->mountable = safe_strdup (g, mountable);
     return 0;
@@ -95,8 +94,7 @@ guestfs_int_check_for_filesystem_on (guestfs_h *g, const char *mountable)
   }
 
   if (whole_device) {
-    if (extend_fses (g) == -1)
-      return -1;
+    extend_fses (g);
     fs = &g->fses[g->nr_fses-1];
 
     r = guestfs_int_check_installer_iso (g, fs, m->im_device);
@@ -145,8 +143,7 @@ check_filesystem (guestfs_h *g, const char *mountable,
   /* Not CLEANUP_FREE, as it will be cleaned up with inspection info */
   char *windows_systemroot = NULL;
 
-  if (extend_fses (g) == -1)
-    return -1;
+  extend_fses (g);
 
   if (!whole_device && m->im_type == MOUNTABLE_DEVICE &&
       guestfs_int_is_partition (g, m->im_device)) {
@@ -331,24 +328,18 @@ check_filesystem (guestfs_h *g, const char *mountable,
   return 0;
 }
 
-static int
+static void
 extend_fses (guestfs_h *g)
 {
   size_t n = g->nr_fses + 1;
   struct inspect_fs *p;
 
-  p = realloc (g->fses, n * sizeof (struct inspect_fs));
-  if (p == NULL) {
-    perrorf (g, "realloc");
-    return -1;
-  }
+  p = safe_realloc (g, g->fses, n * sizeof (struct inspect_fs));
 
   g->fses = p;
   g->nr_fses = n;
 
   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
-
-  return 0;
 }
 
 /* Given a partition (eg. /dev/sda2) then return the partition number
-- 
2.1.0




More information about the Libguestfs mailing list