Check errors aggressively 02/214902/4
authorMichal Bloch <m.bloch@samsung.com>
Thu, 3 Oct 2019 12:10:10 +0000 (14:10 +0200)
committerMichal Bloch <m.bloch@partner.samsung.com>
Mon, 21 Oct 2019 06:15:06 +0000 (06:15 +0000)
Change-Id: Id6c53df33cd3cbde48318c78ad492438b09f768a
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/action.c
src/config.c
src/raw_data_providers/proc_tsm.c
src/utils.c

index e3f0f80..677a7d9 100644 (file)
@@ -51,14 +51,22 @@ static void action_finish(struct action_data *ad)
     char interface[PATH_MAX];
     char format[20];
     GVariantBuilder *builder;
-    GVariant *signal_params;
+    GVariant *signal_params = NULL;
 
-    snprintf(format, 20, "(is@%s@%s@a{sv})", g_variant_get_type_string(ad->actual_value),
-                                             g_variant_get_type_string(ad->allowed_value));
-
-    snprintf(objpath, PATH_MAX, "%s/%s", TSM_DBUS_PATH, ad->ds->name);
-    snprintf(interface, PATH_MAX, "%s.%s", TSM_DBUS_INTERFACE, ad->ds->param_name);
+    if (snprintf(format, 20, "(is@%s@%s@a{sv})", g_variant_get_type_string(ad->actual_value),
+                                             g_variant_get_type_string(ad->allowed_value)) == -1) {
+        _E("Couldn't print dbus variant format: %m");
+        goto skip_dbus_signal;
+    }
 
+    if (snprintf(objpath, PATH_MAX, TSM_DBUS_PATH "/%s", ad->ds->name) == -1) {
+        _E("Couldn't print dbus object path: %m");
+        goto skip_dbus_signal;
+    }
+    if (snprintf(interface, PATH_MAX, TSM_DBUS_INTERFACE ".%s", ad->ds->param_name) == -1) {
+        _E("Couldn't print dbus interface: %m");
+        goto skip_dbus_signal;
+    }
 
     builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
     g_variant_builder_add(builder, "{sv}",
@@ -87,6 +95,8 @@ static void action_finish(struct action_data *ad)
 
     g_variant_builder_unref(builder);
 
+skip_dbus_signal:
+
     /* Kill the process */
     if (ad->ds->process->kill) {
         _D_PROC(ad->ds->process, "Killing process");
@@ -97,11 +107,15 @@ static void action_finish(struct action_data *ad)
     }
 
     /* Send signal */
-    dbus_send_signal(objpath, interface, "AbnormalityDetected", signal_params);
+    if (signal_params) {
+        dbus_send_signal(objpath, interface, "AbnormalityDetected", signal_params);
 
-    _D_PROC(ad->ds->process, "Sent D-Bus signal (%s, %s)",
-                             limit_type_to_string(ad->lt),
-                             ad->ds->param_name);
+        _D_PROC(ad->ds->process, "Sent D-Bus signal (%s, %s)",
+                                 limit_type_to_string(ad->lt),
+                                 ad->ds->param_name);
+    } else {
+        _E("Skipping D-Bus signal due to errors");
+    }
 
     ad->ds->action_in_progress = 0;
 
@@ -159,12 +173,9 @@ static void crash_manager_exit_cb(GPid pid, gint status, gpointer user_data)
     _E("Crash-worker ended without error but didn't provide report path");
 
 finish:
-    action_finish(ad);
+    action_finish(ad); // NB: also closes `stream` through `ad->stdout_fd`
 
     free(line);
-    if (stream)
-        fclose(stream);
-
     g_spawn_close_pid(pid);
 }
 
index 9f96041..93ca0d0 100644 (file)
@@ -99,9 +99,9 @@ int config_load_dir(const char *config_dir)
     }
 
     for (int i = 0; i < n; i++) {
-        snprintf(full_path, PATH_MAX, "%s/%s", config_dir, filelist[i]->d_name);
-
-        if (is_regular_file(full_path))
+        if (snprintf(full_path, PATH_MAX, "%s/%s", config_dir, filelist[i]->d_name) == -1)
+            _W("Unable to format dir path: %m");
+        else if (is_regular_file(full_path))
             config_load_merge(full_path);
 
         free(filelist[i]);
index 3c18cce..7898522 100644 (file)
@@ -77,11 +77,13 @@ static int get_raw_data(struct list_head *rd)
         }
     }
 
-    fclose(fp);
+    if (fclose(fp) == -1)
+        _W("Unable to close file " PROC_TSM_PATH ": %m");
     return 0;
 
 error:
-    fclose(fp);
+    if (fclose(fp) == -1)
+        _W("Unable to close file " PROC_TSM_PATH ": %m");
     raw_data_cleanup(rd);
     return ret;
 }
index ca687b8..44c49bc 100644 (file)
@@ -31,7 +31,10 @@ unsigned long long time_now(void)
 {
     struct timespec ts;
 
-    clock_gettime(CLOCK_BOOTTIME, &ts);
+    if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
+        _W("Couldn't get current clock time: %m");
+        return 0;
+    }
     return ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; // us
 }
 
@@ -81,13 +84,24 @@ int pid_to_basename(int pid, char *basename, size_t basename_buflen)
 const char *smpl2str(struct sample_s *s)
 {
     static char str[MAX_SAMPLE_STR];
+    int r;
 
     if (s->type == ST_DOUBLE)
-        snprintf(str, MAX_SAMPLE_STR, "%2.3lf", s->value.d);
+        r = snprintf(str, MAX_SAMPLE_STR, "%2.3lf", s->value.d);
     else if (s->type == ST_INT)
-        snprintf(str, MAX_SAMPLE_STR, "%d", s->value.i);
+        r = snprintf(str, MAX_SAMPLE_STR, "%d", s->value.i);
     else if (s->type == ST_ULL)
-        snprintf(str, MAX_SAMPLE_STR, "%llu", s->value.ull);
+        r = snprintf(str, MAX_SAMPLE_STR, "%llu", s->value.ull);
+    else {
+        r = -1;
+        errno = EINVAL;
+    }
+
+    if (r == -1) {
+      _W("Couldn't parse sample: %m");
+      str[0] = '?';
+      str[1] = '\0';
+    }
 
     return str;
 }