Fix: memory leak in lttng_enable_event_with_exclusions
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 9 Jul 2014 22:20:03 +0000 (18:20 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Thu, 10 Jul 2014 14:54:16 +0000 (10:54 -0400)
lttng_enable_event_with_exclusions leaks a filter expression when
automatically generated filter statements are used. This happens when
loglevel and logger name filtering are used when enabling JUL events.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
src/lib/lttng-ctl/lttng-ctl.c

index 6363d56b30ad37e8b46b795e02404bdd6120fc41..29f8a10d4d65f7d2295a289793789e520f79ed08 100644 (file)
@@ -753,7 +753,7 @@ end:
  */
 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
                struct lttng_event *ev, const char *channel_name,
-               const char *filter_expression,
+               const char *original_filter_expression,
                int exclusion_count, char **exclusion_list)
 {
        struct lttcomm_session_msg lsm;
@@ -761,6 +761,13 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
        int ret = 0;
        struct filter_parser_ctx *ctx = NULL;
        FILE *fmem = NULL;
+       /*
+        * Cast as non-const since we may replace the filter expression
+        * by a dynamically allocated string. Otherwise, the original
+        * string is not modified.
+        */
+       char *filter_expression = (char *) original_filter_expression;
+       int free_filter_expression = 0;
 
        if (handle == NULL || ev == NULL) {
                return -LTTNG_ERR_INVALID;
@@ -821,21 +828,16 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
 
                        /* Setup JUL filter if needed. */
                        jul_filter = set_jul_filter(filter_expression, ev);
-                       if (!jul_filter) {
-                               if (!filter_expression) {
-                                       /* No JUL and no filter, just skip everything below. */
-                                       goto ask_sessiond;
-                               }
+                       if (!jul_filter && !filter_expression) {
+                               /* No JUL and no filter, just skip everything below. */
+                               goto ask_sessiond;
                        } else {
                                /*
                                 * With a JUL filter, the original filter has been added to it
                                 * thus replace the filter expression.
                                 */
-                               filter_expression = strdup(jul_filter);
-                               free(jul_filter);
-                               if (!filter_expression) {
-                                       return -LTTNG_ERR_FILTER_NOMEM;
-                               }
+                               filter_expression = jul_filter;
+                               free_filter_expression = 1;
                        }
                }
 
@@ -956,6 +958,14 @@ varlen_alloc_error:
                if (fclose(fmem) != 0) {
                        perror("fclose");
                }
+               if (free_filter_expression) {
+                       /*
+                        * The filter expression has been replaced and must be
+                        * freed as it is not the original filter expression
+                        * received as a parameter.
+                        */
+                       free(filter_expression);
+               }
        }
        return ret;
 
This page took 0.026916 seconds and 4 git commands to generate.