Fix multiple memory leaks (#5636)

This commit is contained in:
Peiwei Hu 2025-12-18 22:23:48 +08:00 committed by GitHub
parent b07369ab12
commit 595be1780c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 28 additions and 6 deletions

View file

@ -1346,6 +1346,7 @@ RZ_API RzAsmCode *rz_asm_rasm_assemble(RzAsm *a, const char *buf, bool use_spp)
spp_eval(lbuf, &out);
free(lbuf);
lbuf = rz_str_dup(rz_strbuf_get(out.cout));
rz_strbuf_free(out.cout);
}
acode = rz_asm_massemble(a, lbuf);
free(lbuf);

View file

@ -1643,6 +1643,7 @@ static bool function_from_die(
fcn->prefer_name, fcn->low_pc, die->offset);
if (!ht_up_update(ctx->analysis->debug_info->callable_by_offset, die->offset, callable)) {
RZ_LOG_ERROR("DWARF callable saving failed [0x%" PFMT64x "]\n", die->offset);
rz_type_callable_free(callable);
goto cleanup;
}
if (!ht_up_update(ctx->analysis->debug_info->function_by_offset, die->offset, fcn)) {

View file

@ -25,7 +25,7 @@ RZ_API RZ_OWN RzPlatformTargetIndex *rz_platform_target_index_new() {
if (!target) {
return NULL;
}
target->platforms = ht_up_new(NULL, NULL);
target->platforms = ht_up_new(NULL, (HtUPFreeValue)rz_platform_item_free);
if (!target->platforms) {
free(target);
return NULL;

View file

@ -1826,7 +1826,9 @@ RZ_API void rz_analysis_fcn_vars_add_types(RzAnalysis *analysis, RZ_NONNULL RzAn
}
}
}
rz_type_func_save(analysis->typedb, callable);
if (!rz_type_func_save(analysis->typedb, callable)) {
rz_type_callable_free(callable);
}
rz_analysis_fcn_vars_cache_fini(&cache);
}

View file

@ -132,11 +132,15 @@ static RzPVector /*<RzBinString *>*/ *string_wildcard_search(
char *wildcard = rz_regex_create_wildcard_pattern(opt->min_length, 0);
if (!wildcard) {
RZ_LOG_ERROR("bin_file_strings: Failed to create wildcard pattern!\n");
rz_search_collection_free(collection);
rz_search_opt_free(search_opts);
return NULL;
}
if (!rz_search_collection_string_add(collection, wildcard, RZ_REGEX_EXTENDED, match_alignment, opt->string_encoding)) {
RZ_LOG_ERROR("bin_file_strings: Failed to add wildcard pattern!\n");
free(wildcard);
rz_search_collection_free(collection);
rz_search_opt_free(search_opts);
return NULL;
}
free(wildcard);

View file

@ -98,6 +98,7 @@ static int reloc_target_cmp(const void *a, const void *b, void *user) {
RZ_API RzBinRelocStorage *rz_bin_reloc_storage_new(RZ_OWN RzPVector /*<RzBinReloc *>*/ *relocs) {
RzBinRelocStorage *ret = RZ_NEW0(RzBinRelocStorage);
if (!ret) {
rz_pvector_free(relocs);
return NULL;
}
RzPVector sorter;

View file

@ -5184,6 +5184,7 @@ static RzCmdStatus print_visual_bytes(RzCore *core, RZ_NONNULL const unsigned ch
hist->h = h;
RzStrBuf *str = rz_histogram_interactive_horizontal(hist, data);
rz_cons_canvas_write(hist->can, str->ptr);
rz_strbuf_free(str);
rz_cons_canvas_print_region(hist->can);
rz_cons_newline();
rz_cons_visual_flush();

View file

@ -1930,6 +1930,7 @@ static RzCmdStatus byte_pattern_search(RzCore *core, RZ_OWN RzSearchBytesPattern
return cmd_core_handle_search_hits(core, state, hits);
error:
rz_search_bytes_pattern_free(pattern);
rz_list_free(hits);
rz_search_opt_free(search_opts);
CMD_SEARCH_END();

View file

@ -116,7 +116,10 @@ RZ_API bool rz_search_opt_set_cancel_cb(RZ_NONNULL RzSearchOpt *opt, RzSearchCan
}
RZ_API bool rz_search_opt_set_find_options(RZ_NONNULL RzSearchOpt *opt, RZ_OWN RzSearchFindOpt *find_opts) {
rz_return_val_if_fail(opt, false);
if (!opt) {
rz_search_find_opt_free(find_opts);
rz_return_val_if_reached(false);
}
rz_search_find_opt_free(opt->find_opts);
opt->find_opts = find_opts;
return true;

View file

@ -29,7 +29,8 @@ RZ_API int rz_search_regexp_update(RzSearch *s, ut64 from, const ut8 *buf, int l
compiled = rz_regex_new((char *)kw->bin_keyword, cflags, 0, ccontext);
if (!compiled) {
eprintf("Cannot compile '%s' regexp\n", kw->bin_keyword);
return -1;
ret = -1;
goto beach;
}
matches = rz_regex_match_all_not_grouped(compiled, (const char *)buf, len, 0, RZ_REGEX_DEFAULT);

View file

@ -957,6 +957,7 @@ RZ_IPI int rz_search_hit_cmp(RZ_NULLABLE RzSearchHit *a, RZ_NULLABLE RzSearchHit
RZ_IPI RZ_OWN RzSearchHit *rz_search_hit_new(RZ_NULLABLE const char *hit_desc, ut64 address, size_t size, RZ_NULLABLE RZ_OWN RzSearchHitDetail *hit_detail) {
RzSearchHit *hit = RZ_NEW0(RzSearchHit);
if (!hit) {
rz_search_hit_detail_free(hit_detail);
return NULL;
}
hit->hit_desc = rz_str_dup(hit_desc);

View file

@ -442,7 +442,7 @@ static RzDetectedString *setup_str_regex(const char *re_pattern, RzRegexFlags cf
if (!ds) {
RZ_LOG_ERROR("Failed allocate memory for RzDetectedString\n");
free(re_pattern_clone);
rz_regex_free(re);
rz_regex_free_multi(re);
return NULL;
}
ds->string = re_pattern_clone;

View file

@ -191,7 +191,11 @@ int test_rz_str_search_io_simple(void) {
printf("Hit at 0x%" PFMT64x " size: %" PFMTSZd "\n", hit->address, hit->size);
mu_assert_true(hit->size == 22, "Incorrect size");
mu_assert_eq(hit->address, 0x004005ea, "Incorrect address");
rz_list_free(hits);
rz_list_free(boundaries);
rz_search_collection_free(collection);
rz_search_opt_free(search_opts);
rz_core_free(core);
mu_end;
}

View file

@ -17,6 +17,7 @@
mu_assert_notnull(node, "node is not null (" #name ")"); \
mu_assert_eq(rz_list_length(node->child_list), n_childs, "node contains one child (" #name ")"); \
rz_sign_flirt_node_free(node); \
rz_buf_free(buffer); \
mu_end; \
}

View file

@ -136,6 +136,7 @@ bool test_rz_direct_solver() {
mu_assert_notnull(rop_semantics, "ROP semantics hashtable is NULL");
mu_assert_eq(rop_semantics->count, 2, "ROP semantics hashtable count is not 2");
ht_up_foreach(rop_semantics, rop_gadget_info_cb, ht_rop_analysis);
rz_core_rop_search_context_free(context);
cleanup_test(core, ht_rop_analysis);
mu_end;
}