]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
g-i: Calculate size correctly, avoid use-after-free
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 5 Nov 2019 14:33:43 +0000 (15:33 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 7 Nov 2019 11:11:49 +0000 (12:11 +0100)
gobject-introspection/gidlmodule.c

index d17a249a055bec18b5afbbdc540b01ef54955fef..7eb1fe0ab9081104b56a3aa9b4b94035e54cb945 100644 (file)
@@ -64,7 +64,7 @@ GMetadata *
 g_idl_module_build_metadata (GIdlModule  *module,
                             GList       *modules)
 {
-  guchar *metadata;
+  GMetadata *metadata;
   gsize length;
   gint i;
   GList *e;
@@ -102,6 +102,11 @@ g_idl_module_build_metadata (GIdlModule  *module,
       size += g_idl_node_get_full_size (node);
     }
 
+  /* Adjust size for strings allocated in header below specially */
+  size += strlen (module->name);
+  if (module->shared_library)
+    size += strlen (module->shared_library);
+
   g_message ("allocating %d bytes (%d header, %d directory, %d entries)\n", 
          size, header_size, dir_size, size - header_size - dir_size);
 
@@ -201,15 +206,19 @@ g_idl_module_build_metadata (GIdlModule  *module,
     }
 
   dump_stats ();
-  g_hash_table_destroy (strings);
-  g_hash_table_destroy (types);
 
   header->annotations = offset2;
   
   g_message ("reallocating to %d bytes", offset2);
 
-  metadata = g_realloc (data, offset2);
+  data = g_realloc (data, offset2);
+  header = (Header*) data;
   length = header->size = offset2;
-  return g_metadata_new_from_memory (metadata, length);
+  metadata = g_metadata_new_from_memory (data, length);
+
+  g_hash_table_destroy (strings);
+  g_hash_table_destroy (types);
+
+  return metadata;
 }