Code

src/utils_db_query.c: Fix use-after-free.
authorFlorian Forster <octo@collectd.org>
Wed, 17 Jun 2015 15:28:39 +0000 (17:28 +0200)
committerFlorian Forster <octo@collectd.org>
Wed, 17 Jun 2015 15:28:39 +0000 (17:28 +0200)
"r_area->next" was evaluated after "r_area" was freed.

src/utils_db_query.c

index aadf9c5ec1d7f6a4879a5701ebe19f2a25d23633..ab4299b3a84675e8be8fdc2adfdf7ca879d807fe 100644 (file)
@@ -976,10 +976,9 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
   udb_result_preparation_area_t **next_r_area;
   udb_result_t *r;
 
-  q_area = (udb_query_preparation_area_t *)malloc (sizeof (*q_area));
+  q_area = malloc (sizeof (*q_area));
   if (q_area == NULL)
     return NULL;
-
   memset (q_area, 0, sizeof (*q_area));
 
   next_r_area = &q_area->result_prep_areas;
@@ -987,14 +986,18 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
   {
     udb_result_preparation_area_t *r_area;
 
-    r_area = (udb_result_preparation_area_t *)malloc (sizeof (*r_area));
+    r_area = malloc (sizeof (*r_area));
     if (r_area == NULL)
     {
-      for (r_area = q_area->result_prep_areas;
-          r_area != NULL; r_area = r_area->next)
+      udb_result_preparation_area_t *a = q_area->result_prep_areas;
+
+      while (a != NULL)
       {
-        free (r_area);
+        udb_result_preparation_area_t *next = a->next;
+        sfree (a);
+        a = next;
       }
+
       free (q_area);
       return NULL;
     }