From a7a29f110d51c2cd9c9a82d8449b94257b46269a Mon Sep 17 00:00:00 2001 From: z3phyr Date: Sat, 27 Apr 2024 05:31:41 -0400 Subject: [PATCH] Revert memory leak fix for relocs (#4463) --- librz/bin/bin.c | 9 +++++++-- librz/bin/format/mdmp/mdmp_pe.c | 2 +- librz/bin/p/bin_mach0.c | 6 ++---- librz/bin/p/bin_pe.inc | 2 +- test/db/formats/pe/imports_tinyW7 | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 2e19540ff37..fcd44957574 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -203,8 +203,13 @@ RZ_API void rz_bin_reloc_free(RZ_NULLABLE RzBinReloc *reloc) { if (!reloc) { return; } - rz_bin_import_free(reloc->import); - rz_bin_symbol_free(reloc->symbol); + /** + * TODO: leak in bin_elf, but it will cause double free in bin_pe if free here, + * Because in the bin_elf implementation RzBinObject->imports and RzBinObject->relocs->imports + * are two pieces of data, but they are linked to each other in bin_pe + */ + // rz_bin_import_free(reloc->import); + // rz_bin_symbol_free(reloc->symbol); free(reloc); } diff --git a/librz/bin/format/mdmp/mdmp_pe.c b/librz/bin/format/mdmp/mdmp_pe.c index 9450cb3ca9a..8fcb7bee276 100644 --- a/librz/bin/format/mdmp/mdmp_pe.c +++ b/librz/bin/format/mdmp/mdmp_pe.c @@ -134,7 +134,7 @@ RzPVector /**/ *PE_(rz_bin_mdmp_pe_get_imports)(struct PE_(rz_bin offset -= pe_bin->vaddr; } rel->additive = 0; - rel->import = rz_bin_import_clone(ptr); + rel->import = ptr; rel->addend = 0; rel->vaddr = offset + pe_bin->vaddr; rel->paddr = imports[i].paddr + pe_bin->paddr; diff --git a/librz/bin/p/bin_mach0.c b/librz/bin/p/bin_mach0.c index d3f0ec69e9c..a8d4a3b5d6a 100644 --- a/librz/bin/p/bin_mach0.c +++ b/librz/bin/p/bin_mach0.c @@ -408,11 +408,9 @@ static RzPVector /**/ *relocs(RzBinFile *bf) { free(ptr); break; } - ptr->import = rz_bin_import_clone(imp); + ptr->import = imp; } else if (reloc->ord >= 0 && reloc->ord < rz_pvector_len(&bin->imports_by_ord)) { - RzBinImport *imp = NULL; - imp = rz_pvector_at(&bin->imports_by_ord, reloc->ord); - ptr->import = rz_bin_import_clone(imp); + ptr->import = rz_pvector_at(&bin->imports_by_ord, reloc->ord); } ptr->addend = reloc->addend; ptr->vaddr = reloc->addr; diff --git a/librz/bin/p/bin_pe.inc b/librz/bin/p/bin_pe.inc index 84bb526e591..bbc00c69a8c 100644 --- a/librz/bin/p/bin_pe.inc +++ b/librz/bin/p/bin_pe.inc @@ -474,7 +474,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { rel->type = RZ_BIN_RELOC_32; #endif rel->additive = 0; - rel->import = rz_bin_import_clone(ptr); + rel->import = ptr; rel->addend = 0; { ut8 addr[4]; diff --git a/test/db/formats/pe/imports_tinyW7 b/test/db/formats/pe/imports_tinyW7 index b18932fb4a9..c4a12af454d 100644 --- a/test/db/formats/pe/imports_tinyW7 +++ b/test/db/formats/pe/imports_tinyW7 @@ -18,7 +18,7 @@ vaddr paddr type name 0x800004f4 0x00000234 SET_32 msvcrt_Ordinal_1268 nth vaddr bind type lib name ------------------------------------------------ -284 0x00401048 NONE FUNC kernel32 Ordinal_284 +284 ---------- NONE FUNC kernel32 FindAtomW 1268 0x00401034 NONE FUNC msvcrt Ordinal_1268 EOF RUN