diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/inspect_fs.c | 8 | ||||
-rw-r--r-- | src/inspect_fs_unix.c | 20 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/inspect_fs.c b/src/inspect_fs.c index baae5428..0859a2b4 100644 --- a/src/inspect_fs.c +++ b/src/inspect_fs.c @@ -507,6 +507,10 @@ check_package_management (guestfs_h *g, struct inspect_fs *fs) /* Get the first line of a small file, without any trailing newline * character. + * + * NOTE: If the file is completely empty or begins with a '\n' + * character, this returns an empty string (not NULL). The caller + * will usually need to check for this case. */ char * guestfs___first_line_of_file (guestfs_h *g, const char *filename) @@ -532,9 +536,9 @@ guestfs___first_line_of_file (guestfs_h *g, const char *filename) if (lines == NULL) return NULL; if (lines[0] == NULL) { - error (g, _("%s: file is empty"), filename); guestfs___free_string_list (lines); - return NULL; + /* Empty file: Return an empty string as explained above. */ + return safe_strdup (g, ""); } /* lines[1] should be NULL because of '1' argument above ... */ diff --git a/src/inspect_fs_unix.c b/src/inspect_fs_unix.c index 2caf4002..06936c23 100644 --- a/src/inspect_fs_unix.c +++ b/src/inspect_fs_unix.c @@ -181,6 +181,12 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs, fs->product_name = guestfs___first_line_of_file (g, release_filename); if (fs->product_name == NULL) return -1; + if (STREQ (fs->product_name, "")) { + free (fs->product_name); + fs->product_name = NULL; + error (g, _("release file %s is empty or malformed"), release_filename); + return -1; + } return 0; } @@ -659,13 +665,23 @@ check_hostname_unix (guestfs_h *g, struct inspect_fs *fs) fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME"); if (fs->hostname == NULL) return -1; + if (STREQ (fs->hostname, "")) { + free (fs->hostname); + fs->hostname = NULL; + } } - else if (guestfs_is_file (g, "/etc/hostname")) { + + if (!fs->hostname && guestfs_is_file (g, "/etc/hostname")) { fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname"); if (fs->hostname == NULL) return -1; + if (STREQ (fs->hostname, "")) { + free (fs->hostname); + fs->hostname = NULL; + } } - else if (guestfs_is_file (g, "/etc/sysconfig/network")) { + + if (!fs->hostname && guestfs_is_file (g, "/etc/sysconfig/network")) { const char *configfiles[] = { "/etc/sysconfig/network", NULL }; if (inspect_with_augeas (g, fs, configfiles, check_hostname_redhat) == -1) |