From 6e65b5d02a8f8791311435c3881e679d548b9862 Mon Sep 17 00:00:00 2001 From: radare Date: Fri, 21 Sep 2018 16:32:25 +0200 Subject: [PATCH] Honor search.maxhits in /m (#11609) --- libr/core/cmd_magic.c | 12 +++++++++--- libr/core/cmd_search.c | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libr/core/cmd_magic.c b/libr/core/cmd_magic.c index a26679e30e..f758f9ce7f 100644 --- a/libr/core/cmd_magic.c +++ b/libr/core/cmd_magic.c @@ -12,12 +12,16 @@ static void r_core_magic_reset(RCore *core) { kw_count = 0; } -static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, int v, bool json) { +static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, int v, bool json, int *hits) { const char *fmt; char *q, *p; const char *str; int found = 0, delta = 0, adelta = 0, ret; ut64 curoffset = core->offset; + int maxHits = r_config_get_i (core->config, "search.maxhits"); + if (maxHits > 0 && *hits >= maxHits) { + return 0; + } #define NAH 32 if (--depth<0) { @@ -113,6 +117,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, strcpy (q + 1, q + ((q[2] == ' ')? 3: 2)); } } + (*hits)++; cmdhit = r_config_get (core->config, "cmd.hit"); if (cmdhit && *cmdhit) { r_core_cmd0 (core, cmdhit); @@ -152,7 +157,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, if (!fmt || !*fmt) { fmt = file; } - r_core_magic_at (core, fmt, addr, depth, 1, json); + r_core_magic_at (core, fmt, addr, depth, 1, json, hits); *q = '@'; } break; @@ -191,8 +196,9 @@ seek_exit: static void r_core_magic(RCore *core, const char *file, int v) { ut64 addr = core->offset; + int hits = 0; magicdepth = r_config_get_i (core->config, "magic.depth"); // TODO: do not use global var here - r_core_magic_at (core, file, addr, magicdepth, v, false); + r_core_magic_at (core, file, addr, magicdepth, v, false, &hits); if (addr != core->offset) { r_core_seek (core, addr, true); } diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index b0ccf2e0cf..bd71d16355 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -3003,6 +3003,8 @@ reread: r_cons_printf ("["); } r_core_magic_reset (core); + int maxHits = r_config_get_i (core->config, "search.maxhits"); + int hits = 0; r_list_foreach (param.boundaries, iter, map) { if (!json) { eprintf ("-- %llx %llx\n", map->itv.addr, r_itv_end (map->itv)); @@ -3012,11 +3014,14 @@ reread: if (r_cons_is_breaked ()) { break; } - ret = r_core_magic_at (core, file, addr, 99, false, json); + ret = r_core_magic_at (core, file, addr, 99, false, json, &hits); if (ret == -1) { // something went terribly wrong. break; } + if (maxHits && hits >= maxHits) { + break; + } addr += ret - 1; } r_cons_clear_line (1);