Fix: miscellaneous memory handling fixes
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
index d1fa0d09cb2b04d75b3419cd6fa969a602253eb0..c32bf2b6847e147bcfcfe31041c7cfb300ef2edf 100644 (file)
@@ -3144,19 +3144,25 @@ int ust_app_list_events(struct lttng_event **events)
                        health_code_update();
                        if (count >= nbmem) {
                                /* In case the realloc fails, we free the memory */
-                               void *ptr;
-
-                               DBG2("Reallocating event list from %zu to %zu entries", nbmem,
-                                               2 * nbmem);
-                               nbmem *= 2;
-                               ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event));
-                               if (ptr == NULL) {
+                               struct lttng_event *new_tmp_event;
+                               size_t new_nbmem;
+
+                               new_nbmem = nbmem << 1;
+                               DBG2("Reallocating event list from %zu to %zu entries",
+                                               nbmem, new_nbmem);
+                               new_tmp_event = realloc(tmp_event,
+                                       new_nbmem * sizeof(struct lttng_event));
+                               if (new_tmp_event == NULL) {
                                        PERROR("realloc ust app events");
                                        free(tmp_event);
                                        ret = -ENOMEM;
                                        goto rcu_error;
                                }
-                               tmp_event = ptr;
+                               /* Zero the new memory */
+                               memset(new_tmp_event + nbmem, 0,
+                                       (new_nbmem - nbmem) * sizeof(struct lttng_event));
+                               nbmem = new_nbmem;
+                               tmp_event = new_tmp_event;
                        }
                        memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
                        tmp_event[count].loglevel = uiter.loglevel;
@@ -3244,19 +3250,25 @@ int ust_app_list_event_fields(struct lttng_event_field **fields)
                        health_code_update();
                        if (count >= nbmem) {
                                /* In case the realloc fails, we free the memory */
-                               void *ptr;
-
-                               DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
-                                               2 * nbmem);
-                               nbmem *= 2;
-                               ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field));
-                               if (ptr == NULL) {
+                               struct lttng_event_field *new_tmp_event;
+                               size_t new_nbmem;
+
+                               new_nbmem = nbmem << 1;
+                               DBG2("Reallocating event field list from %zu to %zu entries",
+                                               nbmem, new_nbmem);
+                               new_tmp_event = realloc(tmp_event,
+                                       new_nbmem * sizeof(struct lttng_event_field));
+                               if (new_tmp_event == NULL) {
                                        PERROR("realloc ust app event fields");
                                        free(tmp_event);
                                        ret = -ENOMEM;
                                        goto rcu_error;
                                }
-                               tmp_event = ptr;
+                               /* Zero the new memory */
+                               memset(new_tmp_event + nbmem, 0,
+                                       (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
+                               nbmem = new_nbmem;
+                               tmp_event = new_tmp_event;
                        }
 
                        memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
@@ -4932,6 +4944,7 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
                struct snapshot_output *output, int wait, unsigned int nb_streams)
 {
        int ret = 0;
+       unsigned int snapshot_done = 0;
        struct lttng_ht_iter iter;
        struct ust_app *app;
        char pathname[PATH_MAX];
@@ -5006,6 +5019,7 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
                        if (ret < 0) {
                                goto error;
                        }
+                       snapshot_done = 1;
                }
                break;
        }
@@ -5073,6 +5087,7 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
                        if (ret < 0) {
                                goto error;
                        }
+                       snapshot_done = 1;
                }
                break;
        }
@@ -5081,6 +5096,15 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
                break;
        }
 
+       if (!snapshot_done) {
+               /*
+                * If no snapshot was made and we are not in the error path, this means
+                * that there are no buffers thus no (prior) application to snapshot
+                * data from so we have simply NO data.
+                */
+               ret = -ENODATA;
+       }
+
 error:
        rcu_read_unlock();
        return ret;
This page took 0.026697 seconds and 5 git commands to generate.