[Core/Handler] Replace realloc() with malloc()
authorWook Song <wook16.song@samsung.com>
Mon, 10 Aug 2020 05:27:54 +0000 (14:27 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Mon, 10 Aug 2020 09:46:11 +0000 (18:46 +0900)
After realloc (), the old pointer used in ralloc () could point invalid
address so that we cannot use meta->magiccode. This patch simply
replaces realloc () with malloc () to avoid such issue.

Signed-off-by: Wook Song <wook16.song@samsung.com>
src/core/ne-handler.cc

index 74332b0..11e2f3b 100644 (file)
@@ -396,14 +396,17 @@ npubin_meta * getNPUmodel_metadata (const char *model, bool need_extra)
   if (need_extra && NPUBIN_META_EXTRA (meta->magiccode) > 0) {
     npubin_meta *new_meta;
 
-    new_meta = (npubin_meta *) realloc (meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
+    new_meta = (npubin_meta *) malloc (NPUBIN_META_TOTAL_SIZE(meta->magiccode));
     if (!new_meta) {
       logerr (TAG, "Failed to allocate extra metadata\n");
       goto exit_free;
     }
 
-    ret = fread (new_meta->reserved_extra, 1, NPUBIN_META_EXTRA_SIZE (meta->magiccode), fp);
-    if (ret != NPUBIN_META_EXTRA_SIZE (meta->magiccode)) {
+    memcpy (new_meta, meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
+    free (meta);
+    meta = nullptr;
+    ret = fread (new_meta->reserved_extra, 1, NPUBIN_META_EXTRA_SIZE (new_meta->magiccode), fp);
+    if (ret != NPUBIN_META_EXTRA_SIZE (new_meta->magiccode)) {
       logerr (TAG, "Invalid extra metadata provided\n");
       free (new_meta);
       goto exit_err;