From 75080d85bf4fa68a044b5d38512755d1dbdfa4af Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 19 Oct 2024 06:01:31 -0500 Subject: [PATCH 01/11] Refactor: Use HtSP for storing plugins. This saves iterations when searching for a plugin of a given name. It also allows to save plugin configurations and data in hash table, all addressed by their name. Hence, usage is more streamlined. --- librz/arch/analysis.c | 13 +++-- librz/arch/asm.c | 49 +++++++++------- librz/bin/bin.c | 110 +++++++++++++++++------------------- librz/bp/bp.c | 4 +- librz/bp/bp_plugin.c | 38 +++++++------ librz/core/canalysis.c | 6 +- librz/core/casm.c | 13 +++-- librz/core/cautocmpl.c | 11 ++-- librz/core/cbin.c | 10 +++- librz/core/cconfig.c | 15 +++-- librz/core/ccrypto.c | 6 +- librz/core/cdebug.c | 5 +- librz/core/chash.c | 6 +- librz/core/cio.c | 5 +- librz/core/cmd/cmd_egg.c | 5 +- librz/core/core.c | 5 +- librz/core/cplugin.c | 15 ++--- librz/crypto/crypto.c | 25 ++++---- librz/debug/debug.c | 3 +- librz/debug/plugin.c | 22 +++----- librz/egg/egg.c | 18 +++--- librz/hash/hash.c | 40 ++++--------- librz/include/rz_analysis.h | 2 +- librz/include/rz_asm.h | 4 +- librz/include/rz_bin.h | 4 +- librz/include/rz_bp.h | 2 +- librz/include/rz_core.h | 2 +- librz/include/rz_crypto.h | 8 +-- librz/include/rz_debug.h | 2 +- librz/include/rz_egg.h | 2 +- librz/include/rz_hash.h | 4 +- librz/include/rz_io.h | 2 +- librz/include/rz_lib.h | 17 ------ librz/io/io.c | 2 +- librz/io/io_plugin.c | 18 +++--- librz/main/rz-gg.c | 10 +++- librz/main/rz-hash.c | 8 ++- librz/util/iterator.c | 2 +- 38 files changed, 256 insertions(+), 257 deletions(-) diff --git a/librz/arch/analysis.c b/librz/arch/analysis.c index 890d524fdc6..6f83e4f59a2 100644 --- a/librz/arch/analysis.c +++ b/librz/arch/analysis.c @@ -117,7 +117,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) { analysis->leaddrs = NULL; analysis->imports = rz_list_newf(free); rz_analysis_set_bits(analysis, 32); - analysis->plugins = rz_list_new(); + analysis->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); if (analysis->plugins) { const size_t n_plugins = rz_arch_get_n_plugins(); for (size_t i = 0; i < n_plugins; i++) { @@ -189,7 +189,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) { rz_str_constpool_fini(&a->constpool); ht_sp_free(a->ht_global_var); ht_up_free(a->ht_rop_semantics); - rz_list_free(a->plugins); + ht_sp_free(a->plugins); rz_analysis_debug_info_free(a->debug_info); free(a); return NULL; @@ -197,7 +197,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) { RZ_API bool rz_analysis_plugin_add(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPlugin *p) { rz_return_val_if_fail(analysis && p, false); - RZ_PLUGIN_CHECK_AND_ADD(analysis->plugins, p, RzAnalysisPlugin); + ht_sp_insert(analysis->plugins, p->name, p); return true; } @@ -207,18 +207,18 @@ RZ_API bool rz_analysis_plugin_del(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPl plugin_fini(analysis); analysis->cur = NULL; } - return rz_list_delete_data(analysis->plugins, p); + return ht_sp_delete(analysis->plugins, p->name); } RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { - RzListIter *it; + RzIterator *it = ht_sp_as_iter(analysis->plugins); RzAnalysisPlugin *h; if (analysis) { if (analysis->cur && !strcmp(analysis->cur->name, name)) { return true; } - rz_list_foreach (analysis->plugins, it, h) { + rz_iterator_foreach(it, h) { if (!h || !h->name || strcmp(h->name, name)) { continue; } @@ -234,6 +234,7 @@ RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { } return true; } + rz_iterator_free(it); } return false; } diff --git a/librz/arch/asm.c b/librz/arch/asm.c index 33d480491b8..8deb3944bcb 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -283,7 +283,7 @@ RZ_API RzAsm *rz_asm_new(void) { a->bits = RZ_SYS_BITS; a->bitshift = 0; a->syntax = RZ_ASM_SYNTAX_INTEL; - a->plugins = rz_list_new(); + a->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); if (!a->plugins) { free(a); return NULL; @@ -338,7 +338,7 @@ RZ_API void rz_asm_free(RzAsm *a) { } plugin_fini(a); if (a->plugins) { - rz_list_free(a->plugins); + ht_sp_free(a->plugins); a->plugins = NULL; } rz_syscall_free(a->syscall); @@ -358,7 +358,7 @@ RZ_API bool rz_asm_plugin_add(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { if (rz_asm_is_valid(a, p->name)) { return false; } - RZ_PLUGIN_CHECK_AND_ADD(a->plugins, p, RzAsmPlugin); + ht_sp_insert(a->plugins, p->name, p); return true; } @@ -371,37 +371,41 @@ RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { if (a->acur == p) { a->acur = NULL; } - return rz_list_delete_data(a->plugins, p); + return ht_sp_delete(a->plugins, p->name); } RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (!name || !*name) { return false; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!strcmp(h->name, name)) { return true; } } + rz_iterator_free(iter); return false; } RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; - if (a) { - if (name && *name) { - rz_list_foreach (a->plugins, iter, h) { - if (h->assemble && !strcmp(h->name, name)) { - a->acur = h; - return true; - } - } - } + RzIterator *iter = ht_sp_as_iter(a->plugins); + if (!a) { + return false; + } + if (!(name && *name)) { a->acur = NULL; } + rz_iterator_foreach(iter, h) { + if (h->assemble && !strcmp(h->name, name)) { + a->acur = h; + return true; + } + } + rz_iterator_free(iter); + a->acur = NULL; return false; } @@ -439,7 +443,7 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name */ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (!a || !name) { return false; } @@ -447,7 +451,7 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { if (a->cur && !strcmp(a->cur->arch, name)) { return true; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (h->arch && h->name && !strcmp(h->name, name)) { if (!a->cur || (a->cur && strcmp(a->cur->arch, h->arch))) { plugin_fini(a); @@ -476,6 +480,7 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { return true; } } + rz_iterator_free(iter); sdb_free(a->pair); a->pair = NULL; return false; @@ -628,11 +633,11 @@ static bool assemblerMatches(RzAsm *a, RzAsmPlugin *h) { static Ase findAssembler(RzAsm *a, const char *kw) { Ase ase = NULL; RzAsmPlugin *h; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); if (a->acur && a->acur->assemble) { return a->acur->assemble; } - rz_list_foreach (a->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (assemblerMatches(a, h)) { if (kw) { if (strstr(h->name, kw)) { @@ -643,6 +648,7 @@ static Ase findAssembler(RzAsm *a, const char *kw) { } } } + rz_iterator_free(iter); return ase; } @@ -1193,7 +1199,8 @@ RZ_API char *rz_asm_describe(RzAsm *a, const char *str) { return (a && a->pair) ? sdb_get(a->pair, str) : NULL; } -RZ_API RzList /**/ *rz_asm_get_plugins(RzAsm *a) { +RZ_API RZ_BORROW HtSP /**/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a) { + rz_return_val_if_fail(a, NULL); return a->plugins; } diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 174a2914a90..476bb547ff6 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -11,8 +11,9 @@ #include "i/private.h" // include both generated plugin lists. -#include "rz_bin_plugins.h" -#include "rz_bin_xtr_plugins.h" +#include +#include +#include RZ_LIB_VERSION(rz_bin); @@ -257,7 +258,7 @@ RZ_API RzBinFile *rz_bin_reload(RzBin *bin, RzBinFile *bf, ut64 baseaddr) { RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) { rz_return_val_if_fail(bin && opt, NULL); - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->binxtrs); RzBinXtrPlugin *xtr; bin->file = opt->filename; @@ -270,7 +271,7 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) // XXX - for the time being this is fine, but we may want to // change the name to something like // : - rz_list_foreach (bin->binxtrs, it, xtr) { + rz_iterator_foreach(it, xtr) { if (!xtr->check_buffer) { RZ_LOG_ERROR("Missing check_buffer callback for '%s'\n", xtr->name); continue; @@ -285,6 +286,7 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) } } } + rz_iterator_free(it); if (!bf) { // Uncomment for this speedup: 20s vs 22s // RzBuffer *buf = rz_buf_new_slurp (bin->file); @@ -352,73 +354,66 @@ RZ_API RzBinFile *rz_bin_open_io(RzBin *bin, RzBinOptions *opt) { } RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_name(RzBin *bin, const char *name) { - RzBinPlugin *plugin; - RzListIter *it; - rz_return_val_if_fail(bin && name, NULL); - rz_list_foreach (bin->plugins, it, plugin) { - if (!strcmp(plugin->name, name)) { - return plugin; - } + bool found = false; + RzBinPlugin *plugin = ht_sp_find(bin->plugins, name, &found); + if (found) { + return plugin; } return NULL; } RZ_API RzBinPlugin *rz_bin_get_binplugin_by_buffer(RzBin *bin, RzBuffer *buf) { RzBinPlugin *plugin; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->plugins); rz_return_val_if_fail(bin && buf, NULL); - rz_list_foreach (bin->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->check_buffer) { if (plugin->check_buffer(buf)) { return plugin; } } } + rz_iterator_free(it); return NULL; } RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_filename(RzBin *bin) { RzBinPlugin *plugin; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(bin->plugins); rz_return_val_if_fail(bin, NULL); const char *filename = strrchr(bin->file, RZ_SYS_DIR[0]); filename = filename ? filename + 1 : bin->file; - rz_list_foreach (bin->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->check_filename) { if (plugin->check_filename(filename)) { return plugin; } } } + rz_iterator_free(it); return NULL; } RZ_IPI RzBinXtrPlugin *rz_bin_get_xtrplugin_by_name(RzBin *bin, const char *name) { - RzBinXtrPlugin *xtr; - RzListIter *it; - rz_return_val_if_fail(bin && name, NULL); - // TODO: use a hashtable here - rz_list_foreach (bin->binxtrs, it, xtr) { - if (!strcmp(xtr->name, name)) { - return xtr; - } - // must be set to null - xtr = NULL; + bool found = false; + RzBinXtrPlugin *xtr = ht_sp_find(bin->binxtrs, name, &found); + if (found) { + return xtr; } return NULL; } RZ_API bool rz_bin_plugin_add(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bin->plugins, plugin, RzBinPlugin); + ht_sp_insert(bin->plugins, plugin->name, plugin); return true; } @@ -433,13 +428,13 @@ RZ_API bool rz_bin_plugin_del(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { rz_bin_file_delete(bin, bf); } } - return rz_list_delete_data(bin->plugins, plugin); + return ht_sp_delete(bin->plugins, plugin->name); } RZ_API bool rz_bin_xtr_plugin_add(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bin->binxtrs, plugin, RzBinXtrPlugin); + ht_sp_insert(bin->binxtrs, plugin->name, plugin); if (plugin->init) { plugin->init(bin->user); } @@ -464,7 +459,7 @@ RZ_API bool rz_bin_xtr_plugin_del(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) } } } - return rz_list_delete_data(bin->binxtrs, plugin); + return ht_sp_delete(bin->binxtrs, plugin->name); } RZ_API void rz_bin_free(RZ_NULLABLE RzBin *bin) { @@ -477,13 +472,14 @@ RZ_API void rz_bin_free(RZ_NULLABLE RzBin *bin) { // rz_bin_free_bin_files (bin); rz_list_free(bin->binfiles); - RzListIter *it, *tmp; + RzIterator *it = ht_sp_as_iter(bin->binxtrs); RzBinXtrPlugin *p; - rz_list_foreach_safe (bin->binxtrs, it, tmp, p) { + rz_iterator_foreach(it, p) { plugin_fini(bin, p); } - rz_list_free(bin->binxtrs); - rz_list_free(bin->plugins); + rz_iterator_free(it); + ht_sp_free(bin->binxtrs); + ht_sp_free(bin->plugins); rz_list_free(bin->default_hashes); sdb_free(bin->sdb); rz_id_storage_free(bin->ids); @@ -544,22 +540,18 @@ static void __printXtrPluginDetails(RzBin *bin, RzBinXtrPlugin *bx, int json) { } RZ_API bool rz_bin_list_plugin(RzBin *bin, const char *name, PJ *pj, int json) { - RzListIter *it; RzBinPlugin *bp; RzBinXtrPlugin *bx; rz_return_val_if_fail(bin && name, false); - rz_list_foreach (bin->plugins, it, bp) { - if (rz_str_cmp(name, bp->name, strlen(name))) { - continue; - } + bool found = false; + bp = ht_sp_find(bin->plugins, name, &found); + if (found) { return rz_bin_print_plugin_details(bin, bp, pj, json); } - rz_list_foreach (bin->binxtrs, it, bx) { - if (rz_str_cmp(name, bx->name, strlen(name))) { - continue; - } + bx = ht_sp_find(bin->binxtrs, name, &found); + if (found) { __printXtrPluginDetails(bin, bx, json); return true; } @@ -763,9 +755,16 @@ RZ_API RzBin *rz_bin_new(void) { /* bin parsers */ bin->binfiles = rz_list_newf((RzListFree)rz_bin_file_free); - bin->plugins = rz_list_new_from_array((const void **)bin_static_plugins, RZ_ARRAY_SIZE(bin_static_plugins)); + + bin->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_static_plugins); ++i) { + ht_sp_insert(bin->plugins, bin_static_plugins[i]->name, bin_static_plugins[i]); + } /* extractors */ - bin->binxtrs = rz_list_new_from_array((const void **)bin_xtr_static_plugins, RZ_ARRAY_SIZE(bin_xtr_static_plugins)); + bin->binxtrs = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_xtr_static_plugins); ++i) { + ht_sp_insert(bin->plugins, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i]); + } return bin; @@ -1266,13 +1265,10 @@ RZ_API void rz_bin_trycatch_free(RzBinTrycatch *tc) { RZ_API const RzBinPlugin *rz_bin_plugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL const char *name) { rz_return_val_if_fail(bin && name, NULL); - RzListIter *iter; - RzBinPlugin *bp; - - rz_list_foreach (bin->plugins, iter, bp) { - if (!strcmp(bp->name, name)) { - return bp; - } + bool found = false; + RzBinPlugin *bp = ht_sp_find(bin->plugins, name, &found); + if (found) { + return bp; } return NULL; } @@ -1282,14 +1278,10 @@ RZ_API const RzBinPlugin *rz_bin_plugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL co */ RZ_API const RzBinXtrPlugin *rz_bin_xtrplugin_get(RZ_NONNULL RzBin *bin, RZ_NONNULL const char *name) { rz_return_val_if_fail(bin && name, NULL); - - RzListIter *iter; - RzBinXtrPlugin *bp; - - rz_list_foreach (bin->binxtrs, iter, bp) { - if (!strcmp(bp->name, name)) { - return bp; - } + bool found = false; + RzBinXtrPlugin *bp = ht_sp_find(bin->plugins, name, &found); + if (found) { + return bp; } return NULL; } diff --git a/librz/bp/bp.c b/librz/bp/bp.c index c3fad4139fd..d50c265ffa8 100644 --- a/librz/bp/bp.c +++ b/librz/bp/bp.c @@ -36,7 +36,7 @@ RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx) { bp->traces = rz_bp_traptrace_new(); bp->cb_printf = (PrintfCallback)printf; bp->bps = rz_list_newf((RzListFree)rz_bp_item_free); - bp->plugins = rz_list_new(); + bp->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); bp->nhwbps = 0; for (i = 0; i < RZ_ARRAY_SIZE(bp_static_plugins); i++) { rz_bp_plugin_add(bp, bp_static_plugins[i]); @@ -47,7 +47,7 @@ RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx) { RZ_API RzBreakpoint *rz_bp_free(RzBreakpoint *bp) { rz_list_free(bp->bps); - rz_list_free(bp->plugins); + ht_sp_free(bp->plugins); rz_list_free(bp->traces); free(bp->bps_idx); free(bp); diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index c84f6e8e1eb..c7419ef61f3 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -3,34 +3,34 @@ #include #include +#include +#include RZ_API int rz_bp_plugin_del_byname(RzBreakpoint *bp, RZ_NONNULL const char *name) { rz_return_val_if_fail(bp && name, false); - RzListIter *iter; - RzBreakpointPlugin *h; - rz_list_foreach (bp->plugins, iter, h) { - if (!strcmp(h->name, name)) { - if (bp->cur == h) { - bp->cur = NULL; - } - rz_list_delete(bp->plugins, iter); - bp->nbps--; - return true; - } + bool found = false; + RzBreakpointPlugin *bp_plugin = ht_sp_find(bp->plugins, name, &found); + if (!found) { + return false; } - return false; + if (bp_plugin == bp->cur) { + bp->cur = NULL; + } + ht_sp_delete(bp->plugins, name); + bp->nbps--; + return true; } RZ_API bool rz_bp_plugin_add(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin) { rz_return_val_if_fail(bp && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(bp->plugins, plugin, RzBreakpointPlugin); + ht_sp_insert(bp->plugins, plugin->name, plugin); return true; } RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin) { rz_return_val_if_fail(bp && plugin, false); - bool res = rz_list_delete_data(bp->plugins, plugin); + bool res = ht_sp_delete(bp->plugins, plugin->name); if (res) { bp->nbps--; if (bp->cur == plugin) { @@ -45,24 +45,26 @@ RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpoint */ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { rz_return_val_if_fail(bp && name, false); - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(bp->plugins); RzBreakpointPlugin *h; - rz_list_foreach (bp->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!strcmp(h->name, name)) { bp->cur = h; return true; } } + rz_iterator_free(iter); return false; } // TODO: deprecate RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(bp->plugins); RzBreakpointPlugin *b; - rz_list_foreach (bp->plugins, iter, b) { + rz_iterator_foreach(iter, b) { bp->cb_printf("bp %c %s\n", (bp->cur && !strcmp(bp->cur->name, b->name)) ? '*' : '-', b->name); } + rz_iterator_free(iter); } diff --git a/librz/core/canalysis.c b/librz/core/canalysis.c index a865c18fe68..411e5b80723 100644 --- a/librz/core/canalysis.c +++ b/librz/core/canalysis.c @@ -14,6 +14,7 @@ #include #include "core_private.h" +#include "rz_util/rz_iterator.h" HEAPTYPE(ut64); @@ -3938,13 +3939,14 @@ static bool is_apple_target(RzCore *core) { } static void core_analysis_using_plugins(RzCore *core) { - RzListIter *it; + RzIterator *it = ht_sp_as_iter(core->plugins); const RzCorePlugin *plugin; - rz_list_foreach (core->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (plugin->analysis) { plugin->analysis(core); } } + rz_iterator_free(it); } /** diff --git a/librz/core/casm.c b/librz/core/casm.c index c3734cd7745..f250558b777 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -65,10 +65,10 @@ RZ_API char *rz_core_asm_search(RzCore *core, const char *input) { } static const char *has_esil(RzCore *core, const char *name) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->analysis->plugins); RzAnalysisPlugin *h; rz_return_val_if_fail(core && core->analysis && name, NULL); - rz_list_foreach (core->analysis->plugins, iter, h) { + rz_iterator_foreach(iter, h) { if (!h->name || strcmp(name, h->name)) { continue; } @@ -85,6 +85,7 @@ static const char *has_esil(RzCore *core, const char *name) { // Only the analysis plugin. return "A__"; } + rz_iterator_free(iter); return "___"; } @@ -163,10 +164,10 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC int i; RzAsm *a = core->rasm; RzAsmPlugin *ap; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(a->plugins); RzCmdStatus status; if (arch) { - rz_list_foreach (a->plugins, iter, ap) { + rz_iterator_foreach(iter, ap) { if (ap->cpus && !strcmp(arch, ap->name)) { char *c = rz_str_dup(ap->cpus); int n = rz_str_split(c, ','); @@ -179,17 +180,19 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC } } else { rz_cmd_state_output_array_start(state); - rz_list_foreach (a->plugins, iter, ap) { + rz_iterator_foreach(iter, ap) { const char *license = ap->license ? ap->license : "unknown"; status = rz_core_asm_plugin_print(core, ap, arch, state, license); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); return status; } } rz_cmd_state_output_array_end(state); } + rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cautocmpl.c b/librz/core/cautocmpl.c index c35e0c79c3e..2e096db719f 100644 --- a/librz/core/cautocmpl.c +++ b/librz/core/cautocmpl.c @@ -6,8 +6,6 @@ #include #include -#include "core_private.h" - /** * Describe what needs to be autocompleted. */ @@ -193,8 +191,8 @@ static void autocmplt_bits_plugin(RzAsmPlugin *plugin, RzLineNSCompletionResult static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const char *s, size_t len) { rz_return_if_fail(core->rasm); - RzList *asm_plugins = rz_asm_get_plugins(core->rasm); - RzListIter *it; + HtSP *asm_plugins = rz_asm_get_plugins(core->rasm); + RzIterator *it = ht_sp_as_iter(asm_plugins); RzAsmPlugin *plugin; // @a: can either be used with @a:arch or @a:arch:bits @@ -202,7 +200,7 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch const char *delim = rz_sub_str_rchr(s, 0, len, ':'); if (!delim) { // We autocomplete just the architecture part - rz_list_foreach (asm_plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (!strncmp(plugin->name, s, len)) { rz_line_ns_completion_result_add(res, plugin->name); } @@ -211,13 +209,14 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch } else { // We autocomplete the bits part res->start += delim + 1 - s; - rz_list_foreach (asm_plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (!strncmp(plugin->name, s, delim - s)) { autocmplt_bits_plugin(plugin, res, delim + 1, len - (delim + 1 - s)); break; } } } + rz_iterator_free(it); } static void autocmplt_bits(RzCore *core, RzLineNSCompletionResult *res, const char *s, size_t len) { diff --git a/librz/core/cbin.c b/librz/core/cbin.c index 7679ede502c..dada7992cbf 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "../bin/dwarf/dwarf_private.h" #include "core_private.h" @@ -4904,22 +4905,25 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state RzBinPlugin *bp; RzBinXtrPlugin *bx; - RzListIter *iter; RzCmdStatus status; + RzIterator *iter = ht_sp_as_iter(bin->plugins); rz_cmd_state_output_array_start(state); - rz_list_foreach (bin->plugins, iter, bp) { + rz_iterator_foreach(iter, bp) { status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { return status; } } - rz_list_foreach (bin->binxtrs, iter, bx) { + rz_iterator_free(iter); + iter = ht_sp_as_iter(bin->binxtrs); + rz_iterator_foreach(iter, bx) { status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { return status; } } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cconfig.c b/librz/core/cconfig.c index 9665c4679a6..dbccfd4e1d0 100644 --- a/librz/core/cconfig.c +++ b/librz/core/cconfig.c @@ -249,13 +249,14 @@ static bool cb_analysis_hpskip(void *user, void *data) { static void update_analysis_arch_options(RzCore *core, RzConfigNode *node) { RzAnalysisPlugin *h; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(core->analysis->plugins); if (core && core->analysis && node) { rz_list_purge(node->options); - rz_list_foreach (core->analysis->plugins, it, h) { + rz_iterator_foreach(it, h) { SETOPTIONS(node, h->name, NULL); } } + rz_iterator_free(it); } static bool cb_analysis_arch(void *user, void *data) { @@ -373,14 +374,14 @@ static bool cb_asmassembler(void *user, void *data) { static void update_asmcpu_options(RzCore *core, RzConfigNode *node) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(core->rasm->plugins); rz_return_if_fail(core && core->rasm); const char *arch = rz_config_get(core->config, "asm.arch"); if (!arch || !*arch) { return; } rz_list_purge(node->options); - rz_list_foreach (core->rasm->plugins, iter, h) { + rz_iterator_foreach(it, h) { if (h->cpus && !strcmp(arch, h->name)) { char *c = rz_str_dup(h->cpus); int i, n = rz_str_split(c, ','); @@ -394,6 +395,7 @@ static void update_asmcpu_options(RzCore *core, RzConfigNode *node) { free(c); } } + rz_iterator_free(it); } static bool cb_asmcpu(void *user, void *data) { @@ -425,13 +427,14 @@ static bool cb_asmcpu(void *user, void *data) { static void update_asmarch_options(RzCore *core, RzConfigNode *node) { RzAsmPlugin *h; - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(core->rasm->plugins); if (core && node && core->rasm) { rz_list_purge(node->options); - rz_list_foreach (core->rasm->plugins, iter, h) { + rz_iterator_foreach(it, h) { SETOPTIONS(node, h->name, NULL); } } + rz_iterator_free(it); } static void update_asmbits_options(RzCore *core, RzConfigNode *node) { diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index d31ff60515b..5593afdb367 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only #include +#include static RzCmdStatus core_crypto_plugin_print(RzCmdStateOutput *state, const RzCryptoPlugin *plugin) { PJ *pj = state->d.pj; @@ -36,17 +37,18 @@ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput const RzCryptoPlugin *plugin = NULL; RzCmdStatus status; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(cry->plugins); rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_list_foreach (cry->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { status = core_crypto_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { return status; } } + rz_iterator_free(it); if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index 8bf9f4c6042..f787602f462 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -325,14 +325,14 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s memset(spaces, ' ', 15); spaces[15] = 0; RzDebug *dbg = core->dbg; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(dbg->plugins); RzDebugPlugin *plugin; RzCmdStatus status; if (!dbg) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_list_foreach (dbg->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { int sp = 8 - strlen(plugin->name); spaces[sp] = 0; status = rz_core_debug_plugin_print(dbg, plugin, state, count, spaces); @@ -342,6 +342,7 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s spaces[sp] = ' '; count++; } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/chash.c b/librz/core/chash.c index 85f9bfb8c51..afe3e70d1a6 100644 --- a/librz/core/chash.c +++ b/librz/core/chash.c @@ -36,17 +36,19 @@ RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *st const RzHashPlugin *plugin = NULL; RzCmdStatus status; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(hash->plugins); rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_list_foreach (hash->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { status = rz_core_hash_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(it); return status; } } + rz_iterator_free(it); if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cio.c b/librz/core/cio.c index bd12350beee..03f810f345a 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -480,15 +480,16 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput */ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { RzIOPlugin *plugin; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); if (!io) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); rz_cmd_state_output_set_columnsf(state, "sssss", "perm", "license", "name", "uri", "description"); - rz_list_foreach (io->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { rz_core_io_plugin_print(plugin, state); } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cmd/cmd_egg.c b/librz/core/cmd/cmd_egg.c index 3b2851a6213..1cb8da78578 100644 --- a/librz/core/cmd/cmd_egg.c +++ b/librz/core/cmd/cmd_egg.c @@ -205,12 +205,13 @@ RZ_IPI RzCmdStatus rz_egg_list_plugins_handler(RzCore *core, int argc, const cha if (!egg) { return RZ_CMD_STATUS_ERROR; } - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin *p; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { rz_cons_printf("%s %6s : %s\n", (p->type == RZ_EGG_PLUGIN_SHELLCODE) ? "shc" : "enc", p->name, p->desc); } + rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/core.c b/librz/core/core.c index 4b2da914e78..987a9a47cf7 100644 --- a/librz/core/core.c +++ b/librz/core/core.c @@ -2460,14 +2460,14 @@ RZ_API RzCmdStatus rz_core_core_plugin_print(RzCorePlugin *cp, RzCmdStateOutput } RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *state) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->plugins); RzCorePlugin *cp; RzCmdStatus status; if (!core) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_list_foreach (core->plugins, iter, cp) { + rz_iterator_foreach(iter, cp) { const char *license = cp->license ? cp->license : "???"; @@ -2476,6 +2476,7 @@ RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *st return status; } } + rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cplugin.c b/librz/core/cplugin.c index 45c2e292398..779f0d87d5f 100644 --- a/librz/core/cplugin.c +++ b/librz/core/cplugin.c @@ -13,14 +13,15 @@ static RzCorePlugin *core_static_plugins[] = { RZ_CORE_STATIC_PLUGINS }; RZ_API bool rz_core_plugin_fini(RzCore *core) { rz_return_val_if_fail(core->plugins, false); - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(core->plugins); RzCorePlugin *plugin; - rz_list_foreach (core->plugins, iter, plugin) { + rz_iterator_foreach(iter, plugin) { if (plugin->fini) { plugin->fini(core); } } - rz_list_free(core->plugins); + rz_iterator_free(iter); + ht_sp_free(core->plugins); ht_sp_free(core->plugin_configs); core->plugins = NULL; return true; @@ -30,9 +31,9 @@ RZ_API bool rz_core_plugin_add(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) { rz_return_val_if_fail(core, false); rz_return_val_if_fail(plugin && plugin->init && plugin->name && plugin->author && plugin->license, false); // TODO: Add config from core plugin. - RZ_PLUGIN_CHECK_AND_ADD(core->plugins, plugin, RzCorePlugin); + ht_sp_insert(core->plugins, plugin->name, plugin); if (!plugin->init(core)) { - RZ_PLUGIN_REMOVE(core->plugins, plugin); + ht_sp_delete(core->plugins, plugin->name); return false; } return true; @@ -44,13 +45,13 @@ RZ_API bool rz_core_plugin_del(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) { if (plugin->fini && !plugin->fini(core)) { return false; } - return rz_list_delete_data(core->plugins, plugin); + return ht_sp_delete(core->plugins, plugin->name); } RZ_API bool rz_core_plugin_init(RzCore *core) { int i; bool res = true; - core->plugins = rz_list_new(); + core->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(core_static_plugins); i++) { if (!rz_core_plugin_add(core, core_static_plugins[i])) { RZ_LOG_ERROR("core: error loading core plugin '%s'\n", core_static_plugins[i]->name); diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index 1e42e9646ce..1ae281fea23 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -6,12 +6,12 @@ #include #include "rz_crypto_plugins.h" +static RzCryptoPlugin *crypto_static_plugins[] = { &rz_crypto_plugin_aes, &rz_crypto_plugin_aes_cbc, &rz_crypto_plugin_base64, &rz_crypto_plugin_base91, &rz_crypto_plugin_blowfish, &rz_crypto_plugin_cps2, &rz_crypto_plugin_des, &rz_crypto_plugin_punycode, &rz_crypto_plugin_rc2, &rz_crypto_plugin_rc4, &rz_crypto_plugin_rc6, &rz_crypto_plugin_rol, &rz_crypto_plugin_ror, &rz_crypto_plugin_rot, &rz_crypto_plugin_serpent, &rz_crypto_plugin_xor, &rz_crypto_plugin_sm4_ecb }; + #define RZ_CRYPTO_OUTPUT_SIZE 4096 RZ_LIB_VERSION(rz_crypto); -static RzCryptoPlugin *crypto_static_plugins[] = { RZ_CRYPTO_STATIC_PLUGINS }; - static const struct { const char *name; RzCryptoSelector bit; @@ -66,22 +66,23 @@ RZ_API RZ_BORROW const char *rz_crypto_codec_name(const RzCryptoSelector bit) { RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCrypto *cry, size_t index) { rz_return_val_if_fail(cry, NULL); - RzListIter *it; + RzIterator *it = ht_sp_as_iter(cry->plugins); const RzCryptoPlugin *plugin; size_t i = 0; - rz_list_foreach (cry->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (i == index) { return plugin; } i++; } + rz_iterator_free(it); return NULL; } RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) { rz_return_val_if_fail(cry && plugin, false); - RZ_PLUGIN_CHECK_AND_ADD(cry->plugins, plugin, RzCryptoPlugin); + ht_sp_insert(cry->plugins, plugin->name, plugin); return true; } @@ -91,7 +92,7 @@ RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPl cry->h->fini(cry); cry->h = NULL; } - rz_list_delete_data(cry->plugins, plugin); + ht_sp_delete(cry->plugins, plugin->name); return true; } @@ -107,7 +108,10 @@ RZ_API RZ_OWN RzCrypto *rz_crypto_new(void) { goto rz_crypto_new_bad; } - cry->plugins = rz_list_new_from_array((const void **)crypto_static_plugins, RZ_ARRAY_SIZE(crypto_static_plugins)); + cry->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); + for (size_t i = 0; i < RZ_ARRAY_SIZE(crypto_static_plugins); ++i) { + ht_sp_insert(cry->plugins, crypto_static_plugins[i]->name, crypto_static_plugins[i]); + } if (!cry->plugins) { goto rz_crypto_new_bad; } @@ -126,7 +130,7 @@ RZ_API void rz_crypto_free(RZ_NULLABLE RzCrypto *cry) { if (cry->h && cry->h->fini && !cry->h->fini(cry)) { RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name); } - rz_list_free(cry->plugins); + ht_sp_free(cry->plugins); free(cry->output); free(cry->key); free(cry->iv); @@ -155,12 +159,12 @@ RZ_API void rz_crypto_reset(RZ_NONNULL RzCrypto *cry) { RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) { rz_return_val_if_fail(cry && algo, false); - RzListIter *iter; + RzIterator *it = ht_sp_as_iter(cry->plugins); RzCryptoPlugin *h; if (cry->h && cry->h->fini && !cry->h->fini(cry)) { RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name); } - rz_list_foreach (cry->plugins, iter, h) { + rz_iterator_foreach(it, h) { rz_warn_if_fail(h && h->use); if (h && h->use(algo)) { if (h->init && !h->init(cry)) { @@ -172,6 +176,7 @@ RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) return true; } } + rz_iterator_free(it); return false; } diff --git a/librz/debug/debug.c b/librz/debug/debug.c index 01f791802cb..d1fc5dca85b 100644 --- a/librz/debug/debug.c +++ b/librz/debug/debug.c @@ -8,7 +8,6 @@ #include #include #include -#include RZ_LIB_VERSION(rz_debug); @@ -435,7 +434,7 @@ RZ_API RzDebug *rz_debug_free(RzDebug *dbg) { sdb_free(dbg->sgnls); rz_tree_free(dbg->tree); ht_up_free(dbg->tracenodes); - rz_list_free(dbg->plugins); + ht_sp_free(dbg->plugins); rz_list_free(dbg->call_frames); free(dbg->btalgo); rz_debug_trace_free(dbg->trace); diff --git a/librz/debug/plugin.c b/librz/debug/plugin.c index 4bb8428e2f5..15b19a3c1ee 100644 --- a/librz/debug/plugin.c +++ b/librz/debug/plugin.c @@ -9,25 +9,19 @@ static RzDebugPlugin *debug_static_plugins[] = { RZ_DEBUG_STATIC_PLUGINS }; RZ_API void rz_debug_plugin_init(RzDebug *dbg) { int i; - dbg->plugins = rz_list_new(); + dbg->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(debug_static_plugins); i++) { rz_debug_plugin_add(dbg, debug_static_plugins[i]); } } -RZ_API bool rz_debug_use(RzDebug *dbg, const char *str) { +RZ_API bool rz_debug_use(RzDebug *dbg, const char *name) { rz_return_val_if_fail(dbg, false); RzDebugPlugin *new_plugin = NULL; - if (str) { - RzDebugPlugin *h; - RzListIter *iter; - rz_list_foreach (dbg->plugins, iter, h) { - if (h->name && !strcmp(str, h->name)) { - new_plugin = h; - break; - } - } - if (!new_plugin) { + if (name) { + bool found = false; + new_plugin = ht_sp_find(dbg->plugins, name, &found); + if (!found) { return false; } } @@ -58,7 +52,7 @@ RZ_API bool rz_debug_use(RzDebug *dbg, const char *str) { RZ_API bool rz_debug_plugin_add(RzDebug *dbg, RZ_NONNULL RzDebugPlugin *plugin) { rz_return_val_if_fail(dbg && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(dbg->plugins, plugin, RzDebugPlugin); + ht_sp_insert(dbg->plugins, plugin->name, plugin); return true; } @@ -69,7 +63,7 @@ RZ_API bool rz_debug_plugin_del(RzDebug *dbg, RZ_NONNULL RzDebugPlugin *plugin) dbg->cur = NULL; dbg->plugin_data = NULL; } - return rz_list_delete_data(dbg->plugins, plugin); + return ht_sp_delete(dbg->plugins, plugin->name); } RZ_API bool rz_debug_plugin_set_reg_profile(RzDebug *dbg, const char *profile) { diff --git a/librz/egg/egg.c b/librz/egg/egg.c index 65520aa944c..64e90107269 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -79,7 +79,7 @@ RZ_API RzEgg *rz_egg_new(void) { if (!egg->patches) { goto beach; } - egg->plugins = rz_list_new(); + egg->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(egg_static_plugins); i++) { rz_egg_plugin_add(egg, egg_static_plugins[i]); } @@ -92,13 +92,13 @@ RZ_API RzEgg *rz_egg_new(void) { RZ_API bool rz_egg_plugin_add(RzEgg *a, RZ_NONNULL RzEggPlugin *plugin) { rz_return_val_if_fail(a && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(a->plugins, plugin, RzEggPlugin); + ht_sp_insert(a->plugins, plugin->name, plugin); return true; } RZ_API bool rz_egg_plugin_del(RzEgg *a, RZ_NONNULL RzEggPlugin *plugin) { rz_return_val_if_fail(a && plugin, false); - return rz_list_delete_data(a->plugins, plugin); + return ht_sp_delete(a->plugins, plugin->name); } RZ_API char *rz_egg_to_string(RzEgg *egg) { @@ -115,7 +115,7 @@ RZ_API void rz_egg_free(RzEgg *egg) { rz_asm_free(egg->rasm); rz_syscall_free(egg->syscall); sdb_free(egg->db); - rz_list_free(egg->plugins); + ht_sp_free(egg->plugins); rz_list_free(egg->patches); rz_egg_lang_free(egg); free(egg); @@ -533,9 +533,9 @@ RZ_API char *rz_egg_option_get(RzEgg *egg, const char *key) { RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { RzEggPlugin *p; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzBuffer *b; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_SHELLCODE && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { @@ -548,14 +548,15 @@ RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { return true; } } + rz_iterator_free(iter); return false; } RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { RzEggPlugin *p; - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzBuffer *b; - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { @@ -566,6 +567,7 @@ RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { return true; } } + rz_iterator_free(iter); return false; } diff --git a/librz/hash/hash.c b/librz/hash/hash.c index 4fb30662cd7..36cdeff8c79 100644 --- a/librz/hash/hash.c +++ b/librz/hash/hash.c @@ -215,32 +215,13 @@ static HashCfgConfig *hash_cfg_config_new(const RzHashPlugin *plugin) { return mdc; } -RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_index(RZ_NONNULL RzHash *rh, size_t index) { - rz_return_val_if_fail(rh, NULL); - - RzListIter *it; - const RzHashPlugin *rhp; - size_t i = 0; - - rz_list_foreach (rh->plugins, it, rhp) { - if (i == index) { - return rhp; - } - i++; - } - return NULL; -} - RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_name(RZ_NONNULL RzHash *rh, RZ_NONNULL const char *name) { rz_return_val_if_fail(name && rh, NULL); - RzListIter *it; - const RzHashPlugin *rhp; - - rz_list_foreach (rh->plugins, it, rhp) { - if (!strcmp(rhp->name, name)) { - return rhp; - } + bool found = false; + const RzHashPlugin *rhp = ht_sp_find(rh->plugins, name, &found); + if (found) { + return rhp; } return NULL; } @@ -326,10 +307,10 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha } HashCfgConfig *mdc = NULL; - RzListIter *it; + RzIterator *it = ht_sp_as_iter(md->hash->plugins); const RzHashPlugin *plugin; - rz_list_foreach (md->hash->plugins, it, plugin) { + rz_iterator_foreach(it, plugin) { if (is_all || !strcmp(plugin->name, name)) { mdc = hash_cfg_config_new(plugin); if (!mdc) { @@ -347,6 +328,7 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha } } } + rz_iterator_free(it); if (is_all) { return true; @@ -708,7 +690,7 @@ RZ_API RzHash *rz_hash_new(void) { #if REQUIRE_OPENSSL_PROVIDER rz_hash_init_openssl_lib(); #endif /* REQUIRE_OPENSSL_PROVIDER */ - rh->plugins = rz_list_new(); + rh->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (int i = 0; i < RZ_ARRAY_SIZE(hash_static_plugins); i++) { rz_hash_plugin_add(rh, hash_static_plugins[i]); } @@ -719,7 +701,7 @@ RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh) { if (!rh) { return; } - rz_list_free(rh->plugins); + ht_sp_free(rh->plugins); free(rh); #if REQUIRE_OPENSSL_PROVIDER rz_hash_fini_openssl_lib(); @@ -732,11 +714,11 @@ RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh) { */ RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin) { rz_return_val_if_fail(rh && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(rh->plugins, plugin, RzHashPlugin); + ht_sp_insert(rh->plugins, plugin->name, plugin); return true; } RZ_API bool rz_hash_plugin_del(RZ_NONNULL RzHash *rh, RZ_NONNULL RzHashPlugin *plugin) { rz_return_val_if_fail(rh && plugin, false); - return rz_list_delete_data(rh->plugins, plugin); + return ht_sp_delete(rh->plugins, plugin->name); } diff --git a/librz/include/rz_analysis.h b/librz/include/rz_analysis.h index 8a847e05b2e..6a35627cfab 100644 --- a/librz/include/rz_analysis.h +++ b/librz/include/rz_analysis.h @@ -497,7 +497,7 @@ typedef struct rz_analysis_t { RzAnalysisILVM *il_vm; ///< user-faced VM, NEVER use this for any analysis passes! struct rz_analysis_plugin_t *cur; RzAnalysisRange *limit; // analysis.from, analysis.to - RzList /**/ *plugins; + HtSP /**/ *plugins; Sdb *sdb_noret; Sdb *sdb_fmts; HtUP *ht_xrefs_from; diff --git a/librz/include/rz_asm.h b/librz/include/rz_asm.h index d086fa36101..d0fab182895 100644 --- a/librz/include/rz_asm.h +++ b/librz/include/rz_asm.h @@ -108,7 +108,7 @@ typedef struct rz_asm_t { ut64 pc; _RzAsmPlugin *cur; _RzAsmPlugin *acur; - RzList /**/ *plugins; + HtSP /**/ *plugins; RzBinBind binb; RzParse *ifilter; RzParse *ofilter; @@ -180,7 +180,7 @@ RZ_API ut8 *rz_asm_from_string(RzAsm *a, ut64 addr, const char *b, int *l); RZ_API int rz_asm_sub_names_input(RzAsm *a, const char *f); RZ_API int rz_asm_sub_names_output(RzAsm *a, const char *f); RZ_API char *rz_asm_describe(RzAsm *a, const char *str); -RZ_API RzList /**/ *rz_asm_get_plugins(RzAsm *a); +RZ_API RZ_BORROW HtSP /**/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a); RZ_API void rz_asm_list_directives(void); /* code.c */ diff --git a/librz/include/rz_bin.h b/librz/include/rz_bin.h index 592981a30fa..829dcce4a2f 100644 --- a/librz/include/rz_bin.h +++ b/librz/include/rz_bin.h @@ -367,8 +367,8 @@ struct rz_bin_t { int rawstr; RZ_DEPRECATE Sdb *sdb; RzIDStorage *ids; - RzList /**/ *plugins; - RzList /**/ *binxtrs; + HtSP /**/ *plugins; + HtSP /**/ *binxtrs; RzList /**/ *binfiles; PrintfCallback cb_printf; int loadany; diff --git a/librz/include/rz_bp.h b/librz/include/rz_bp.h index 912102c1eba..3d2fff72c21 100644 --- a/librz/include/rz_bp.h +++ b/librz/include/rz_bp.h @@ -84,7 +84,7 @@ typedef struct rz_bp_t { RzIOBind iob; // compile time dependency RzBreakpointPlugin *cur; RzList /**/ *traces; // XXX - RzList /**/ *plugins; + HtSP /**/ *plugins; PrintfCallback cb_printf; RzBreakpointCallback breakpoint; /* storage of breakpoints */ diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 3469f19dd8a..20796b8131a 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -252,7 +252,7 @@ struct rz_core_t { // NOTE: Do not change the order of fields above! // They are used in pointer passing hacks in rz_types.h. RzIO *io; - RzList /**/ *plugins; ///< List of registered core plugins + HtSP /**/ *plugins; ///< List of registered core plugins RzConfig *config; HtSP /*: RzConfig>*/ *plugin_configs; ///< Pointers to plugin configurations. Indexed by "plugins." ut64 offset; // current seek diff --git a/librz/include/rz_crypto.h b/librz/include/rz_crypto.h index 6c08a57e8d8..ee33e68cbc1 100644 --- a/librz/include/rz_crypto.h +++ b/librz/include/rz_crypto.h @@ -1,9 +1,9 @@ #ifndef RZ_CRYPTO_H #define RZ_CRYPTO_H -#include "rz_types.h" -#include "rz_list.h" -#include "rz_crypto/rz_des.h" +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -34,7 +34,7 @@ typedef struct rz_crypto_t { int output_size; int dir; void *user; - RzList /**/ *plugins; + HtSP /**/ *plugins; } RzCrypto; typedef struct rz_crypto_plugin_t { diff --git a/librz/include/rz_debug.h b/librz/include/rz_debug.h index b0df51a6353..ebc464aaca3 100644 --- a/librz/include/rz_debug.h +++ b/librz/include/rz_debug.h @@ -292,7 +292,7 @@ typedef struct rz_debug_t { struct rz_debug_plugin_t *cur; void *plugin_data; - RzList /**/ *plugins; + HtSP /**/ *plugins; bool pc_at_bp; /* after a breakpoint, is the pc at the bp? */ bool pc_at_bp_set; /* is the pc_at_bp variable set already? */ diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index 8a9a0098c64..bfd5a89e915 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -99,7 +99,7 @@ typedef struct rz_egg_t { RzSyscall *syscall; RzEggLang lang; Sdb *db; - RzList /**/ *plugins; + HtSP /**/ *plugins; RzList /**/ *patches; // struct rz_egg_emit_t *remit; int arch; diff --git a/librz/include/rz_hash.h b/librz/include/rz_hash.h index 83f280a7c70..bab1b12fd50 100644 --- a/librz/include/rz_hash.h +++ b/librz/include/rz_hash.h @@ -6,6 +6,7 @@ #include #include +#include #include #ifdef __cplusplus @@ -39,7 +40,7 @@ typedef struct rz_hash_plugin_t { } RzHashPlugin; typedef struct rz_hash_t { - RzList /**/ *plugins; + HtSP /**/ *plugins; } RzHash; typedef struct rz_hash_cfg_t { @@ -54,7 +55,6 @@ RZ_API RzHash *rz_hash_new(void); RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh); RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin); RZ_API bool rz_hash_plugin_del(RZ_NONNULL RzHash *rh, RZ_NONNULL RzHashPlugin *plugin); -RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_index(RZ_NONNULL RzHash *rh, size_t index); RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_name(RZ_NONNULL RzHash *rh, RZ_NONNULL const char *name); RZ_API RZ_OWN RzHashCfg *rz_hash_cfg_new(RZ_NONNULL RzHash *rh); diff --git a/librz/include/rz_io.h b/librz/include/rz_io.h index 51c3de97295..f27d570d39c 100644 --- a/librz/include/rz_io.h +++ b/librz/include/rz_io.h @@ -77,7 +77,7 @@ typedef struct rz_io_t { RzSkyline cache_skyline; ut8 *write_mask; int write_mask_len; - RzList /**/ *plugins; + HtSP /**/ *plugins; char *runprofile; char *envprofile; #if USE_PTRACE_WRAP diff --git a/librz/include/rz_lib.h b/librz/include/rz_lib.h index 295edf4923f..f652ba80a4a 100644 --- a/librz/include/rz_lib.h +++ b/librz/include/rz_lib.h @@ -106,23 +106,6 @@ typedef struct rz_lib_t { HtSU *opened_dirs; ///< Hashtable to keep track of already opened directories } RzLib; -#define RZ_PLUGIN_CHECK_AND_ADD(plugins, plugin, py_type) \ - do { \ - RzListIter *_it; \ - py_type *_p; \ - rz_list_foreach ((plugins), _it, _p) { \ - if (!strcmp(_p->name, (plugin)->name)) { \ - return false; \ - } \ - } \ - rz_list_append(plugins, plugin); \ - } while (0) - -#define RZ_PLUGIN_REMOVE(plugins, plugin) \ - do { \ - rz_list_delete_data(plugins, plugin); \ - } while (0) - #ifdef RZ_API RZ_API RzLib *rz_lib_new(RZ_NULLABLE const char *symname, RZ_NULLABLE const char *symnamefunc); RZ_API void rz_lib_free(RzLib *lib); diff --git a/librz/io/io.c b/librz/io/io.c index 13ee108f2f6..bfde065ebf2 100644 --- a/librz/io/io.c +++ b/librz/io/io.c @@ -733,7 +733,7 @@ RZ_API int rz_io_fini(RzIO *io) { rz_io_desc_cache_fini_all(io); rz_io_desc_fini(io); rz_io_map_fini(io); - rz_list_free(io->plugins); + ht_sp_free(io->plugins); rz_io_cache_fini(io); if (io->runprofile) { RZ_FREE(io->runprofile); diff --git a/librz/io/io_plugin.c b/librz/io/io_plugin.c index ab45d270828..9ac77cf4056 100644 --- a/librz/io/io_plugin.c +++ b/librz/io/io_plugin.c @@ -6,6 +6,8 @@ #include #include "rz_io_plugins.h" +#include "rz_util/ht_sp.h" +#include "rz_util/rz_iterator.h" static volatile RzIOPlugin *default_plugin = NULL; @@ -13,7 +15,7 @@ static RzIOPlugin *io_static_plugins[] = { RZ_IO_STATIC_PLUGINS }; RZ_API bool rz_io_plugin_add(RzIO *io, RZ_NONNULL RZ_BORROW RzIOPlugin *plugin) { rz_return_val_if_fail(io && plugin && plugin->name, false); - RZ_PLUGIN_CHECK_AND_ADD(io->plugins, plugin, RzIOPlugin); + ht_sp_insert(io->plugins, plugin->name, plugin); return true; } @@ -29,7 +31,7 @@ static bool close_if_plugin(void *user, void *data, ut32 id) { RZ_API bool rz_io_plugin_del(RzIO *io, RZ_NONNULL RZ_BORROW RzIOPlugin *plugin) { rz_return_val_if_fail(io && plugin, false); rz_id_storage_foreach(io->files, close_if_plugin, plugin); - return rz_list_delete_data(io->plugins, plugin); + return ht_sp_delete(io->plugins, plugin->name); } RZ_API bool rz_io_plugin_init(RzIO *io) { @@ -37,7 +39,7 @@ RZ_API bool rz_io_plugin_init(RzIO *io) { if (!io) { return false; } - io->plugins = rz_list_new(); + io->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (i = 0; i < RZ_ARRAY_SIZE(io_static_plugins); i++) { if (!io_static_plugins[i]->name) { continue; @@ -55,9 +57,9 @@ RZ_API RzIOPlugin *rz_io_plugin_get_default(RzIO *io, const char *filename, bool } RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool many) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); RzIOPlugin *ret; - rz_list_foreach (io->plugins, iter, ret) { + rz_iterator_foreach(iter, ret) { if (!ret || !ret->check) { continue; } @@ -65,17 +67,19 @@ RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool man return ret; } } + rz_iterator_free(iter); return rz_io_plugin_get_default(io, filename, many); } RZ_API RzIOPlugin *rz_io_plugin_byname(RzIO *io, const char *name) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(io->plugins); RzIOPlugin *iop; - rz_list_foreach (io->plugins, iter, iop) { + rz_iterator_foreach(iter, iop) { if (!strcmp(name, iop->name)) { return iop; } } + rz_iterator_free(iter); return rz_io_plugin_get_default(io, name, false); } diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index b2181655134..156a630b12b 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2011-2020 pancake // SPDX-License-Identifier: LGPL-3.0-only +#include "rz_util/rz_iterator.h" #include #include #include @@ -68,20 +69,23 @@ static int usage(int v) { } static void list(RzEgg *egg) { - RzListIter *iter; + RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin *p; printf("shellcodes:\n"); - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_SHELLCODE) { printf("%10s : %s\n", p->name, p->desc); } } + rz_iterator_free(iter); + iter = ht_sp_as_iter(egg->plugins); printf("encoders:\n"); - rz_list_foreach (egg->plugins, iter, p) { + rz_iterator_foreach(iter, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER) { printf("%10s : %s\n", p->name, p->desc); } } + rz_iterator_free(iter); } static bool create(const char *format, const char *arch, int bits, const ut8 *code, int codelen) { diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index 8d9908fa56a..d8a6282a156 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -131,10 +131,12 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { printf("flags algorithm license author\n"); const RzHashPlugin *rmdp; - for (size_t i = 0; (rmdp = rz_hash_plugin_by_index(ctx->rh, i)); ++i) { + RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); + rz_iterator_foreach(it, rmdp) { snprintf(flags, sizeof(flags), "____h%c", rmdp->support_hmac ? 'm' : '_'); printf("%6s %-14s %-10s %s\n", flags, rmdp->name, rmdp->license, rmdp->author); } + rz_iterator_free(it); const RzCryptoPlugin *rcp; for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(ctx->rc, i)); i++) { @@ -809,9 +811,11 @@ static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { if (!list) { return NULL; } - for (ut64 i = 0; (plugin = rz_hash_plugin_by_index(ctx->rh, i)); ++i) { + RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); + rz_iterator_foreach(it, plugin) { rz_list_append(list, (void *)plugin->name); } + rz_iterator_free(it); return list; } return rz_str_split_list(ctx->algorithm, ",", 0); diff --git a/librz/util/iterator.c b/librz/util/iterator.c index 88e78e6357b..d562b48a416 100644 --- a/librz/util/iterator.c +++ b/librz/util/iterator.c @@ -75,4 +75,4 @@ RZ_API void rz_iterator_free(RzIterator *it) { it->free_u(it->u); } free(it); -} \ No newline at end of file +} From 0d86681949479bb5b565ca8704a726410b7865e9 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 22 Oct 2024 10:59:34 -0500 Subject: [PATCH 02/11] Fix: iterators iterate over pointers of pointers. --- librz/arch/analysis.c | 5 +++-- librz/arch/asm.c | 16 ++++++++++------ librz/bin/bin.c | 21 +++++++++++++-------- librz/bp/bp_plugin.c | 10 ++++++---- librz/core/canalysis.c | 5 +++-- librz/core/casm.c | 13 ++++++++----- librz/core/cautocmpl.c | 8 +++++--- librz/core/cbin.c | 10 ++++++---- librz/core/cconfig.c | 18 +++++++++++------- librz/core/ccrypto.c | 5 +++-- librz/core/cdebug.c | 5 +++-- librz/core/chash.c | 5 +++-- librz/core/cio.c | 7 +++++-- librz/core/cmd/cmd_egg.c | 5 +++-- librz/core/core.c | 5 +++-- librz/core/cplugin.c | 5 +++-- librz/crypto/crypto.c | 10 ++++++---- librz/egg/egg.c | 12 ++++++++---- librz/hash/hash.c | 5 +++-- librz/io/io_plugin.c | 12 ++++++++---- librz/main/rz-gg.c | 8 +++++--- librz/main/rz-hash.c | 10 ++++++---- 22 files changed, 124 insertions(+), 76 deletions(-) diff --git a/librz/arch/analysis.c b/librz/arch/analysis.c index 6f83e4f59a2..8b3e295a840 100644 --- a/librz/arch/analysis.c +++ b/librz/arch/analysis.c @@ -212,13 +212,14 @@ RZ_API bool rz_analysis_plugin_del(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPl RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { RzIterator *it = ht_sp_as_iter(analysis->plugins); - RzAnalysisPlugin *h; + RzAnalysisPlugin **val; if (analysis) { if (analysis->cur && !strcmp(analysis->cur->name, name)) { return true; } - rz_iterator_foreach(it, h) { + rz_iterator_foreach(it, val) { + RzAnalysisPlugin *h = *val; if (!h || !h->name || strcmp(h->name, name)) { continue; } diff --git a/librz/arch/asm.c b/librz/arch/asm.c index 8deb3944bcb..ff0a270a4b8 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -375,12 +375,13 @@ RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { } RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) { - RzAsmPlugin *h; RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; if (!name || !*name) { return false; } - rz_iterator_foreach(iter, h) { + rz_iterator_foreach(iter, val) { + RzAsmPlugin *h = *val; if (!strcmp(h->name, name)) { return true; } @@ -442,8 +443,9 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name * \return false Asm plugin failed to be enabled. */ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { - RzAsmPlugin *h; + rz_return_val_if_fail(a && name, false); RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; if (!a || !name) { return false; } @@ -451,7 +453,8 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) { if (a->cur && !strcmp(a->cur->arch, name)) { return true; } - rz_iterator_foreach(iter, h) { + rz_iterator_foreach(iter, val) { + RzAsmPlugin *h = *val; if (h->arch && h->name && !strcmp(h->name, name)) { if (!a->cur || (a->cur && strcmp(a->cur->arch, h->arch))) { plugin_fini(a); @@ -632,12 +635,13 @@ static bool assemblerMatches(RzAsm *a, RzAsmPlugin *h) { static Ase findAssembler(RzAsm *a, const char *kw) { Ase ase = NULL; - RzAsmPlugin *h; RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; if (a->acur && a->acur->assemble) { return a->acur->assemble; } - rz_iterator_foreach(iter, h) { + rz_iterator_foreach(iter, val) { + RzAsmPlugin *h = *val; if (assemblerMatches(a, h)) { if (kw) { if (strstr(h->name, kw)) { diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 476bb547ff6..0f22404304e 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -259,7 +259,7 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) rz_return_val_if_fail(bin && opt, NULL); RzIterator *it = ht_sp_as_iter(bin->binxtrs); - RzBinXtrPlugin *xtr; + RzBinXtrPlugin **val; bin->file = opt->filename; if (opt->obj_opts.loadaddr == UT64_MAX) { @@ -271,7 +271,8 @@ RZ_API RzBinFile *rz_bin_open_buf(RzBin *bin, RzBuffer *buf, RzBinOptions *opt) // XXX - for the time being this is fine, but we may want to // change the name to something like // : - rz_iterator_foreach(it, xtr) { + rz_iterator_foreach(it, val) { + RzBinXtrPlugin *xtr = *val; if (!xtr->check_buffer) { RZ_LOG_ERROR("Missing check_buffer callback for '%s'\n", xtr->name); continue; @@ -365,12 +366,14 @@ RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_name(RzBin *bin, const char *name) { } RZ_API RzBinPlugin *rz_bin_get_binplugin_by_buffer(RzBin *bin, RzBuffer *buf) { - RzBinPlugin *plugin; + rz_return_val_if_fail(bin && buf, NULL); RzIterator *it = ht_sp_as_iter(bin->plugins); + RzBinPlugin **val; rz_return_val_if_fail(bin && buf, NULL); - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + RzBinPlugin *plugin = *val; if (plugin->check_buffer) { if (plugin->check_buffer(buf)) { return plugin; @@ -382,14 +385,15 @@ RZ_API RzBinPlugin *rz_bin_get_binplugin_by_buffer(RzBin *bin, RzBuffer *buf) { } RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_filename(RzBin *bin) { - RzBinPlugin *plugin; RzIterator *it = ht_sp_as_iter(bin->plugins); + RzBinPlugin **val; rz_return_val_if_fail(bin, NULL); const char *filename = strrchr(bin->file, RZ_SYS_DIR[0]); filename = filename ? filename + 1 : bin->file; - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + RzBinPlugin *plugin = *val; if (plugin->check_filename) { if (plugin->check_filename(filename)) { return plugin; @@ -473,8 +477,9 @@ RZ_API void rz_bin_free(RZ_NULLABLE RzBin *bin) { rz_list_free(bin->binfiles); RzIterator *it = ht_sp_as_iter(bin->binxtrs); - RzBinXtrPlugin *p; - rz_iterator_foreach(it, p) { + RzBinXtrPlugin **val; + rz_iterator_foreach(it, val) { + RzBinXtrPlugin *p = *val; plugin_fini(bin, p); } rz_iterator_free(it); diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index c7419ef61f3..22806ce6954 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -46,8 +46,9 @@ RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpoint RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { rz_return_val_if_fail(bp && name, false); RzIterator *iter = ht_sp_as_iter(bp->plugins); - RzBreakpointPlugin *h; - rz_iterator_foreach(iter, h) { + RzBreakpointPlugin **val; + rz_iterator_foreach(iter, val) { + RzBreakpointPlugin *h = *val; if (!strcmp(h->name, name)) { bp->cur = h; return true; @@ -60,8 +61,9 @@ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { // TODO: deprecate RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { RzIterator *iter = ht_sp_as_iter(bp->plugins); - RzBreakpointPlugin *b; - rz_iterator_foreach(iter, b) { + RzBreakpointPlugin **val; + rz_iterator_foreach(iter, val) { + RzBreakpointPlugin *b = *val; bp->cb_printf("bp %c %s\n", (bp->cur && !strcmp(bp->cur->name, b->name)) ? '*' : '-', b->name); diff --git a/librz/core/canalysis.c b/librz/core/canalysis.c index 411e5b80723..b729a9a6d00 100644 --- a/librz/core/canalysis.c +++ b/librz/core/canalysis.c @@ -3940,8 +3940,9 @@ static bool is_apple_target(RzCore *core) { static void core_analysis_using_plugins(RzCore *core) { RzIterator *it = ht_sp_as_iter(core->plugins); - const RzCorePlugin *plugin; - rz_iterator_foreach(it, plugin) { + RzCorePlugin **val; + rz_iterator_foreach(it, val) { + RzCorePlugin *plugin = *val; if (plugin->analysis) { plugin->analysis(core); } diff --git a/librz/core/casm.c b/librz/core/casm.c index f250558b777..bca80cee470 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -66,9 +66,10 @@ RZ_API char *rz_core_asm_search(RzCore *core, const char *input) { static const char *has_esil(RzCore *core, const char *name) { RzIterator *iter = ht_sp_as_iter(core->analysis->plugins); - RzAnalysisPlugin *h; + RzAnalysisPlugin **val; rz_return_val_if_fail(core && core->analysis && name, NULL); - rz_iterator_foreach(iter, h) { + rz_iterator_foreach(iter, val) { + RzAnalysisPlugin *h = *val; if (!h->name || strcmp(name, h->name)) { continue; } @@ -163,11 +164,12 @@ RZ_API RzCmdStatus rz_core_asm_plugin_print(RzCore *core, RzAsmPlugin *ap, const RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzCmdStateOutput *state) { int i; RzAsm *a = core->rasm; - RzAsmPlugin *ap; RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; RzCmdStatus status; if (arch) { - rz_iterator_foreach(iter, ap) { + rz_iterator_foreach(iter, val) { + RzAsmPlugin *ap = *val; if (ap->cpus && !strcmp(arch, ap->name)) { char *c = rz_str_dup(ap->cpus); int n = rz_str_split(c, ','); @@ -180,7 +182,8 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC } } else { rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, ap) { + rz_iterator_foreach(iter, val) { + RzAsmPlugin *ap = *val; const char *license = ap->license ? ap->license : "unknown"; diff --git a/librz/core/cautocmpl.c b/librz/core/cautocmpl.c index 2e096db719f..d3e0be1b526 100644 --- a/librz/core/cautocmpl.c +++ b/librz/core/cautocmpl.c @@ -193,14 +193,15 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch HtSP *asm_plugins = rz_asm_get_plugins(core->rasm); RzIterator *it = ht_sp_as_iter(asm_plugins); - RzAsmPlugin *plugin; + RzAsmPlugin **val; // @a: can either be used with @a:arch or @a:arch:bits // Check for `:` to determine where we are const char *delim = rz_sub_str_rchr(s, 0, len, ':'); if (!delim) { // We autocomplete just the architecture part - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + RzAsmPlugin *plugin = *val; if (!strncmp(plugin->name, s, len)) { rz_line_ns_completion_result_add(res, plugin->name); } @@ -209,7 +210,8 @@ static void autocmplt_arch(RzCore *core, RzLineNSCompletionResult *res, const ch } else { // We autocomplete the bits part res->start += delim + 1 - s; - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + RzAsmPlugin *plugin = *val; if (!strncmp(plugin->name, s, delim - s)) { autocmplt_bits_plugin(plugin, res, delim + 1, len - (delim + 1 - s)); break; diff --git a/librz/core/cbin.c b/librz/core/cbin.c index dada7992cbf..e3f379bdbb6 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -4903,13 +4903,14 @@ RZ_IPI RzCmdStatus rz_core_binxtr_plugin_print(const RzBinXtrPlugin *bx, RzCmdSt RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state) { rz_return_val_if_fail(bin && state, RZ_CMD_STATUS_ERROR); - RzBinPlugin *bp; - RzBinXtrPlugin *bx; RzCmdStatus status; RzIterator *iter = ht_sp_as_iter(bin->plugins); + RzBinPlugin **bp_val; + RzBinXtrPlugin **bx_val; rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, bp) { + rz_iterator_foreach(iter, bp_val) { + RzBinPlugin *bp = *bp_val; status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { return status; @@ -4917,7 +4918,8 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state } rz_iterator_free(iter); iter = ht_sp_as_iter(bin->binxtrs); - rz_iterator_foreach(iter, bx) { + rz_iterator_foreach(iter, bx_val) { + RzBinXtrPlugin *bx = *bx_val; status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { return status; diff --git a/librz/core/cconfig.c b/librz/core/cconfig.c index dbccfd4e1d0..e7d6973b511 100644 --- a/librz/core/cconfig.c +++ b/librz/core/cconfig.c @@ -248,11 +248,12 @@ static bool cb_analysis_hpskip(void *user, void *data) { } static void update_analysis_arch_options(RzCore *core, RzConfigNode *node) { - RzAnalysisPlugin *h; RzIterator *it = ht_sp_as_iter(core->analysis->plugins); + RzAnalysisPlugin **val; if (core && core->analysis && node) { rz_list_purge(node->options); - rz_iterator_foreach(it, h) { + rz_iterator_foreach(it, val) { + RzAnalysisPlugin *h = *val; SETOPTIONS(node, h->name, NULL); } } @@ -373,15 +374,17 @@ static bool cb_asmassembler(void *user, void *data) { } static void update_asmcpu_options(RzCore *core, RzConfigNode *node) { - RzAsmPlugin *h; - RzIterator *it = ht_sp_as_iter(core->rasm->plugins); rz_return_if_fail(core && core->rasm); + + RzIterator *it = ht_sp_as_iter(core->rasm->plugins); + RzAsmPlugin **val; const char *arch = rz_config_get(core->config, "asm.arch"); if (!arch || !*arch) { return; } rz_list_purge(node->options); - rz_iterator_foreach(it, h) { + rz_iterator_foreach(it, val) { + RzAsmPlugin *h = *val; if (h->cpus && !strcmp(arch, h->name)) { char *c = rz_str_dup(h->cpus); int i, n = rz_str_split(c, ','); @@ -426,11 +429,12 @@ static bool cb_asmcpu(void *user, void *data) { } static void update_asmarch_options(RzCore *core, RzConfigNode *node) { - RzAsmPlugin *h; RzIterator *it = ht_sp_as_iter(core->rasm->plugins); + RzAsmPlugin **val; if (core && node && core->rasm) { rz_list_purge(node->options); - rz_iterator_foreach(it, h) { + rz_iterator_foreach(it, val) { + RzAsmPlugin *h = *val; SETOPTIONS(node, h->name, NULL); } } diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index 5593afdb367..80b7358016c 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -35,14 +35,15 @@ static RzCmdStatus core_crypto_plugin_print(RzCmdStateOutput *state, const RzCry RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput *state) { rz_return_val_if_fail(cry, RZ_CMD_STATUS_ERROR); - const RzCryptoPlugin *plugin = NULL; RzCmdStatus status; RzIterator *it = ht_sp_as_iter(cry->plugins); + RzCryptoPlugin **val; rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + const RzCryptoPlugin *plugin = *val; status = core_crypto_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { return status; diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index f787602f462..9f02183e9f1 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -326,13 +326,14 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s spaces[15] = 0; RzDebug *dbg = core->dbg; RzIterator *iter = ht_sp_as_iter(dbg->plugins); - RzDebugPlugin *plugin; + RzDebugPlugin **val; RzCmdStatus status; if (!dbg) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, plugin) { + rz_iterator_foreach(iter, val) { + RzDebugPlugin *plugin = *val; int sp = 8 - strlen(plugin->name); spaces[sp] = 0; status = rz_core_debug_plugin_print(dbg, plugin, state, count, spaces); diff --git a/librz/core/chash.c b/librz/core/chash.c index afe3e70d1a6..ca64cecf357 100644 --- a/librz/core/chash.c +++ b/librz/core/chash.c @@ -34,14 +34,15 @@ RZ_API RzCmdStatus rz_core_hash_plugin_print(RzCmdStateOutput *state, const RzHa RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *state) { rz_return_val_if_fail(hash, RZ_CMD_STATUS_ERROR); - const RzHashPlugin *plugin = NULL; RzCmdStatus status; RzIterator *it = ht_sp_as_iter(hash->plugins); + RzHashPlugin **val; rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + const RzHashPlugin *plugin = *val; status = rz_core_hash_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(it); diff --git a/librz/core/cio.c b/librz/core/cio.c index 03f810f345a..4fc7cf85c48 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -479,14 +479,17 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput * \param state Specify how plugins shall be printed */ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { - RzIOPlugin *plugin; + rz_return_val_if_fail(io && state, RZ_CMD_STATUS_ERROR); RzIterator *iter = ht_sp_as_iter(io->plugins); + RzIOPlugin **val; + if (!io) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); rz_cmd_state_output_set_columnsf(state, "sssss", "perm", "license", "name", "uri", "description"); - rz_iterator_foreach(iter, plugin) { + rz_iterator_foreach(iter, val) { + RzIOPlugin *plugin = *val; rz_core_io_plugin_print(plugin, state); } rz_iterator_free(iter); diff --git a/librz/core/cmd/cmd_egg.c b/librz/core/cmd/cmd_egg.c index 1cb8da78578..e62e2f2ac51 100644 --- a/librz/core/cmd/cmd_egg.c +++ b/librz/core/cmd/cmd_egg.c @@ -206,8 +206,9 @@ RZ_IPI RzCmdStatus rz_egg_list_plugins_handler(RzCore *core, int argc, const cha return RZ_CMD_STATUS_ERROR; } RzIterator *iter = ht_sp_as_iter(egg->plugins); - RzEggPlugin *p; - rz_iterator_foreach(iter, p) { + RzEggPlugin **val; + rz_iterator_foreach(iter, val) { + RzEggPlugin *p = *val; rz_cons_printf("%s %6s : %s\n", (p->type == RZ_EGG_PLUGIN_SHELLCODE) ? "shc" : "enc", p->name, p->desc); } diff --git a/librz/core/core.c b/librz/core/core.c index 987a9a47cf7..ac47f72c350 100644 --- a/librz/core/core.c +++ b/librz/core/core.c @@ -2461,13 +2461,14 @@ RZ_API RzCmdStatus rz_core_core_plugin_print(RzCorePlugin *cp, RzCmdStateOutput RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *state) { RzIterator *iter = ht_sp_as_iter(core->plugins); - RzCorePlugin *cp; + RzCorePlugin **val; RzCmdStatus status; if (!core) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, cp) { + rz_iterator_foreach(iter, val) { + RzCorePlugin *cp = *val; const char *license = cp->license ? cp->license : "???"; diff --git a/librz/core/cplugin.c b/librz/core/cplugin.c index 779f0d87d5f..5c8c57786af 100644 --- a/librz/core/cplugin.c +++ b/librz/core/cplugin.c @@ -14,8 +14,9 @@ RZ_API bool rz_core_plugin_fini(RzCore *core) { rz_return_val_if_fail(core->plugins, false); RzIterator *iter = ht_sp_as_iter(core->plugins); - RzCorePlugin *plugin; - rz_iterator_foreach(iter, plugin) { + RzCorePlugin **val; + rz_iterator_foreach(iter, val) { + RzCorePlugin *plugin = *val; if (plugin->fini) { plugin->fini(core); } diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index 1ae281fea23..80039076328 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -67,10 +67,11 @@ RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCr rz_return_val_if_fail(cry, NULL); RzIterator *it = ht_sp_as_iter(cry->plugins); - const RzCryptoPlugin *plugin; + RzCryptoPlugin **val; size_t i = 0; - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + const RzCryptoPlugin *plugin = *val; if (i == index) { return plugin; } @@ -160,11 +161,12 @@ RZ_API void rz_crypto_reset(RZ_NONNULL RzCrypto *cry) { RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) { rz_return_val_if_fail(cry && algo, false); RzIterator *it = ht_sp_as_iter(cry->plugins); - RzCryptoPlugin *h; + RzCryptoPlugin **val; if (cry->h && cry->h->fini && !cry->h->fini(cry)) { RZ_LOG_ERROR("[!] crypto: error terminating '%s' plugin\n", cry->h->name); } - rz_iterator_foreach(it, h) { + rz_iterator_foreach(it, val) { + RzCryptoPlugin *h = *val; rz_warn_if_fail(h && h->use); if (h && h->use(algo)) { if (h->init && !h->init(cry)) { diff --git a/librz/egg/egg.c b/librz/egg/egg.c index 64e90107269..cc87fbe54c8 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -532,10 +532,12 @@ RZ_API char *rz_egg_option_get(RzEgg *egg, const char *key) { } RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { - RzEggPlugin *p; + rz_return_val_if_fail(egg && name, false); RzIterator *iter = ht_sp_as_iter(egg->plugins); + RzEggPlugin **val; RzBuffer *b; - rz_iterator_foreach(iter, p) { + rz_iterator_foreach(iter, val) { + RzEggPlugin *p = *val; if (p->type == RZ_EGG_PLUGIN_SHELLCODE && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { @@ -553,10 +555,12 @@ RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { } RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { - RzEggPlugin *p; + rz_return_val_if_fail(egg && name, false); RzIterator *iter = ht_sp_as_iter(egg->plugins); + RzEggPlugin **val; RzBuffer *b; - rz_iterator_foreach(iter, p) { + rz_iterator_foreach(iter, val) { + RzEggPlugin *p = *val; if (p->type == RZ_EGG_PLUGIN_ENCODER && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { diff --git a/librz/hash/hash.c b/librz/hash/hash.c index 36cdeff8c79..ff021e620d3 100644 --- a/librz/hash/hash.c +++ b/librz/hash/hash.c @@ -308,9 +308,10 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha HashCfgConfig *mdc = NULL; RzIterator *it = ht_sp_as_iter(md->hash->plugins); - const RzHashPlugin *plugin; + const RzHashPlugin **val; - rz_iterator_foreach(it, plugin) { + rz_iterator_foreach(it, val) { + const RzHashPlugin *plugin = *val; if (is_all || !strcmp(plugin->name, name)) { mdc = hash_cfg_config_new(plugin); if (!mdc) { diff --git a/librz/io/io_plugin.c b/librz/io/io_plugin.c index 9ac77cf4056..1e9bd398189 100644 --- a/librz/io/io_plugin.c +++ b/librz/io/io_plugin.c @@ -57,9 +57,11 @@ RZ_API RzIOPlugin *rz_io_plugin_get_default(RzIO *io, const char *filename, bool } RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool many) { + rz_return_val_if_fail(io && filename, NULL); RzIterator *iter = ht_sp_as_iter(io->plugins); - RzIOPlugin *ret; - rz_iterator_foreach(iter, ret) { + RzIOPlugin **val; + rz_iterator_foreach(iter, val) { + RzIOPlugin *ret = *val; if (!ret || !ret->check) { continue; } @@ -72,9 +74,11 @@ RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool man } RZ_API RzIOPlugin *rz_io_plugin_byname(RzIO *io, const char *name) { + rz_return_val_if_fail(io && name, NULL); RzIterator *iter = ht_sp_as_iter(io->plugins); - RzIOPlugin *iop; - rz_iterator_foreach(iter, iop) { + RzIOPlugin **val; + rz_iterator_foreach(iter, val) { + RzIOPlugin *iop = *val; if (!strcmp(name, iop->name)) { return iop; } diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index 156a630b12b..759b51014c6 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -70,9 +70,10 @@ static int usage(int v) { static void list(RzEgg *egg) { RzIterator *iter = ht_sp_as_iter(egg->plugins); - RzEggPlugin *p; + RzEggPlugin **val; printf("shellcodes:\n"); - rz_iterator_foreach(iter, p) { + rz_iterator_foreach(iter, val) { + RzEggPlugin *p = *val; if (p->type == RZ_EGG_PLUGIN_SHELLCODE) { printf("%10s : %s\n", p->name, p->desc); } @@ -80,7 +81,8 @@ static void list(RzEgg *egg) { rz_iterator_free(iter); iter = ht_sp_as_iter(egg->plugins); printf("encoders:\n"); - rz_iterator_foreach(iter, p) { + rz_iterator_foreach(iter, val) { + RzEggPlugin *p = *val; if (p->type == RZ_EGG_PLUGIN_ENCODER) { printf("%10s : %s\n", p->name, p->desc); } diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index d8a6282a156..f0a02ff475f 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -130,9 +130,10 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { printf("flags algorithm license author\n"); - const RzHashPlugin *rmdp; RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - rz_iterator_foreach(it, rmdp) { + const RzHashPlugin **val; + rz_iterator_foreach(it, val) { + const RzHashPlugin *rmdp = *val; snprintf(flags, sizeof(flags), "____h%c", rmdp->support_hmac ? 'm' : '_'); printf("%6s %-14s %-10s %s\n", flags, rmdp->name, rmdp->license, rmdp->author); } @@ -806,13 +807,14 @@ static void hash_context_compare_hashes(RzHashContext *ctx, size_t filesize, boo static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { if (!strcmp(ctx->algorithm, "all")) { - const RzHashPlugin *plugin; RzList *list = rz_list_newf(NULL); if (!list) { return NULL; } RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - rz_iterator_foreach(it, plugin) { + RzHashPlugin **val; + rz_iterator_foreach(it, val) { + const RzHashPlugin *plugin = *val; rz_list_append(list, (void *)plugin->name); } rz_iterator_free(it); From c179cee134cd3e1d3c26c2d79282636458acb5e6 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 22 Oct 2024 11:23:18 -0500 Subject: [PATCH 03/11] Soften the NULL check. If arch is passed on from another struct, NULL is an allowed invalid value. --- librz/arch/asm.c | 10 +++++----- librz/include/rz_asm.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/librz/arch/asm.c b/librz/arch/asm.c index ff0a270a4b8..ae353812051 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -442,13 +442,13 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name * \return true Put Asm plugin successfully in use. * \return false Asm plugin failed to be enabled. */ -RZ_API bool rz_asm_use(RzAsm *a, const char *name) { - rz_return_val_if_fail(a && name, false); - RzIterator *iter = ht_sp_as_iter(a->plugins); - RzAsmPlugin **val; - if (!a || !name) { +RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) { + rz_return_val_if_fail(a, false); + if (!name) { return false; } + RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; RzCore *core = a->core; if (a->cur && !strcmp(a->cur->arch, name)) { return true; diff --git a/librz/include/rz_asm.h b/librz/include/rz_asm.h index d0fab182895..b29860b4147 100644 --- a/librz/include/rz_asm.h +++ b/librz/include/rz_asm.h @@ -159,7 +159,7 @@ RZ_API bool rz_asm_plugin_add(RzAsm *a, RZ_NONNULL RzAsmPlugin *foo); RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *foo); RZ_API bool rz_asm_setup(RzAsm *a, const char *arch, int bits, int big_endian); RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name); -RZ_API bool rz_asm_use(RzAsm *a, const char *name); +RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name); RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name); RZ_API bool rz_asm_set_arch(RzAsm *a, const char *name, int bits); RZ_DEPRECATE RZ_API int rz_asm_set_bits(RzAsm *a, int bits); From 5570189a4ea83932dbe72af8a812c4900cae4e88 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 22 Oct 2024 12:36:32 -0500 Subject: [PATCH 04/11] Fix some leaks --- librz/arch/asm.c | 8 ++++++-- librz/bin/bin.c | 4 ++-- librz/bp/bp_plugin.c | 1 + librz/core/cbin.c | 2 ++ librz/hash/hash.c | 3 +++ librz/io/io_plugin.c | 2 ++ 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/librz/arch/asm.c b/librz/arch/asm.c index ae353812051..fa4906d3355 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -375,14 +375,16 @@ RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { } RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) { - RzIterator *iter = ht_sp_as_iter(a->plugins); - RzAsmPlugin **val; if (!name || !*name) { return false; } + + RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; rz_iterator_foreach(iter, val) { RzAsmPlugin *h = *val; if (!strcmp(h->name, name)) { + rz_iterator_free(iter); return true; } } @@ -470,6 +472,7 @@ RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) { } if (h->init && !h->init(&a->plugin_data)) { RZ_LOG_ERROR("asm plugin '%s' failed to initialize.\n", h->name); + rz_iterator_free(iter); return false; } @@ -480,6 +483,7 @@ RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) { set_plugin_configs(core, h->name, h->get_config(a->plugin_data)); } a->cur = h; + rz_iterator_free(iter); return true; } } diff --git a/librz/bin/bin.c b/librz/bin/bin.c index 0f22404304e..ff946fe36b9 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -370,12 +370,11 @@ RZ_API RzBinPlugin *rz_bin_get_binplugin_by_buffer(RzBin *bin, RzBuffer *buf) { RzIterator *it = ht_sp_as_iter(bin->plugins); RzBinPlugin **val; - rz_return_val_if_fail(bin && buf, NULL); - rz_iterator_foreach(it, val) { RzBinPlugin *plugin = *val; if (plugin->check_buffer) { if (plugin->check_buffer(buf)) { + rz_iterator_free(it); return plugin; } } @@ -396,6 +395,7 @@ RZ_IPI RzBinPlugin *rz_bin_get_binplugin_by_filename(RzBin *bin) { RzBinPlugin *plugin = *val; if (plugin->check_filename) { if (plugin->check_filename(filename)) { + rz_iterator_free(it); return plugin; } } diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index 22806ce6954..9c2ba39fdaa 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -51,6 +51,7 @@ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { RzBreakpointPlugin *h = *val; if (!strcmp(h->name, name)) { bp->cur = h; + rz_iterator_free(iter); return true; } } diff --git a/librz/core/cbin.c b/librz/core/cbin.c index e3f379bdbb6..f1a9e74f5b0 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -4913,6 +4913,7 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state RzBinPlugin *bp = *bp_val; status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); return status; } } @@ -4922,6 +4923,7 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state RzBinXtrPlugin *bx = *bx_val; status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); return status; } } diff --git a/librz/hash/hash.c b/librz/hash/hash.c index ff021e620d3..4bd931192d1 100644 --- a/librz/hash/hash.c +++ b/librz/hash/hash.c @@ -315,16 +315,19 @@ RZ_API bool rz_hash_cfg_configure(RZ_NONNULL RzHashCfg *md, RZ_NONNULL const cha if (is_all || !strcmp(plugin->name, name)) { mdc = hash_cfg_config_new(plugin); if (!mdc) { + rz_iterator_free(it); return false; } if (!rz_list_append(md->configurations, mdc)) { RZ_LOG_ERROR("msg digest: cannot allocate memory for list entry.\n"); hash_cfg_config_free(mdc); + rz_iterator_free(it); return false; } if (!is_all) { + rz_iterator_free(it); return true; } } diff --git a/librz/io/io_plugin.c b/librz/io/io_plugin.c index 1e9bd398189..e068069b5a5 100644 --- a/librz/io/io_plugin.c +++ b/librz/io/io_plugin.c @@ -66,6 +66,7 @@ RZ_API RzIOPlugin *rz_io_plugin_resolve(RzIO *io, const char *filename, bool man continue; } if (ret->check(io, filename, many)) { + rz_iterator_free(iter); return ret; } } @@ -80,6 +81,7 @@ RZ_API RzIOPlugin *rz_io_plugin_byname(RzIO *io, const char *name) { rz_iterator_foreach(iter, val) { RzIOPlugin *iop = *val; if (!strcmp(name, iop->name)) { + rz_iterator_free(iter); return iop; } } From f274eaf8be97b75b5e5703e23cdcd4f28743f1ab Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 22 Oct 2024 13:32:21 -0500 Subject: [PATCH 05/11] Fix NULL checks and incorrect inits --- librz/arch/analysis.c | 42 ++++++++++++++++++++++-------------------- librz/arch/asm.c | 14 ++++++++------ librz/bin/bin.c | 2 +- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/librz/arch/analysis.c b/librz/arch/analysis.c index 8b3e295a840..1e5bc02c7bc 100644 --- a/librz/arch/analysis.c +++ b/librz/arch/analysis.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -211,32 +212,33 @@ RZ_API bool rz_analysis_plugin_del(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPl } RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) { + rz_return_val_if_fail(analysis && name, false); + if (analysis->cur && !strcmp(analysis->cur->name, name)) { + return true; + } + RzIterator *it = ht_sp_as_iter(analysis->plugins); RzAnalysisPlugin **val; - - if (analysis) { - if (analysis->cur && !strcmp(analysis->cur->name, name)) { - return true; + rz_iterator_foreach(it, val) { + RzAnalysisPlugin *h = *val; + if (!h || !h->name || strcmp(h->name, name)) { + continue; } - rz_iterator_foreach(it, val) { - RzAnalysisPlugin *h = *val; - if (!h || !h->name || strcmp(h->name, name)) { - continue; - } - plugin_fini(analysis); - analysis->cur = h; - if (h->init && !h->init(&analysis->plugin_data)) { - RZ_LOG_ERROR("analysis plugin '%s' failed to initialize.\n", h->name); - return false; - } - rz_analysis_set_reg_profile(analysis); - if (analysis->il_vm) { - rz_analysis_il_vm_setup(analysis); - } - return true; + plugin_fini(analysis); + analysis->cur = h; + if (h->init && !h->init(&analysis->plugin_data)) { + RZ_LOG_ERROR("analysis plugin '%s' failed to initialize.\n", h->name); + rz_iterator_free(it); + return false; + } + rz_analysis_set_reg_profile(analysis); + if (analysis->il_vm) { + rz_analysis_il_vm_setup(analysis); } rz_iterator_free(it); + return true; } + rz_iterator_free(it); return false; } diff --git a/librz/arch/asm.c b/librz/arch/asm.c index fa4906d3355..1f73088066a 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -393,17 +393,19 @@ RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) { } RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) { - RzAsmPlugin *h; - RzIterator *iter = ht_sp_as_iter(a->plugins); if (!a) { return false; } if (!(name && *name)) { a->acur = NULL; } - rz_iterator_foreach(iter, h) { + RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; + rz_iterator_foreach(iter, val) { + RzAsmPlugin *h = *val; if (h->assemble && !strcmp(h->name, name)) { a->acur = h; + rz_iterator_free(iter); return true; } } @@ -449,12 +451,12 @@ RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) { if (!name) { return false; } - RzIterator *iter = ht_sp_as_iter(a->plugins); - RzAsmPlugin **val; - RzCore *core = a->core; if (a->cur && !strcmp(a->cur->arch, name)) { return true; } + RzIterator *iter = ht_sp_as_iter(a->plugins); + RzAsmPlugin **val; + RzCore *core = a->core; rz_iterator_foreach(iter, val) { RzAsmPlugin *h = *val; if (h->arch && h->name && !strcmp(h->name, name)) { diff --git a/librz/bin/bin.c b/librz/bin/bin.c index ff946fe36b9..c9fb129a18c 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -768,7 +768,7 @@ RZ_API RzBin *rz_bin_new(void) { /* extractors */ bin->binxtrs = ht_sp_new(HT_STR_DUP, NULL, NULL); for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_xtr_static_plugins); ++i) { - ht_sp_insert(bin->plugins, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i]); + ht_sp_insert(bin->binxtrs, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i]); } return bin; From d3c5c76d50f8ea5f3f9f3dfbe95da2c6c0b8ea17 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 05:47:18 -0500 Subject: [PATCH 06/11] Fix more leaks --- librz/core/casm.c | 6 +++++- librz/crypto/crypto.c | 3 +++ librz/main/rz-hash.c | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/librz/core/casm.c b/librz/core/casm.c index bca80cee470..864913f3c1f 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -65,9 +65,9 @@ RZ_API char *rz_core_asm_search(RzCore *core, const char *input) { } static const char *has_esil(RzCore *core, const char *name) { + rz_return_val_if_fail(core && core->analysis && name, NULL); RzIterator *iter = ht_sp_as_iter(core->analysis->plugins); RzAnalysisPlugin **val; - rz_return_val_if_fail(core && core->analysis && name, NULL); rz_iterator_foreach(iter, val) { RzAnalysisPlugin *h = *val; if (!h->name || strcmp(name, h->name)) { @@ -75,15 +75,19 @@ static const char *has_esil(RzCore *core, const char *name) { } if (h->il_config && h->esil) { // Analysis with RzIL and ESIL + rz_iterator_free(iter); return "AeI"; } else if (h->il_config) { // Analysis with RzIL + rz_iterator_free(iter); return "A_I"; } else if (h->esil) { // Analysis with ESIL + rz_iterator_free(iter); return "Ae_"; } // Only the analysis plugin. + rz_iterator_free(iter); return "A__"; } rz_iterator_free(iter); diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index 80039076328..fc104d51db6 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -73,6 +73,7 @@ RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCr rz_iterator_foreach(it, val) { const RzCryptoPlugin *plugin = *val; if (i == index) { + rz_iterator_free(it); return plugin; } i++; @@ -171,10 +172,12 @@ RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) if (h && h->use(algo)) { if (h->init && !h->init(cry)) { RZ_LOG_ERROR("[!] crypto: error initializing '%s' plugin\n", cry->h->name); + rz_iterator_free(it); return false; } cry->h = h; + rz_iterator_free(it); return true; } } diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index f0a02ff475f..25c541f8fb1 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -509,6 +509,7 @@ static void hash_parse_cmdline(int argc, const char **argv, RzHashContext *ctx) } static void hash_context_fini(RzHashContext *ctx) { + free(ctx->key.buf); free(ctx->algorithm); free(ctx->compare); free(ctx->iv); From 2928d6ae698fdcd7e5ef5399724f56dc0f84dffc Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 06:01:08 -0500 Subject: [PATCH 07/11] Add warning if plugin with same name was added before and is overwritten. --- librz/arch/analysis.c | 4 +++- librz/arch/asm.c | 8 ++++++-- librz/bin/bin.c | 16 ++++++++++++---- librz/bp/bp_plugin.c | 4 +++- librz/core/cplugin.c | 4 +++- librz/crypto/crypto.c | 8 ++++++-- librz/debug/plugin.c | 4 +++- librz/egg/egg.c | 4 +++- librz/hash/hash.c | 4 +++- librz/io/io_plugin.c | 4 +++- 10 files changed, 45 insertions(+), 15 deletions(-) diff --git a/librz/arch/analysis.c b/librz/arch/analysis.c index 1e5bc02c7bc..70dda618a62 100644 --- a/librz/arch/analysis.c +++ b/librz/arch/analysis.c @@ -198,7 +198,9 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) { RZ_API bool rz_analysis_plugin_add(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPlugin *p) { rz_return_val_if_fail(analysis && p, false); - ht_sp_insert(analysis->plugins, p->name, p); + if (!ht_sp_insert(analysis->plugins, p->name, p)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", p->name); + } return true; } diff --git a/librz/arch/asm.c b/librz/arch/asm.c index 1f73088066a..6310e3b5471 100644 --- a/librz/arch/asm.c +++ b/librz/arch/asm.c @@ -358,7 +358,9 @@ RZ_API bool rz_asm_plugin_add(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) { if (rz_asm_is_valid(a, p->name)) { return false; } - ht_sp_insert(a->plugins, p->name, p); + if (!ht_sp_insert(a->plugins, p->name, p)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", p->name); + } return true; } @@ -423,7 +425,9 @@ RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) { static void set_plugin_configs(RZ_BORROW RzCore *core, const char *plugin_name, RZ_OWN RzConfig *pcfg) { rz_return_if_fail(pcfg && core); rz_config_lock(pcfg, 1); - ht_sp_insert(core->plugin_configs, plugin_name, pcfg); + if (!ht_sp_insert(core->plugin_configs, plugin_name, pcfg)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin_name); + } } /** diff --git a/librz/bin/bin.c b/librz/bin/bin.c index c9fb129a18c..595bf7b774f 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -417,7 +417,9 @@ RZ_IPI RzBinXtrPlugin *rz_bin_get_xtrplugin_by_name(RzBin *bin, const char *name RZ_API bool rz_bin_plugin_add(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - ht_sp_insert(bin->plugins, plugin->name, plugin); + if (!ht_sp_insert(bin->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } @@ -438,7 +440,9 @@ RZ_API bool rz_bin_plugin_del(RzBin *bin, RZ_NONNULL RzBinPlugin *plugin) { RZ_API bool rz_bin_xtr_plugin_add(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) { rz_return_val_if_fail(bin && plugin, false); - ht_sp_insert(bin->binxtrs, plugin->name, plugin); + if (!ht_sp_insert(bin->binxtrs, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } if (plugin->init) { plugin->init(bin->user); } @@ -763,12 +767,16 @@ RZ_API RzBin *rz_bin_new(void) { bin->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_static_plugins); ++i) { - ht_sp_insert(bin->plugins, bin_static_plugins[i]->name, bin_static_plugins[i]); + if (!ht_sp_insert(bin->plugins, bin_static_plugins[i]->name, bin_static_plugins[i])) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", bin_static_plugins[i]->name); + } } /* extractors */ bin->binxtrs = ht_sp_new(HT_STR_DUP, NULL, NULL); for (size_t i = 0; i < RZ_ARRAY_SIZE(bin_xtr_static_plugins); ++i) { - ht_sp_insert(bin->binxtrs, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i]); + if (!ht_sp_insert(bin->binxtrs, bin_xtr_static_plugins[i]->name, bin_xtr_static_plugins[i])) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", bin_xtr_static_plugins[i]->name); + } } return bin; diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index 9c2ba39fdaa..62e3109ebe6 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -24,7 +24,9 @@ RZ_API int rz_bp_plugin_del_byname(RzBreakpoint *bp, RZ_NONNULL const char *name RZ_API bool rz_bp_plugin_add(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin) { rz_return_val_if_fail(bp && plugin, false); - ht_sp_insert(bp->plugins, plugin->name, plugin); + if (!ht_sp_insert(bp->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } diff --git a/librz/core/cplugin.c b/librz/core/cplugin.c index 5c8c57786af..d1c9b6bb450 100644 --- a/librz/core/cplugin.c +++ b/librz/core/cplugin.c @@ -32,7 +32,9 @@ RZ_API bool rz_core_plugin_add(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) { rz_return_val_if_fail(core, false); rz_return_val_if_fail(plugin && plugin->init && plugin->name && plugin->author && plugin->license, false); // TODO: Add config from core plugin. - ht_sp_insert(core->plugins, plugin->name, plugin); + if (!ht_sp_insert(core->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } if (!plugin->init(core)) { ht_sp_delete(core->plugins, plugin->name); return false; diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index fc104d51db6..29317bc6814 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -84,7 +84,9 @@ RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCr RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) { rz_return_val_if_fail(cry && plugin, false); - ht_sp_insert(cry->plugins, plugin->name, plugin); + if (!ht_sp_insert(cry->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } @@ -112,7 +114,9 @@ RZ_API RZ_OWN RzCrypto *rz_crypto_new(void) { cry->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL); for (size_t i = 0; i < RZ_ARRAY_SIZE(crypto_static_plugins); ++i) { - ht_sp_insert(cry->plugins, crypto_static_plugins[i]->name, crypto_static_plugins[i]); + if (!ht_sp_insert(cry->plugins, crypto_static_plugins[i]->name, crypto_static_plugins[i])) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", crypto_static_plugins[i]->name); + } } if (!cry->plugins) { goto rz_crypto_new_bad; diff --git a/librz/debug/plugin.c b/librz/debug/plugin.c index 15b19a3c1ee..ef4c924a4d3 100644 --- a/librz/debug/plugin.c +++ b/librz/debug/plugin.c @@ -52,7 +52,9 @@ RZ_API bool rz_debug_use(RzDebug *dbg, const char *name) { RZ_API bool rz_debug_plugin_add(RzDebug *dbg, RZ_NONNULL RzDebugPlugin *plugin) { rz_return_val_if_fail(dbg && plugin && plugin->name, false); - ht_sp_insert(dbg->plugins, plugin->name, plugin); + if (!ht_sp_insert(dbg->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } diff --git a/librz/egg/egg.c b/librz/egg/egg.c index cc87fbe54c8..fefccd0a00a 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -92,7 +92,9 @@ RZ_API RzEgg *rz_egg_new(void) { RZ_API bool rz_egg_plugin_add(RzEgg *a, RZ_NONNULL RzEggPlugin *plugin) { rz_return_val_if_fail(a && plugin && plugin->name, false); - ht_sp_insert(a->plugins, plugin->name, plugin); + if (!ht_sp_insert(a->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } diff --git a/librz/hash/hash.c b/librz/hash/hash.c index 4bd931192d1..27c18f61e52 100644 --- a/librz/hash/hash.c +++ b/librz/hash/hash.c @@ -718,7 +718,9 @@ RZ_API void rz_hash_free(RZ_NULLABLE RzHash *rh) { */ RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin) { rz_return_val_if_fail(rh && plugin && plugin->name, false); - ht_sp_insert(rh->plugins, plugin->name, plugin); + if (!ht_sp_insert(rh->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } diff --git a/librz/io/io_plugin.c b/librz/io/io_plugin.c index e068069b5a5..334394c4f1f 100644 --- a/librz/io/io_plugin.c +++ b/librz/io/io_plugin.c @@ -15,7 +15,9 @@ static RzIOPlugin *io_static_plugins[] = { RZ_IO_STATIC_PLUGINS }; RZ_API bool rz_io_plugin_add(RzIO *io, RZ_NONNULL RZ_BORROW RzIOPlugin *plugin) { rz_return_val_if_fail(io && plugin && plugin->name, false); - ht_sp_insert(io->plugins, plugin->name, plugin); + if (!ht_sp_insert(io->plugins, plugin->name, plugin)) { + RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin->name); + } return true; } From ef67490a307cbb093ffcf676b6cd0f1b22e0af1e Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 11:07:59 -0500 Subject: [PATCH 08/11] Document rz_str_cmp --- librz/include/rz_util/rz_str.h | 2 +- librz/util/str.c | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/librz/include/rz_util/rz_str.h b/librz/include/rz_util/rz_str.h index 5133aed15ff..a70a4fbd88d 100644 --- a/librz/include/rz_util/rz_str.h +++ b/librz/include/rz_util/rz_str.h @@ -183,7 +183,7 @@ RZ_API RZ_OWN char *rz_sub_str_ptr(RZ_NONNULL const char *str, RZ_NONNULL const RZ_API char *rz_str_ichr(char *str, char chr); RZ_API bool rz_str_ccmp(const char *dst, const char *orig, int ch); RZ_API bool rz_str_cmp_list(const char *list, const char *item, char sep); -RZ_API int rz_str_cmp(const char *dst, const char *orig, int len); +RZ_API int rz_str_cmp(RZ_NULLABLE const char *dst, RZ_NULLABLE const char *orig, int len); RZ_API int rz_str_casecmp(const char *dst, const char *orig); RZ_API int rz_str_ncasecmp(const char *dst, const char *orig, size_t n); RZ_API int rz_str_ccpy(char *dst, char *orig, int ch); diff --git a/librz/util/str.c b/librz/util/str.c index 1dc71513aff..6db98616cf3 100644 --- a/librz/util/str.c +++ b/librz/util/str.c @@ -1017,8 +1017,18 @@ RZ_API bool rz_str_cmp_list(const char *list, const char *item, char sep) { return true; } -// like strncmp, but checking for null pointers -RZ_API int rz_str_cmp(const char *a, const char *b, int len) { +/** + * \brief Like strncmp, but checking for null pointers. + * + * \param a String a. + * \param b String b. + * \param len The number of characters to compare. If -1 it compares until the end. + * + * \return Negative value, if a < b + * \return 0, if a == b. + * \return Positive value, if a > b + */ +RZ_API int rz_str_cmp(RZ_NULLABLE const char *a, RZ_NULLABLE const char *b, int len) { if ((a == b) || (!a && !b)) { return 0; } From 200035bab3c5bfb5b0ad8de1a079771d7c202486 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 12:09:01 -0500 Subject: [PATCH 09/11] Add function to create new list from iterator. --- .clang-format | 4 ++-- librz/include/rz_list.h | 5 +++++ librz/util/list.c | 18 ++++++++++++++++++ test/unit/test_list.c | 26 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/.clang-format b/.clang-format index dd5e08d0662..6a23028c113 100644 --- a/.clang-format +++ b/.clang-format @@ -22,7 +22,7 @@ AlignConsecutiveMacros: true AlignTrailingComments: false AlignOperands: false Cpp11BracedListStyle: false -ForEachMacros: ['rz_list_foreach', 'rz_list_foreach_safe', 'rz_vector_foreach', 'rz_vector_foreach_prev', 'rz_vector_enumerate', 'rz_pvector_foreach', 'rz_pvector_enumerate', 'rz_rbtree_foreach', 'rz_interval_tree_foreach', 'rz_skiplist_foreach', 'graph_foreach_anode'] +ForEachMacros: ['rz_list_foreach', 'rz_list_foreach_enum', 'rz_list_foreach_safe', 'rz_vector_foreach', 'rz_vector_foreach_prev', 'rz_vector_enumerate', 'rz_pvector_foreach', 'rz_pvector_enumerate', 'rz_rbtree_foreach', 'rz_interval_tree_foreach', 'rz_skiplist_foreach', 'graph_foreach_anode'] SortIncludes: false RequiresClausePosition: SingleLine -TypenameMacros: ['HT_', 'Ht_', 'HtName_'] \ No newline at end of file +TypenameMacros: ['HT_', 'Ht_', 'HtName_'] diff --git a/librz/include/rz_list.h b/librz/include/rz_list.h index cff7d6d820b..18ccd8e3dc2 100644 --- a/librz/include/rz_list.h +++ b/librz/include/rz_list.h @@ -1,6 +1,7 @@ #ifndef RZ_LIST_H #define RZ_LIST_H +#include #include #ifdef __cplusplus @@ -33,6 +34,9 @@ typedef int (*RzListComparator)(const void *value, const void *list_data, void * #define rz_list_foreach(list, it, pos) \ if (list) \ for (it = list->head; it && (pos = it->elem, 1); it = it->next) +#define rz_list_foreach_enum(list, it, pos, i) \ + if (list) \ + for (it = list->head, i = 0; it && (pos = it->elem, 1); it = it->next, ++i) #define rz_list_foreach_iter(iter, it, pos) \ for (it = iter; it && (pos = it->elem, 1); it = it->next) /* Safe when calling rz_list_delete() while iterating over the list. */ @@ -62,6 +66,7 @@ typedef int (*RzListComparator)(const void *value, const void *list_data, void * RZ_API RZ_OWN RzList *rz_list_new(void); RZ_API RZ_OWN RzList *rz_list_newf(RZ_NULLABLE RzListFree f); RZ_API RZ_OWN RzList *rz_list_new_from_array(RZ_NONNULL const void **arr, size_t arr_size); +RZ_API RZ_OWN RzList *rz_list_new_from_iterator(RZ_BORROW RZ_NONNULL RzIterator *iter); RZ_API RZ_BORROW RzListIter *rz_list_iter_get_prev(RZ_NONNULL RzListIter *iter); RZ_API RZ_BORROW RzListIter *rz_list_iter_get_next(RZ_NONNULL RzListIter *iter); RZ_API RZ_BORROW void *rz_list_iter_get_prev_data(RZ_NONNULL RzListIter *iter); diff --git a/librz/util/list.c b/librz/util/list.c index 1e03892c5de..19846e074a5 100644 --- a/librz/util/list.c +++ b/librz/util/list.c @@ -296,6 +296,24 @@ RZ_API RZ_OWN RzList *rz_list_new_from_array(RZ_NONNULL const void **arr, size_t return l; } +/** + * \brief Allocates a new RzList and adds all elements of the iterator \p iter to it. + * \p iter keeps the ownership over the values. + * + * \return The produced list. Or NULL in case of failure. + **/ +RZ_API RZ_OWN RzList *rz_list_new_from_iterator(RZ_BORROW RZ_NONNULL RzIterator *iter) { + RzList *l = rz_list_new(); + if (!l) { + return NULL; + } + void **val; + rz_iterator_foreach(iter, val) { + rz_list_append(l, (void *)*val); + } + return l; +} + /** * \brief Creates a RzListIter element that can be inserted into a RzList * diff --git a/test/unit/test_list.c b/test/unit/test_list.c index 4ed964a7af0..92fdfd8a8e7 100644 --- a/test/unit/test_list.c +++ b/test/unit/test_list.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only #include +#include #include "minunit.h" #define BUF_LENGTH 100 @@ -218,6 +219,30 @@ bool test_rz_list_sort5(void) { mu_end; } +bool test_rz_list_from_iter(void) { + HtUP *alpha_ht = ht_up_new(NULL, NULL); + char *unordered_alphabeth[] = { "b", "w", "k", "a", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z" }; + char *lower[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; + + for (size_t i = 0; i < 26; i++) { + ht_up_insert(alpha_ht, i, (void *)unordered_alphabeth[i]); + } + RzIterator *iter = ht_up_as_iter(alpha_ht); + RzList *list = rz_list_new_from_iterator(iter); + rz_list_sort(list, (RzListComparator)strcmp, NULL); + mu_assert_eq(rz_list_length(list), 26, "Number of elements are off"); + RzListIter *it; + const char *elem; + size_t i = 0; + rz_list_foreach_enum(list, it, elem, i) { + mu_assert_streq(elem, lower[i], "Value mismatched."); + } + ht_up_free(alpha_ht); + rz_iterator_free(iter); + rz_list_free(list); + mu_end; +} + // 3-valued comparator -> {LT,EQ,GT}. static int pintcmp(int *a, int *b, void *user) { return (int)(*a > *b) - (int)(*b > *a); @@ -501,6 +526,7 @@ int all_tests() { mu_run_test(test_rz_list_reverse); mu_run_test(test_rz_list_clone); mu_run_test(test_rz_list_find_ptr); + mu_run_test(test_rz_list_from_iter); return tests_passed != tests_run; } From db593ddf36c7f9f5644eb813f895c98a19c041de Mon Sep 17 00:00:00 2001 From: Rot127 Date: Wed, 23 Oct 2024 12:46:51 -0500 Subject: [PATCH 10/11] Sort plugins alphabetically when listed. --- librz/bp/bp_plugin.c | 9 +- librz/core/casm.c | 13 +- librz/core/cbin.c | 23 ++- librz/core/ccrypto.c | 16 +- librz/core/cdebug.c | 13 +- librz/core/cio.c | 12 +- librz/include/rz_asm.h | 14 ++ librz/include/rz_bin.h | 28 ++++ librz/include/rz_bp.h | 14 ++ librz/include/rz_crypto.h | 15 ++ librz/include/rz_debug.h | 17 +++ librz/include/rz_egg.h | 14 ++ librz/include/rz_hash.h | 15 ++ librz/include/rz_io.h | 14 ++ librz/main/rz-gg.c | 17 ++- librz/main/rz-hash.c | 45 +++--- test/db/archos/linux-x64/cmd_list | 16 +- test/db/archos/linux-x64/dbg_dl | 10 +- test/db/cmd/cmd_list | 233 +++++++++++++++--------------- test/db/tools/rz_hash | 174 +++++++++++----------- test/unit/test_list.c | 2 +- 21 files changed, 441 insertions(+), 273 deletions(-) diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index 62e3109ebe6..95141b8cea4 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -64,12 +64,15 @@ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { // TODO: deprecate RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { RzIterator *iter = ht_sp_as_iter(bp->plugins); - RzBreakpointPlugin **val; - rz_iterator_foreach(iter, val) { - RzBreakpointPlugin *b = *val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_breakpoint_plugin_cmp, NULL); + RzListIter *it; + RzBreakpointPlugin *b; + rz_list_foreach (plugin_list, it, b) { bp->cb_printf("bp %c %s\n", (bp->cur && !strcmp(bp->cur->name, b->name)) ? '*' : '-', b->name); } + rz_list_free(plugin_list); rz_iterator_free(iter); } diff --git a/librz/core/casm.c b/librz/core/casm.c index 864913f3c1f..a108576342d 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -169,11 +169,13 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC int i; RzAsm *a = core->rasm; RzIterator *iter = ht_sp_as_iter(a->plugins); - RzAsmPlugin **val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_asm_plugin_cmp, NULL); + RzListIter *it; + RzAsmPlugin *ap; RzCmdStatus status; if (arch) { - rz_iterator_foreach(iter, val) { - RzAsmPlugin *ap = *val; + rz_list_foreach (plugin_list, it, ap) { if (ap->cpus && !strcmp(arch, ap->name)) { char *c = rz_str_dup(ap->cpus); int n = rz_str_split(c, ','); @@ -186,19 +188,20 @@ RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzC } } else { rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, val) { - RzAsmPlugin *ap = *val; + rz_list_foreach (plugin_list, it, ap) { const char *license = ap->license ? ap->license : "unknown"; status = rz_core_asm_plugin_print(core, ap, arch, state, license); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } } rz_cmd_state_output_array_end(state); } + rz_list_free(plugin_list); rz_iterator_free(iter); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cbin.c b/librz/core/cbin.c index f1a9e74f5b0..98465ba4910 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -4904,29 +4904,38 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state rz_return_val_if_fail(bin && state, RZ_CMD_STATUS_ERROR); RzCmdStatus status; + rz_cmd_state_output_array_start(state); + RzIterator *iter = ht_sp_as_iter(bin->plugins); - RzBinPlugin **bp_val; - RzBinXtrPlugin **bx_val; + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_bin_plugin_cmp, NULL); + RzListIter *it; + RzBinPlugin *bp; + RzBinXtrPlugin *bx; - rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, bp_val) { - RzBinPlugin *bp = *bp_val; + rz_list_foreach (plugin_list, it, bp) { status = rz_core_bin_plugin_print(bp, state); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } } + rz_list_free(plugin_list); rz_iterator_free(iter); + iter = ht_sp_as_iter(bin->binxtrs); - rz_iterator_foreach(iter, bx_val) { - RzBinXtrPlugin *bx = *bx_val; + plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_bin_xtr_plugin_cmp, NULL); + rz_list_foreach (plugin_list, it, bx) { status = rz_core_binxtr_plugin_print(bx, state); if (status != RZ_CMD_STATUS_OK) { rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } } + rz_list_free(plugin_list); rz_iterator_free(iter); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index 80b7358016c..8fad02e20ff 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -36,20 +36,26 @@ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput rz_return_val_if_fail(cry, RZ_CMD_STATUS_ERROR); RzCmdStatus status; - RzIterator *it = ht_sp_as_iter(cry->plugins); - RzCryptoPlugin **val; rz_cmd_state_output_array_start(state); if (state->mode == RZ_OUTPUT_MODE_STANDARD) { rz_cons_println("algorithm license author"); } - rz_iterator_foreach(it, val) { - const RzCryptoPlugin *plugin = *val; + RzIterator *iter = ht_sp_as_iter(cry->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_crypto_plugin_cmp, NULL); + RzListIter *it; + RzCryptoPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { status = core_crypto_plugin_print(state, plugin); if (status != RZ_CMD_STATUS_OK) { + rz_list_free(plugin_list); + rz_iterator_free(iter); return status; } } - rz_iterator_free(it); + rz_list_free(plugin_list); + rz_iterator_free(iter); + if (state->mode == RZ_OUTPUT_MODE_QUIET) { rz_cons_newline(); } diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index 9f02183e9f1..b159b3d2ada 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -325,25 +325,30 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s memset(spaces, ' ', 15); spaces[15] = 0; RzDebug *dbg = core->dbg; - RzIterator *iter = ht_sp_as_iter(dbg->plugins); - RzDebugPlugin **val; RzCmdStatus status; if (!dbg) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); - rz_iterator_foreach(iter, val) { - RzDebugPlugin *plugin = *val; + RzIterator *iter = ht_sp_as_iter(dbg->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_debug_plugin_cmp, NULL); + RzListIter *it; + RzDebugPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { int sp = 8 - strlen(plugin->name); spaces[sp] = 0; status = rz_core_debug_plugin_print(dbg, plugin, state, count, spaces); if (status != RZ_CMD_STATUS_OK) { + rz_iterator_free(iter); + rz_list_free(plugin_list); return status; } spaces[sp] = ' '; count++; } rz_iterator_free(iter); + rz_list_free(plugin_list); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/core/cio.c b/librz/core/cio.c index 4fc7cf85c48..e92b8dd3171 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -480,19 +480,23 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput */ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { rz_return_val_if_fail(io && state, RZ_CMD_STATUS_ERROR); - RzIterator *iter = ht_sp_as_iter(io->plugins); - RzIOPlugin **val; if (!io) { return RZ_CMD_STATUS_ERROR; } rz_cmd_state_output_array_start(state); rz_cmd_state_output_set_columnsf(state, "sssss", "perm", "license", "name", "uri", "description"); - rz_iterator_foreach(iter, val) { - RzIOPlugin *plugin = *val; + + RzIterator *iter = ht_sp_as_iter(io->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_io_plugin_cmp, NULL); + RzListIter *it; + RzIOPlugin *plugin; + rz_list_foreach (plugin_list, it, plugin) { rz_core_io_plugin_print(plugin, state); } rz_iterator_free(iter); + rz_list_free(plugin_list); rz_cmd_state_output_array_end(state); return RZ_CMD_STATUS_OK; } diff --git a/librz/include/rz_asm.h b/librz/include/rz_asm.h index b29860b4147..821c9d2c71b 100644 --- a/librz/include/rz_asm.h +++ b/librz/include/rz_asm.h @@ -149,6 +149,20 @@ typedef struct rz_asm_plugin_t { const char *platforms; } RzAsmPlugin; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_asm_plugin_cmp(RZ_NULLABLE const RzAsmPlugin *a, RZ_NULLABLE const RzAsmPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API /* asm.c */ RZ_API RzAsm *rz_asm_new(void); diff --git a/librz/include/rz_bin.h b/librz/include/rz_bin.h index 829dcce4a2f..da9b9da0aa7 100644 --- a/librz/include/rz_bin.h +++ b/librz/include/rz_bin.h @@ -770,6 +770,34 @@ typedef struct rz_bin_bind_t { ut32 visibility; } RzBinBind; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_bin_plugin_cmp(RZ_NULLABLE const RzBinPlugin *a, RZ_NULLABLE const RzBinPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_bin_xtr_plugin_cmp(RZ_NULLABLE const RzBinXtrPlugin *a, RZ_NULLABLE const RzBinXtrPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + RZ_API RzBinField *rz_bin_field_new(ut64 paddr, ut64 vaddr, int size, const char *name, const char *comment, const char *format, bool format_named); RZ_API void rz_bin_field_free(RZ_NULLABLE RzBinField *field); RZ_API RzBinClassField *rz_bin_class_field_new(ut64 vaddr, ut64 paddr, const char *name, const char *classname, const char *libname, const char *type); diff --git a/librz/include/rz_bp.h b/librz/include/rz_bp.h index 3d2fff72c21..25ce4fff2c3 100644 --- a/librz/include/rz_bp.h +++ b/librz/include/rz_bp.h @@ -106,6 +106,20 @@ typedef struct rz_bp_trace_t { int bitlen; } RzBreakpointTrace; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_breakpoint_plugin_cmp(RZ_NULLABLE const RzBreakpointPlugin *a, RZ_NULLABLE const RzBreakpointPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx); RZ_API RzBreakpoint *rz_bp_free(RzBreakpoint *bp); diff --git a/librz/include/rz_crypto.h b/librz/include/rz_crypto.h index ee33e68cbc1..d174c121145 100644 --- a/librz/include/rz_crypto.h +++ b/librz/include/rz_crypto.h @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -53,6 +54,20 @@ typedef struct rz_crypto_plugin_t { typedef ut64 RzCryptoSelector; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_crypto_plugin_cmp(RZ_NULLABLE const RzCryptoPlugin *a, RZ_NULLABLE const RzCryptoPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h); RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h); diff --git a/librz/include/rz_debug.h b/librz/include/rz_debug.h index ebc464aaca3..dc888ceb8ac 100644 --- a/librz/include/rz_debug.h +++ b/librz/include/rz_debug.h @@ -15,6 +15,8 @@ #include #include "rz_bind.h" +#include "rz_util/rz_assert.h" +#include "rz_util/rz_str.h" #ifdef __cplusplus extern "C" { #endif @@ -430,6 +432,21 @@ typedef struct rz_debug_esil_watchpoint_t { } RzDebugEsilWatchpoint; #ifdef RZ_API + +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_debug_plugin_cmp(RZ_NULLABLE const RzDebugPlugin *a, RZ_NULLABLE const RzDebugPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + RZ_API RZ_OWN RzDebug *rz_debug_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *bp_ctx); RZ_API RzDebug *rz_debug_free(RzDebug *dbg); diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index bfd5a89e915..e452040ed51 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -165,6 +165,20 @@ typedef struct rz_egg_emit_t { void (*get_while_end)(RzEgg *egg, char *out, const char *ctxpush, const char *label); } RzEggEmit; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_egg_plugin_cmp(RZ_NULLABLE const RzEggPlugin *a, RZ_NULLABLE const RzEggPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzEgg *rz_egg_new(void); RZ_API void rz_egg_lang_init(RzEgg *egg); diff --git a/librz/include/rz_hash.h b/librz/include/rz_hash.h index bab1b12fd50..eecb9ac8cd7 100644 --- a/librz/include/rz_hash.h +++ b/librz/include/rz_hash.h @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -49,6 +50,20 @@ typedef struct rz_hash_cfg_t { RzHash *hash; } RzHashCfg; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_hash_plugin_cmp(RZ_NULLABLE const RzHashPlugin *a, RZ_NULLABLE const RzHashPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + #ifdef RZ_API RZ_API RzHash *rz_hash_new(void); diff --git a/librz/include/rz_io.h b/librz/include/rz_io.h index f27d570d39c..1f485ccaccd 100644 --- a/librz/include/rz_io.h +++ b/librz/include/rz_io.h @@ -270,6 +270,20 @@ typedef struct rz_io_bind_t { #endif } RzIOBind; +/** + * \brief Compare plugins by name (via strcmp). + */ +static inline int rz_io_plugin_cmp(RZ_NULLABLE const RzIOPlugin *a, RZ_NULLABLE const RzIOPlugin *b) { + if (!a && !b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } + return rz_str_cmp(a->name, b->name, -1); +} + // map.c RZ_API RzIOMap *rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size); RZ_API void rz_io_map_init(RzIO *io); diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index 759b51014c6..5db9beb04a9 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -69,24 +69,25 @@ static int usage(int v) { } static void list(RzEgg *egg) { - RzIterator *iter = ht_sp_as_iter(egg->plugins); - RzEggPlugin **val; printf("shellcodes:\n"); - rz_iterator_foreach(iter, val) { - RzEggPlugin *p = *val; + RzIterator *iter = ht_sp_as_iter(egg->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_egg_plugin_cmp, NULL); + RzListIter *it; + RzEggPlugin *p; + rz_list_foreach (plugin_list, it, p) { if (p->type == RZ_EGG_PLUGIN_SHELLCODE) { printf("%10s : %s\n", p->name, p->desc); } } - rz_iterator_free(iter); - iter = ht_sp_as_iter(egg->plugins); + printf("encoders:\n"); - rz_iterator_foreach(iter, val) { - RzEggPlugin *p = *val; + rz_list_foreach (plugin_list, it, p) { if (p->type == RZ_EGG_PLUGIN_ENCODER) { printf("%10s : %s\n", p->name, p->desc); } } + rz_list_free(plugin_list); rz_iterator_free(iter); } diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index 25c541f8fb1..1a5afd6529b 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -130,14 +130,17 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { printf("flags algorithm license author\n"); - RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - const RzHashPlugin **val; - rz_iterator_foreach(it, val) { - const RzHashPlugin *rmdp = *val; + RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); + RzListIter *it; + const RzHashPlugin *rmdp; + rz_list_foreach (plugin_list, it, rmdp) { snprintf(flags, sizeof(flags), "____h%c", rmdp->support_hmac ? 'm' : '_'); printf("%6s %-14s %-10s %s\n", flags, rmdp->name, rmdp->license, rmdp->author); } - rz_iterator_free(it); + rz_list_free(plugin_list); + rz_iterator_free(iter); const RzCryptoPlugin *rcp; for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(ctx->rc, i)); i++) { @@ -807,21 +810,25 @@ static void hash_context_compare_hashes(RzHashContext *ctx, size_t filesize, boo } static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { - if (!strcmp(ctx->algorithm, "all")) { - RzList *list = rz_list_newf(NULL); - if (!list) { - return NULL; - } - RzIterator *it = ht_sp_as_iter(ctx->rh->plugins); - RzHashPlugin **val; - rz_iterator_foreach(it, val) { - const RzHashPlugin *plugin = *val; - rz_list_append(list, (void *)plugin->name); - } - rz_iterator_free(it); - return list; + if (strcmp(ctx->algorithm, "all")) { + return rz_str_split_list(ctx->algorithm, ",", 0); + } + + RzList *list = rz_list_newf(NULL); + if (!list) { + return NULL; + } + RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); + RzList *plugin_list = rz_list_new_from_iterator(iter); + rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); + RzListIter *it; + const RzHashPlugin *rmdp; + rz_list_foreach (plugin_list, it, rmdp) { + rz_list_append(list, (void *)rmdp->name); } - return rz_str_split_list(ctx->algorithm, ",", 0); + rz_list_free(plugin_list); + rz_iterator_free(iter); + return list; } static bool calculate_hash(RzHashContext *ctx, RzIO *io, const char *filename) { diff --git a/test/db/archos/linux-x64/cmd_list b/test/db/archos/linux-x64/cmd_list index 9e6d8697a49..e61eae98817 100644 --- a/test/db/archos/linux-x64/cmd_list +++ b/test/db/archos/linux-x64/cmd_list @@ -9,11 +9,11 @@ EXPECT=< Date: Thu, 24 Oct 2024 12:00:47 -0500 Subject: [PATCH 11/11] Fix parameter attributes, NULL checks, leaks and single rename. --- librz/bp/bp_plugin.c | 8 ++++++-- librz/core/casm.c | 7 ++++++- librz/core/cbin.c | 4 ++++ librz/core/ccrypto.c | 4 ++++ librz/core/cdebug.c | 7 ++++++- librz/core/chash.c | 4 ++-- librz/core/cio.c | 6 +++++- librz/core/cmd/cmd_debug.c | 2 +- librz/crypto/crypto.c | 4 ++-- librz/egg/egg.c | 8 ++++++-- librz/include/rz_bp.h | 2 +- librz/include/rz_core.h | 8 ++++---- librz/include/rz_egg.h | 4 ++-- librz/include/rz_list.h | 2 +- librz/main/rz-gg.c | 4 ++++ librz/main/rz-hash.c | 8 ++++++++ librz/util/list.c | 6 +++++- test/unit/test_list.c | 1 + 18 files changed, 68 insertions(+), 21 deletions(-) diff --git a/librz/bp/bp_plugin.c b/librz/bp/bp_plugin.c index 95141b8cea4..6a340422724 100644 --- a/librz/bp/bp_plugin.c +++ b/librz/bp/bp_plugin.c @@ -61,10 +61,14 @@ RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name) { return false; } -// TODO: deprecate -RZ_API void rz_bp_plugin_list(RzBreakpoint *bp) { +RZ_DEPRECATE RZ_API void rz_bp_plugin_print(RZ_NONNULL RzBreakpoint *bp) { + rz_return_if_fail(bp); RzIterator *iter = ht_sp_as_iter(bp->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return; + } rz_list_sort(plugin_list, (RzListComparator)rz_breakpoint_plugin_cmp, NULL); RzListIter *it; RzBreakpointPlugin *b; diff --git a/librz/core/casm.c b/librz/core/casm.c index a108576342d..9b7fd58c23e 100644 --- a/librz/core/casm.c +++ b/librz/core/casm.c @@ -165,11 +165,16 @@ RZ_API RzCmdStatus rz_core_asm_plugin_print(RzCore *core, RzAsmPlugin *ap, const return RZ_CMD_STATUS_OK; } -RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzCmdStateOutput *state) { +RZ_API RzCmdStatus rz_core_asm_plugins_print(RZ_NONNULL RZ_BORROW RzCore *core, RZ_NULLABLE const char *arch, RZ_OUT RzCmdStateOutput *state) { + rz_return_val_if_fail(core && state, RZ_CMD_STATUS_ERROR); int i; RzAsm *a = core->rasm; RzIterator *iter = ht_sp_as_iter(a->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return RZ_CMD_STATUS_ERROR; + } rz_list_sort(plugin_list, (RzListComparator)rz_asm_plugin_cmp, NULL); RzListIter *it; RzAsmPlugin *ap; diff --git a/librz/core/cbin.c b/librz/core/cbin.c index 98465ba4910..9ca5b064006 100644 --- a/librz/core/cbin.c +++ b/librz/core/cbin.c @@ -4908,6 +4908,10 @@ RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state RzIterator *iter = ht_sp_as_iter(bin->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return RZ_CMD_STATUS_ERROR; + } rz_list_sort(plugin_list, (RzListComparator)rz_bin_plugin_cmp, NULL); RzListIter *it; RzBinPlugin *bp; diff --git a/librz/core/ccrypto.c b/librz/core/ccrypto.c index 8fad02e20ff..1a4b7fe759b 100644 --- a/librz/core/ccrypto.c +++ b/librz/core/ccrypto.c @@ -42,6 +42,10 @@ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput } RzIterator *iter = ht_sp_as_iter(cry->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return RZ_CMD_STATUS_ERROR; + } rz_list_sort(plugin_list, (RzListComparator)rz_crypto_plugin_cmp, NULL); RzListIter *it; RzCryptoPlugin *plugin; diff --git a/librz/core/cdebug.c b/librz/core/cdebug.c index b159b3d2ada..254a6ea297d 100644 --- a/librz/core/cdebug.c +++ b/librz/core/cdebug.c @@ -319,7 +319,8 @@ RZ_API RzCmdStatus rz_core_debug_plugin_print(RzDebug *dbg, RzDebugPlugin *plugi return RZ_CMD_STATUS_OK; } -RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *state) { +RZ_API RzCmdStatus rz_core_debug_plugins_print(RZ_NONNULL RZ_BORROW RzCore *core, RZ_OUT RzCmdStateOutput *state) { + rz_return_val_if_fail(core && state, RZ_CMD_STATUS_ERROR); int count = 0; char spaces[16]; memset(spaces, ' ', 15); @@ -332,6 +333,10 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s rz_cmd_state_output_array_start(state); RzIterator *iter = ht_sp_as_iter(dbg->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return RZ_CMD_STATUS_ERROR; + } rz_list_sort(plugin_list, (RzListComparator)rz_debug_plugin_cmp, NULL); RzListIter *it; RzDebugPlugin *plugin; diff --git a/librz/core/chash.c b/librz/core/chash.c index ca64cecf357..7388cabb490 100644 --- a/librz/core/chash.c +++ b/librz/core/chash.c @@ -31,8 +31,8 @@ RZ_API RzCmdStatus rz_core_hash_plugin_print(RzCmdStateOutput *state, const RzHa return RZ_CMD_STATUS_OK; } -RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *state) { - rz_return_val_if_fail(hash, RZ_CMD_STATUS_ERROR); +RZ_API RzCmdStatus rz_core_hash_plugins_print(RZ_NONNULL RZ_BORROW RzHash *hash, RZ_OUT RzCmdStateOutput *state) { + rz_return_val_if_fail(hash && state, RZ_CMD_STATUS_ERROR); RzCmdStatus status; RzIterator *it = ht_sp_as_iter(hash->plugins); diff --git a/librz/core/cio.c b/librz/core/cio.c index e92b8dd3171..f04a421f993 100644 --- a/librz/core/cio.c +++ b/librz/core/cio.c @@ -478,7 +478,7 @@ RZ_API RzCmdStatus rz_core_io_plugin_print(RzIOPlugin *plugin, RzCmdStateOutput * \param io Reference to RzIO instance * \param state Specify how plugins shall be printed */ -RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { +RZ_API RzCmdStatus rz_core_io_plugins_print(RZ_NONNULL RZ_BORROW RzIO *io, RzCmdStateOutput *state) { rz_return_val_if_fail(io && state, RZ_CMD_STATUS_ERROR); if (!io) { @@ -489,6 +489,10 @@ RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state) { RzIterator *iter = ht_sp_as_iter(io->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return RZ_CMD_STATUS_ERROR; + } rz_list_sort(plugin_list, (RzListComparator)rz_io_plugin_cmp, NULL); RzListIter *it; RzIOPlugin *plugin; diff --git a/librz/core/cmd/cmd_debug.c b/librz/core/cmd/cmd_debug.c index 4ea6ea39efe..9b677853727 100644 --- a/librz/core/cmd/cmd_debug.c +++ b/librz/core/cmd/cmd_debug.c @@ -2142,7 +2142,7 @@ RZ_IPI RzCmdStatus rz_cmd_debug_toggle_bp_trace_index_handler(RzCore *core, int // dbh RZ_IPI RzCmdStatus rz_cmd_debug_bp_plugin_handler(RzCore *core, int argc, const char **argv) { if (argc == 1) { - rz_bp_plugin_list(core->dbg->bp); + rz_bp_plugin_print(core->dbg->bp); } else if (argc == 2) { if (!rz_bp_use(core->dbg->bp, argv[1])) { RZ_LOG_ERROR("Failed to set breakpoint plugin handler to %s\n", argv[1]); diff --git a/librz/crypto/crypto.c b/librz/crypto/crypto.c index 29317bc6814..9dcdc06b4cc 100644 --- a/librz/crypto/crypto.c +++ b/librz/crypto/crypto.c @@ -6,12 +6,12 @@ #include #include "rz_crypto_plugins.h" -static RzCryptoPlugin *crypto_static_plugins[] = { &rz_crypto_plugin_aes, &rz_crypto_plugin_aes_cbc, &rz_crypto_plugin_base64, &rz_crypto_plugin_base91, &rz_crypto_plugin_blowfish, &rz_crypto_plugin_cps2, &rz_crypto_plugin_des, &rz_crypto_plugin_punycode, &rz_crypto_plugin_rc2, &rz_crypto_plugin_rc4, &rz_crypto_plugin_rc6, &rz_crypto_plugin_rol, &rz_crypto_plugin_ror, &rz_crypto_plugin_rot, &rz_crypto_plugin_serpent, &rz_crypto_plugin_xor, &rz_crypto_plugin_sm4_ecb }; - #define RZ_CRYPTO_OUTPUT_SIZE 4096 RZ_LIB_VERSION(rz_crypto); +static RzCryptoPlugin *crypto_static_plugins[] = { RZ_CRYPTO_STATIC_PLUGINS }; + static const struct { const char *name; RzCryptoSelector bit; diff --git a/librz/egg/egg.c b/librz/egg/egg.c index fefccd0a00a..55bf38dd9b2 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -533,7 +533,7 @@ RZ_API char *rz_egg_option_get(RzEgg *egg, const char *key) { return sdb_get(egg->db, key); } -RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { +RZ_API int rz_egg_shellcode(RZ_NONNULL RZ_BORROW RzEgg *egg, const char *name) { rz_return_val_if_fail(egg && name, false); RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin **val; @@ -544,11 +544,13 @@ RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { b = p->build(egg); if (!b) { RZ_LOG_ERROR("egg: %s Shellcode has failed\n", p->name); + rz_iterator_free(iter); return false; } ut64 tmpsz; const ut8 *tmp = rz_buf_data(b, &tmpsz); rz_egg_raw(egg, tmp, tmpsz); + rz_iterator_free(iter); return true; } } @@ -556,7 +558,7 @@ RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name) { return false; } -RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { +RZ_API int rz_egg_encode(RZ_NONNULL RZ_BORROW RzEgg *egg, const char *name) { rz_return_val_if_fail(egg && name, false); RzIterator *iter = ht_sp_as_iter(egg->plugins); RzEggPlugin **val; @@ -566,10 +568,12 @@ RZ_API int rz_egg_encode(RzEgg *egg, const char *name) { if (p->type == RZ_EGG_PLUGIN_ENCODER && !strcmp(name, p->name)) { b = p->build(egg); if (!b) { + rz_iterator_free(iter); return false; } rz_buf_free(egg->bin); egg->bin = b; + rz_iterator_free(iter); return true; } } diff --git a/librz/include/rz_bp.h b/librz/include/rz_bp.h index 25ce4fff2c3..3dc4c553b68 100644 --- a/librz/include/rz_bp.h +++ b/librz/include/rz_bp.h @@ -131,7 +131,7 @@ RZ_API bool rz_bp_plugin_add(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpoint RZ_API bool rz_bp_plugin_del(RzBreakpoint *bp, RZ_BORROW RZ_NONNULL RzBreakpointPlugin *plugin); RZ_API int rz_bp_use(RZ_NONNULL RzBreakpoint *bp, RZ_NONNULL const char *name); RZ_API int rz_bp_plugin_del_byname(RzBreakpoint *bp, RZ_NONNULL const char *name); -RZ_API void rz_bp_plugin_list(RzBreakpoint *bp); +RZ_DEPRECATE RZ_API void rz_bp_plugin_print(RZ_NONNULL RzBreakpoint *bp); RZ_API int rz_bp_size(RZ_NONNULL RzBreakpoint *bp, int bits); RZ_API int rz_bp_size_at(RZ_NONNULL RzBreakpoint *bp, ut64 addr); diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 20796b8131a..290c9f7c095 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -589,18 +589,18 @@ RZ_API void rz_core_dbg_follow_seek_register(RzCore *core); RZ_API RZ_OWN RzList /**/ *rz_core_debug_backtraces(RzCore *core); RZ_API void rz_backtrace_free(RZ_NULLABLE RzBacktrace *bt); -RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *state); +RZ_API RzCmdStatus rz_core_debug_plugins_print(RZ_NONNULL RZ_BORROW RzCore *core, RZ_OUT RzCmdStateOutput *state); RZ_API void rz_core_debug_map_update_flags(RzCore *core); RZ_API void rz_core_debug_map_print(RzCore *core, ut64 addr, RzCmdStateOutput *state); /* chash.c */ -RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *state); +RZ_API RzCmdStatus rz_core_hash_plugins_print(RZ_NONNULL RZ_BORROW RzHash *hash, RZ_OUT RzCmdStateOutput *state); /* ccrypto.c */ RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput *state); /* cio.c */ -RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state); +RZ_API RzCmdStatus rz_core_io_plugins_print(RZ_NONNULL RZ_BORROW RzIO *io, RzCmdStateOutput *state); /* cio.c */ RZ_API RzCmdStatus rz_core_parser_plugins_print(RzParse *parser, RzCmdStateOutput *state); @@ -850,7 +850,7 @@ RZ_API RzList /**/ *rz_core_asm_hit_list_new(void); RZ_API void rz_core_asm_hit_free(void *_hit); RZ_API void rz_core_set_asm_configs(RzCore *core, char *arch, ut32 bits, int segoff); RZ_API char *rz_core_asm_search(RzCore *core, const char *input); -RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzCmdStateOutput *state); +RZ_API RzCmdStatus rz_core_asm_plugins_print(RZ_NONNULL RZ_BORROW RzCore *core, RZ_NULLABLE const char *arch, RZ_OUT RzCmdStateOutput *state); RZ_API RzList /**/ *rz_core_asm_strsearch(RzCore *core, const char *input, ut64 from, ut64 to, int maxhits, int regexp, int everyByte, int mode); RZ_API RzList /**/ *rz_core_asm_bwdisassemble(RzCore *core, ut64 addr, int n, int len); RZ_API RzList /**/ *rz_core_asm_back_disassemble_instr(RzCore *core, ut64 addr, int len, ut32 hit_count, ut32 extra_padding); diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index e452040ed51..a36c2ae5a2a 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -197,8 +197,8 @@ RZ_API void rz_egg_syscall(RzEgg *egg, const char *arg, ...) RZ_PRINTF_CHECK(2, RZ_API void rz_egg_alloc(RzEgg *egg, int n); RZ_API void rz_egg_label(RzEgg *egg, const char *name); RZ_API int rz_egg_raw(RzEgg *egg, const ut8 *b, int len); -RZ_API int rz_egg_encode(RzEgg *egg, const char *name); -RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name); +RZ_API int rz_egg_encode(RZ_NONNULL RZ_BORROW RzEgg *egg, const char *name); +RZ_API int rz_egg_shellcode(RZ_NONNULL RZ_BORROW RzEgg *egg, const char *name); #define rz_egg_get_shellcodes(x) x->plugins RZ_API void rz_egg_option_set(RzEgg *egg, const char *k, const char *v); RZ_API char *rz_egg_option_get(RzEgg *egg, const char *k); diff --git a/librz/include/rz_list.h b/librz/include/rz_list.h index 18ccd8e3dc2..3f127d21771 100644 --- a/librz/include/rz_list.h +++ b/librz/include/rz_list.h @@ -65,7 +65,7 @@ typedef int (*RzListComparator)(const void *value, const void *list_data, void * RZ_API RZ_OWN RzList *rz_list_new(void); RZ_API RZ_OWN RzList *rz_list_newf(RZ_NULLABLE RzListFree f); -RZ_API RZ_OWN RzList *rz_list_new_from_array(RZ_NONNULL const void **arr, size_t arr_size); +RZ_API RZ_OWN RzList *rz_list_new_from_array(const void **arr, size_t arr_size); RZ_API RZ_OWN RzList *rz_list_new_from_iterator(RZ_BORROW RZ_NONNULL RzIterator *iter); RZ_API RZ_BORROW RzListIter *rz_list_iter_get_prev(RZ_NONNULL RzListIter *iter); RZ_API RZ_BORROW RzListIter *rz_list_iter_get_next(RZ_NONNULL RzListIter *iter); diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index 5db9beb04a9..0ae55cf4472 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -72,6 +72,10 @@ static void list(RzEgg *egg) { printf("shellcodes:\n"); RzIterator *iter = ht_sp_as_iter(egg->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return; + } rz_list_sort(plugin_list, (RzListComparator)rz_egg_plugin_cmp, NULL); RzListIter *it; RzEggPlugin *p; diff --git a/librz/main/rz-hash.c b/librz/main/rz-hash.c index 1a5afd6529b..3e8f275bba2 100644 --- a/librz/main/rz-hash.c +++ b/librz/main/rz-hash.c @@ -132,6 +132,10 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) { RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return; + } rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); RzListIter *it; const RzHashPlugin *rmdp; @@ -820,6 +824,10 @@ static RzList /**/ *parse_hash_algorithms(RzHashContext *ctx) { } RzIterator *iter = ht_sp_as_iter(ctx->rh->plugins); RzList *plugin_list = rz_list_new_from_iterator(iter); + if (!plugin_list) { + rz_iterator_free(iter); + return NULL; + } rz_list_sort(plugin_list, (RzListComparator)rz_hash_plugin_cmp, NULL); RzListIter *it; const RzHashPlugin *rmdp; diff --git a/librz/util/list.c b/librz/util/list.c index 19846e074a5..4cbcec3e109 100644 --- a/librz/util/list.c +++ b/librz/util/list.c @@ -284,11 +284,14 @@ RZ_API RZ_OWN RzList *rz_list_newf(RZ_NULLABLE RzListFree f) { * \brief Allocates a new RzList and adds an array elements to it * **/ -RZ_API RZ_OWN RzList *rz_list_new_from_array(RZ_NONNULL const void **arr, size_t arr_size) { +RZ_API RZ_OWN RzList *rz_list_new_from_array(const void **arr, size_t arr_size) { RzList *l = rz_list_new(); if (!l) { return NULL; } + if (!arr) { + return l; + } size_t i; for (i = 0; i < arr_size; i++) { rz_list_append(l, (void *)arr[i]); @@ -303,6 +306,7 @@ RZ_API RZ_OWN RzList *rz_list_new_from_array(RZ_NONNULL const void **arr, size_t * \return The produced list. Or NULL in case of failure. **/ RZ_API RZ_OWN RzList *rz_list_new_from_iterator(RZ_BORROW RZ_NONNULL RzIterator *iter) { + rz_return_val_if_fail(iter, NULL); RzList *l = rz_list_new(); if (!l) { return NULL; diff --git a/test/unit/test_list.c b/test/unit/test_list.c index cedc5d82e97..e0dd0e4de15 100644 --- a/test/unit/test_list.c +++ b/test/unit/test_list.c @@ -229,6 +229,7 @@ bool test_rz_list_from_iter(void) { } RzIterator *iter = ht_up_as_iter(alpha_ht); RzList *list = rz_list_new_from_iterator(iter); + mu_assert_notnull(list, "List init failed."); rz_list_sort(list, (RzListComparator)strcmp, NULL); mu_assert_eq(rz_list_length(list), 26, "Number of elements are off"); RzListIter *it;