coredumpctl: check return value
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 17 Jul 2019 08:02:13 +0000 (10:02 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 17 Jul 2019 09:35:04 +0000 (11:35 +0200)
retrieve() allocates memory, so it may fail.
Coverity CID#1402338.

src/coredump/coredumpctl.c

index 2b17dc5..292c186 100644 (file)
@@ -738,9 +738,22 @@ static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp)
 
         /* Look for a coredump on disk first. */
         r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
-        if (r == 0)
-                retrieve(data, len, "COREDUMP_FILENAME", &filename);
-        else {
+        if (r == 0) {
+                r = retrieve(data, len, "COREDUMP_FILENAME", &filename);
+                if (r < 0)
+                        return r;
+                assert(r > 0);
+
+                if (access(filename, R_OK) < 0)
+                        return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
+
+                if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
+                        *path = TAKE_PTR(filename);
+
+                        return 0;
+                }
+
+        } else {
                 if (r != -ENOENT)
                         return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m");
                 /* Check that we can have a COREDUMP field. We still haven't set a high
@@ -754,17 +767,6 @@ static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp)
                         return log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
         }
 
-        if (filename) {
-                if (access(filename, R_OK) < 0)
-                        return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
-
-                if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
-                        *path = TAKE_PTR(filename);
-
-                        return 0;
-                }
-        }
-
         if (path) {
                 const char *vt;