From 30029b7ffb7f7bfda474301fd134e773fdf7db7e Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 8 Feb 2013 14:10:41 +0000 Subject: [PATCH] lib: Use CLEANUP_FREE_, CLEANUP_FREE__LIST macros in a few places in the library. --- src/file.c | 12 +++--- src/fuse.c | 27 ++++--------- src/inspect-apps.c | 9 ++--- src/inspect-fs-cd.c | 3 +- src/inspect-fs-windows.c | 86 ++++++++++++++++++---------------------- 5 files changed, 56 insertions(+), 81 deletions(-) diff --git a/src/file.c b/src/file.c index d2672ded2..7f4d4df5a 100644 --- a/src/file.c +++ b/src/file.c @@ -385,13 +385,15 @@ guestfs__lstatlist (guestfs_h *g, const char *dir, char * const*names) { size_t len = count_strings (names); size_t old_len; - struct guestfs_stat_list *ret, *stats; + struct guestfs_stat_list *ret; ret = safe_malloc (g, sizeof *ret); ret->len = 0; ret->val = NULL; while (len > 0) { + CLEANUP_FREE_STAT_LIST struct guestfs_stat_list *stats = NULL; + /* Note we don't need to free up the strings because take_strings * does not do a deep copy. */ @@ -413,8 +415,6 @@ guestfs__lstatlist (guestfs_h *g, const char *dir, char * const*names) ret->len * sizeof (struct guestfs_stat)); memcpy (&ret->val[old_len], stats->val, stats->len * sizeof (struct guestfs_stat)); - - guestfs_free_stat_list (stats); } return ret; @@ -427,13 +427,15 @@ guestfs__lxattrlist (guestfs_h *g, const char *dir, char *const *names) { size_t len = count_strings (names); size_t i, old_len; - struct guestfs_xattr_list *ret, *xattrs; + struct guestfs_xattr_list *ret; ret = safe_malloc (g, sizeof *ret); ret->len = 0; ret->val = NULL; while (len > 0) { + CLEANUP_FREE_XATTR_LIST struct guestfs_xattr_list *xattrs = NULL; + /* Note we don't need to free up the strings because take_strings * does not do a deep copy. */ @@ -461,8 +463,6 @@ guestfs__lxattrlist (guestfs_h *g, const char *dir, char *const *names) memcpy (ret->val[old_len].attrval, xattrs->val[i].attrval, xattrs->val[i].attrval_len); } - - guestfs_free_xattr_list (xattrs); } return ret; diff --git a/src/fuse.c b/src/fuse.c index fb5e94777..bdcdf48e3 100644 --- a/src/fuse.c +++ b/src/fuse.c @@ -127,9 +127,8 @@ mount_local_readdir (const char *path, void *buf, fuse_fill_dir_t filler, dir_cache_remove_all_expired (g, now); - struct guestfs_dirent_list *ents; - - ents = guestfs_readdir (g, path); + CLEANUP_FREE_DIRENT_LIST struct guestfs_dirent_list *ents = + guestfs_readdir (g, path); if (ents == NULL) RETURN_ERRNO; @@ -169,7 +168,8 @@ mount_local_readdir (const char *path, void *buf, fuse_fill_dir_t filler, names[i] = ents->val[i].name; names[i] = NULL; - struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names); + CLEANUP_FREE_STAT_LIST struct guestfs_stat_list *ss = + guestfs_lstatlist (g, path, names); if (ss) { for (i = 0; i < ss->len; ++i) { if (ss->val[i].ino >= 0) { @@ -192,10 +192,10 @@ mount_local_readdir (const char *path, void *buf, fuse_fill_dir_t filler, lsc_insert (g, path, names[i], now, &statbuf); } } - guestfs_free_stat_list (ss); } - struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names); + CLEANUP_FREE_XATTR_LIST struct guestfs_xattr_list *xattrs = + guestfs_lxattrlist (g, path, names); if (xattrs) { size_t ni, num; struct guestfs_xattr *first; @@ -216,7 +216,6 @@ mount_local_readdir (const char *path, void *buf, fuse_fill_dir_t filler, i--; } } - guestfs_free_xattr_list (xattrs); } char **links = guestfs_readlinklist (g, path, names); @@ -235,8 +234,6 @@ mount_local_readdir (const char *path, void *buf, fuse_fill_dir_t filler, free (names); } - guestfs_free_dirent_list (ents); - return 0; } @@ -254,9 +251,7 @@ mount_local_getattr (const char *path, struct stat *statbuf) return 0; } - struct guestfs_stat *r; - - r = guestfs_lstat (g, path); + CLEANUP_FREE_STAT struct guestfs_stat *r = guestfs_lstat (g, path); if (r == NULL) RETURN_ERRNO; @@ -274,8 +269,6 @@ mount_local_getattr (const char *path, struct stat *statbuf) statbuf->st_mtime = r->mtime; statbuf->st_ctime = r->ctime; - guestfs_free_stat (r); - return 0; } @@ -671,9 +664,7 @@ mount_local_statfs (const char *path, struct statvfs *stbuf) DECL_G (); DEBUG_CALL ("%s, %p", path, stbuf); - struct guestfs_statvfs *r; - - r = guestfs_statvfs (g, path); + CLEANUP_FREE_STATVFS struct guestfs_statvfs *r = guestfs_statvfs (g, path); if (r == NULL) RETURN_ERRNO; @@ -689,8 +680,6 @@ mount_local_statfs (const char *path, struct statvfs *stbuf) stbuf->f_flag = r->flag; stbuf->f_namemax = r->namemax; - guestfs_free_statvfs (r); - return 0; } diff --git a/src/inspect-apps.c b/src/inspect-apps.c index bd8d865cf..135adf7df 100644 --- a/src/inspect-apps.c +++ b/src/inspect-apps.c @@ -378,8 +378,7 @@ list_applications_rpm (guestfs_h *g, struct inspect_fs *fs) error: free_rpm_names_list (&list); - if (apps != NULL) - guestfs_free_application2_list (apps); + guestfs_free_application2_list (apps); return NULL; } @@ -472,7 +471,7 @@ list_applications_deb (guestfs_h *g, struct inspect_fs *fs) ret = apps; out: - if (ret == NULL && apps != NULL) + if (ret == NULL) guestfs_free_application2_list (apps); /* if (fp) @@ -529,7 +528,7 @@ list_applications_windows_from_path (guestfs_h *g, struct guestfs_application2_list *apps, const char **path, size_t path_len) { - struct guestfs_hivex_node_list *children = NULL; + CLEANUP_FREE_HIVEX_NODE_LIST struct guestfs_hivex_node_list *children = NULL; int64_t node; size_t i; @@ -592,8 +591,6 @@ list_applications_windows_from_path (guestfs_h *g, } } } - - guestfs_free_hivex_node_list (children); } static void diff --git a/src/inspect-fs-cd.c b/src/inspect-fs-cd.c index ef2999b3f..e4f257f94 100644 --- a/src/inspect-fs-cd.c +++ b/src/inspect-fs-cd.c @@ -486,7 +486,7 @@ int guestfs___check_installer_iso (guestfs_h *g, struct inspect_fs *fs, const char *device) { - struct guestfs_isoinfo *isoinfo; + CLEANUP_FREE_ISOINFO struct guestfs_isoinfo *isoinfo = NULL; const struct osinfo *osinfo; int r; @@ -497,7 +497,6 @@ guestfs___check_installer_iso (guestfs_h *g, struct inspect_fs *fs, return 0; r = guestfs___osinfo_map (g, isoinfo, &osinfo); - guestfs_free_isoinfo (isoinfo); if (r == -1) /* Fatal error. */ return -1; if (r == 0) /* Could not locate any matching ISO. */ diff --git a/src/inspect-fs-windows.c b/src/inspect-fs-windows.c index 10467a54b..346cc5574 100644 --- a/src/inspect-fs-windows.c +++ b/src/inspect-fs-windows.c @@ -224,7 +224,7 @@ check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs) const char *hivepath[] = { "Microsoft", "Windows NT", "CurrentVersion" }; size_t i; - struct guestfs_hivex_value_list *values = NULL; + CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values = NULL; if (guestfs_hivex_open (g, software_path, GUESTFS_HIVEX_OPEN_VERBOSE, g->verbose, -1) == -1) @@ -235,11 +235,11 @@ check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs) node = guestfs_hivex_node_get_child (g, node, hivepath[i]); if (node == -1) - goto out1; + goto out; if (node == 0) { perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); - goto out1; + goto out; } values = guestfs_hivex_node_values (g, node); @@ -248,43 +248,41 @@ check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs) int64_t value = values->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, value); if (key == NULL) - goto out2; + goto out; if (STRCASEEQ (key, "ProductName")) { fs->product_name = guestfs_hivex_value_utf8 (g, value); if (!fs->product_name) - goto out2; + goto out; } else if (STRCASEEQ (key, "CurrentVersion")) { CLEANUP_FREE char *version = guestfs_hivex_value_utf8 (g, value); if (!version) - goto out2; + goto out; char *major, *minor; if (match2 (g, version, re_windows_version, &major, &minor)) { fs->major_version = guestfs___parse_unsigned_int (g, major); free (major); if (fs->major_version == -1) { free (minor); - goto out2; + goto out; } fs->minor_version = guestfs___parse_unsigned_int (g, minor); free (minor); if (fs->minor_version == -1) - goto out2; + goto out; } } else if (STRCASEEQ (key, "InstallationType")) { fs->product_variant = guestfs_hivex_value_utf8 (g, value); if (!fs->product_variant) - goto out2; + goto out; } } ret = 0; - out2: - guestfs_free_hivex_value_list (values); - out1: + out: guestfs_hivex_close (g); return ret; @@ -314,7 +312,8 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) int ret = -1; int64_t root, node, value; - struct guestfs_hivex_value_list *values = NULL; + CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values = NULL; + CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values2 = NULL; int32_t dword; size_t i, count; CLEANUP_FREE void *buf = NULL; @@ -324,36 +323,36 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) if (guestfs_hivex_open (g, system_path, GUESTFS_HIVEX_OPEN_VERBOSE, g->verbose, -1) == -1) - goto out0; + goto out; root = guestfs_hivex_root (g); if (root == 0) - goto out1; + goto out; /* Get the CurrentControlSet. */ node = guestfs_hivex_node_get_child (g, root, "Select"); if (node == -1) - goto out1; + goto out; if (node == 0) { error (g, "hivex: could not locate HKLM\\SYSTEM\\Select"); - goto out1; + goto out; } value = guestfs_hivex_node_get_value (g, node, "Current"); if (value == -1) - goto out1; + goto out; if (value == 0) { error (g, "hivex: HKLM\\System\\Select Default entry not found"); - goto out1; + goto out; } /* XXX Should check the type. */ buf = guestfs_hivex_value_value (g, value, &buflen); if (buflen != 4) { error (g, "hivex: HKLM\\System\\Select\\Current expected to be DWORD"); - goto out1; + goto out; } dword = le32toh (*(int32_t *)buf); fs->windows_current_control_set = safe_asprintf (g, "ControlSet%03d", dword); @@ -364,7 +363,7 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) */ node = guestfs_hivex_node_get_child (g, root, "MountedDevices"); if (node == -1) - goto out1; + goto out; if (node == 0) /* Not found: skip getting drive letter mappings (RHBZ#803664). */ @@ -380,7 +379,7 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) CLEANUP_FREE char *key = guestfs_hivex_value_key (g, values->val[i].hivex_value_h); if (key == NULL) - goto out1; + goto out; if (STRCASEEQLEN (key, "\\DosDevices\\", 12) && c_isalpha (key[12]) && key[13] == ':') count++; @@ -392,7 +391,7 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) int64_t v = values->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, v); if (key == NULL) - goto out1; + goto out; if (STRCASEEQLEN (key, "\\DosDevices\\", 12) && c_isalpha (key[12]) && key[13] == ':') { /* Get the binary value. Is it a fixed disk? */ @@ -424,39 +423,36 @@ check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) } if (node == -1) - goto out1; + goto out; if (node == 0) { perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\%s\\Services\\Tcpip\\Parameters", fs->windows_current_control_set); - goto out1; + goto out; } - guestfs_free_hivex_value_list (values); - values = guestfs_hivex_node_values (g, node); - if (values == NULL) - goto out1; + values2 = guestfs_hivex_node_values (g, node); + if (values2 == NULL) + goto out; - for (i = 0; i < values->len; ++i) { - int64_t v = values->val[i].hivex_value_h; + for (i = 0; i < values2->len; ++i) { + int64_t v = values2->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, v); if (key == NULL) - goto out1; + goto out; if (STRCASEEQ (key, "Hostname")) { fs->hostname = guestfs_hivex_value_utf8 (g, v); if (!fs->hostname) - goto out1; + goto out; } /* many other interesting fields here ... */ } ret = 0; - out1: + out: guestfs_hivex_close (g); - out0: - if (values) guestfs_free_hivex_value_list (values); return ret; } @@ -471,9 +467,8 @@ static char * map_registry_disk_blob (guestfs_h *g, const void *blob) { CLEANUP_FREE_STRING_LIST char **devices = NULL; - struct guestfs_partition_list *partitions = NULL; + CLEANUP_FREE_PARTITION_LIST struct guestfs_partition_list *partitions = NULL; size_t i, j, len; - char *ret = NULL; uint64_t part_offset; /* First 4 bytes are the disk ID. Search all devices to find the @@ -481,7 +476,7 @@ map_registry_disk_blob (guestfs_h *g, const void *blob) */ devices = guestfs_list_devices (g); if (devices == NULL) - goto out; + return NULL; for (i = 0; devices[i] != NULL; ++i) { /* Read the disk ID. */ @@ -494,7 +489,7 @@ map_registry_disk_blob (guestfs_h *g, const void *blob) if (memcmp (diskid, blob, 4) == 0) /* found it */ goto found_disk; } - goto out; + return NULL; found_disk: /* Next 8 bytes are the offset of the partition in bytes(!) given as @@ -511,22 +506,17 @@ map_registry_disk_blob (guestfs_h *g, const void *blob) partitions = guestfs_part_list (g, devices[i]); if (partitions == NULL) - goto out; + return NULL; for (j = 0; j < partitions->len; ++j) { if (partitions->val[j].part_start == part_offset) /* found it */ goto found_partition; } - goto out; + return NULL; found_partition: /* Construct the full device name. */ - ret = safe_asprintf (g, "%s%d", devices[i], partitions->val[j].part_num); - - out: - if (partitions) - guestfs_free_partition_list (partitions); - return ret; + return safe_asprintf (g, "%s%d", devices[i], partitions->val[j].part_num); } char *