Improve error handling 16/153516/5
authorMichal Bloch <m.bloch@samsung.com>
Thu, 28 Sep 2017 14:38:52 +0000 (16:38 +0200)
committerKunhoon Baik <knhoon.baik@samsung.com>
Fri, 3 Nov 2017 07:51:01 +0000 (07:51 +0000)
 * handle reallocation failures in scan dump.
 * generate a log if unlinking a failed dump file also fails
 * fix a memory leak when passing duplicate `--save-core` parameter to crash-pipe; generate a log in that case
 * explicitly free a resource on exit

Change-Id: I71b22b3375c6c9ab9a43dab41e80eadc3fa599f6
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/crash-manager/crash-manager.c
src/crash-pipe/crash-pipe.c

index 4a49b7e..f25c486 100644 (file)
@@ -600,9 +600,16 @@ static int scan_dump(struct file_info **dump_list, size_t *usage)
                goto exit;
        }
 
-       if (dump_num != scan_num)
-               temp_list = (struct file_info *)realloc(temp_list,
+       if (dump_num != scan_num) {
+               struct file_info *const tmp = (struct file_info *)realloc(temp_list,
                                dump_num * sizeof(struct file_info));
+               if (!tmp) {
+                       free(temp_list);
+                       dump_num = -1;
+                       goto exit;
+               }
+               temp_list = tmp;
+       }
 
        qsort(temp_list, dump_num, sizeof(struct file_info), mtime_cmp);
 
index 4d05ce2..d04037b 100644 (file)
@@ -109,7 +109,11 @@ static int save_core(const char *core_path)
        }
        if (r < 0) {
                syslog(LOG_ERR, "crash-pipe: Error while saving core file %s: %m. Removing core.\n", core_path);
-               (void)unlink(core_path); // XXX check errors here too
+               if (unlink(core_path) < 0) {
+                       // ignore errno because we're already dealing with the one from splice
+                       // amp the log level to CRIT because things are starting to look pretty grim
+                       syslog(LOG_CRIT, "crash-pipe: Error while removing core file %s: %m.\n", core_path);
+               }
        }
 
        close(fd);
@@ -132,6 +136,10 @@ int main(int argc, char *argv[])
                        usage();
                        exit(EXIT_SUCCESS);
                } else if (c == OPT_SAVE_CORE) {
+                       if (opt_save_core) {
+                               syslog(LOG_WARNING, "crash-pipe: duplicate --save-core passed\n");
+                               free(opt_save_core);
+                       }
                        opt_save_core = strdup(optarg);
                        if (!opt_save_core) {
                                syslog(LOG_CRIT, "Out of memory. Exiting.");
@@ -143,8 +151,10 @@ int main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       if (opt_save_core)
+       if (opt_save_core) {
                ret = save_core(opt_save_core);
+               free(opt_save_core);
+       }
 
        return ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }