REST/NAMESTORE: fix memleak
authorSchanzenbach, Martin <mschanzenbach@posteo.de>
Tue, 30 Apr 2019 15:40:36 +0000 (17:40 +0200)
committerSchanzenbach, Martin <mschanzenbach@posteo.de>
Tue, 30 Apr 2019 15:40:36 +0000 (17:40 +0200)
src/json/json_gnsrecord.c
src/namestore/plugin_rest_namestore.c

index eef9e0e79afd3e5fd2938bc14cd65fd089963326..2ba4da591a5c1585b947f920e12918173195f814 100644 (file)
@@ -48,24 +48,19 @@ struct GnsRecordInfo
 static void
 cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info)
 {
-  if (NULL != gnsrecord_info)
+  if (NULL != *(gnsrecord_info->rd))
   {
-    if (NULL != *(gnsrecord_info->rd))
+    for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
     {
-      for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
-      {
-        if (NULL != (*(gnsrecord_info->rd))[i].data)
-          GNUNET_free ((char *) (*(gnsrecord_info->rd))[i].data);
-      }
-      GNUNET_free (*(gnsrecord_info->rd));
-      *(gnsrecord_info->rd) = NULL;
+      if (NULL != (*(gnsrecord_info->rd))[i].data)
+        GNUNET_free ((char *) (*(gnsrecord_info->rd))[i].data);
     }
-    if (NULL != *(gnsrecord_info->name))
-      GNUNET_free (*(gnsrecord_info->name));
-    *(gnsrecord_info->name) = NULL;
-    GNUNET_free (gnsrecord_info);
+    GNUNET_free (*(gnsrecord_info->rd));
+    *(gnsrecord_info->rd) = NULL;
   }
-
+  if (NULL != *(gnsrecord_info->name))
+    GNUNET_free (*(gnsrecord_info->name));
+  *(gnsrecord_info->name) = NULL;
 }
 
 
@@ -210,7 +205,12 @@ parse_gnsrecordobject (void *cls,
   }
   gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
   *(gnsrecord_info->name) = GNUNET_strdup (name);
-  return parse_record_data (gnsrecord_info, data);
+  if (GNUNET_OK != parse_record_data (gnsrecord_info, data))
+  {
+    cleanup_recordinfo (gnsrecord_info);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
 }
 
 
@@ -224,7 +224,7 @@ static void
 clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
 {
   struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
-  cleanup_recordinfo (gnsrecord_info);
+  GNUNET_free (gnsrecord_info);
 }
 
 
@@ -244,12 +244,12 @@ GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd,
   gnsrecord_info->name = name;
   gnsrecord_info->rd_count = rd_count;
   struct GNUNET_JSON_Specification ret = {.parser = &parse_gnsrecordobject,
-                                          .cleaner = &clean_gnsrecordobject,
-                                          .cls = NULL,
-                                          .field = NULL,
-                                          .ptr = (struct GnsRecordInfo *)
-                                            gnsrecord_info,
-                                          .ptr_size = 0,
-                                          .size_ptr = NULL};
+    .cleaner = &clean_gnsrecordobject,
+    .cls = NULL,
+    .field = NULL,
+    .ptr = (struct GnsRecordInfo *)
+      gnsrecord_info,
+    .ptr_size = 0,
+    .size_ptr = NULL};
   return ret;
 }
index 39b21c233ab79dba7c1032afa2ce4dabc171872f..1d8c180fbd13e4160584fe062d8d2c6f483cfcf4 100644 (file)
@@ -628,6 +628,7 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
     json_decref (data_js);
     return;
   }
+  GNUNET_JSON_parse_free (gnsspec);
   if (0 >= strlen (handle->record_name))
   {
     handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA);