Fix more leaks in RAnal and RCore

This commit is contained in:
pancake 2022-10-17 00:25:49 +02:00 committed by pancake
parent 8dcffdb67e
commit 4ed98d75c3
13 changed files with 53 additions and 36 deletions

View File

@ -119,13 +119,14 @@ R_API RAnalCond *r_anal_cond_new_from_op(RAnalOp *op) {
if (!(cond = r_anal_cond_new ())) {
return NULL;
}
//v->reg[0] = op->src[0];
//v->reg[1] = op->src[1];
cond->arg[0] = r_anal_value_copy (r_vector_index_ptr (&op->srcs, 0));
cond->arg[1] = r_anal_value_copy (r_vector_index_ptr (&op->srcs, 1));
r_vector_fini (&op->srcs);
// TODO: moar!
//cond->arg[1] = op->src[1];
RAnalValue *src0 = r_vector_at (&op->srcs, 0);
RAnalValue *src1 = r_vector_at (&op->srcs, 1);
if (!src0 || !src1) {
return NULL;
}
// TODO: use r_ref
cond->arg[0] = r_anal_value_copy (src0);
cond->arg[1] = r_anal_value_copy (src1);
return cond;
}

View File

@ -4323,6 +4323,9 @@ static void set_opdir(RAnalOp *op) {
}
static void set_src_dst(RAnalValue *val, RReg *reg, csh *handle, cs_insn *insn, int x, int bits) {
if (!val) {
return;
}
cs_arm_op armop = INSOP (x);
cs_arm64_op arm64op = INSOP64 (x);
if (bits == 64) {
@ -4410,9 +4413,9 @@ static void op_fillval(RAnal *anal, RAnalOp *op, csh handle, cs_insn *insn, int
break;
}
for (j = 0; j < 3; j++, i++) {
set_src_dst (r_vector_index_ptr (&op->srcs, j), anal->reg, &handle, insn, i, bits);
set_src_dst (r_vector_at (&op->srcs, j), anal->reg, &handle, insn, i, bits);
}
set_src_dst (r_vector_index_ptr (&op->dsts, 0), anal->reg, &handle, insn, 0, bits);
set_src_dst (r_vector_at (&op->dsts, 0), anal->reg, &handle, insn, 0, bits);
break;
case R_ANAL_OP_TYPE_STORE:
if (count > 2) {
@ -4428,9 +4431,9 @@ static void op_fillval(RAnal *anal, RAnalOp *op, csh handle, cs_insn *insn, int
}
}
}
set_src_dst (r_vector_index_ptr (&op->dsts, 0), anal->reg, &handle, insn, --count, bits);
set_src_dst (r_vector_at (&op->dsts, 0), anal->reg, &handle, insn, --count, bits);
for (j = 0; j < 3 && j < count; j++) {
set_src_dst (r_vector_index_ptr (&op->srcs, j), anal->reg, &handle, insn, j, bits);
set_src_dst (r_vector_at (&op->srcs, j), anal->reg, &handle, insn, j, bits);
}
break;
default:
@ -4521,6 +4524,7 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn
}
cs_free (insn, n);
} else {
cs_free (insn, n);
op->size = 4;
op->type = R_ANAL_OP_TYPE_ILL;
if (len < 4) {

View File

@ -2256,6 +2256,8 @@ static void anop_esil(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
esilprintf (op, "%s,I2D,%s", src, dst);
break;
}
free (src);
free (dst);
break;
}
case X86_INS_BT:

View File

@ -66,6 +66,7 @@ static ut32 _rate_compat(RArchPlugin *p, RArchConfig *cfg) {
break;
default:
bits = UT32_MAX;
break;
}
ut32 score = 0;
if (!strcmp (p->arch, cfg->arch)) {
@ -105,6 +106,9 @@ R_API bool r_arch_use(RArch *arch, RArchConfig *config) {
if (!config) {
config = arch->cfg;
}
if (config && arch->cfg == config) {
return true;
}
if (!config) {
// arch->decoder = NULL;
}
@ -113,10 +117,8 @@ R_API bool r_arch_use(RArch *arch, RArchConfig *config) {
return false;
}
RArchConfig *oconfig = arch->cfg;
if (oconfig == config) {
return true;
}
r_ref (config);
r_unref (arch->cfg);
arch->cfg = config;
if (!r_arch_use_decoder (arch, dname)) {
arch->cfg = oconfig;
@ -133,14 +135,14 @@ R_API bool r_arch_use(RArch *arch, RArchConfig *config) {
R_API bool r_arch_set_bits(RArch *arch, ut32 bits) {
r_return_val_if_fail (arch && bits, false);
if (!arch->cfg) {
arch->cfg = r_arch_config_new ();
if (!arch->cfg) {
RArchConfig *cfg = r_arch_config_new ();
if (!cfg) {
return false;
}
// r_arch_config_set_bits (arch->cfg, bits);
arch->cfg->bits = bits;
if (!r_arch_use (arch, arch->cfg)) {
r_unref (arch->cfg);
cfg->bits = bits;
if (!r_arch_use (arch, cfg)) {
r_unref (cfg);
arch->cfg = NULL;
return false;
}
@ -167,13 +169,13 @@ R_API bool r_arch_set_bits(RArch *arch, ut32 bits) {
R_API bool r_arch_set_endian(RArch *arch, ut32 endian) {
r_return_val_if_fail (arch, false);
if (!arch->cfg) {
arch->cfg = r_arch_config_new ();
if (!arch->cfg) {
RArchConfig *cfg = r_arch_config_new ();
if (!cfg) {
return false;
}
arch->cfg->endian = endian;
if (!r_arch_use (arch, arch->cfg)) {
r_unref (arch->cfg);
cfg->endian = endian;
if (!r_arch_use (arch, cfg)) {
r_unref (cfg);
arch->cfg = NULL;
return false;
}
@ -204,14 +206,14 @@ R_API bool r_arch_set_arch(RArch *arch, char *archname) {
return false;
}
if (!arch->cfg) {
arch->cfg = r_arch_config_new ();
if (!arch->cfg) {
RArchConfig *cfg = r_arch_config_new ();
if (!cfg) {
free (_arch);
return false;
}
arch->cfg->arch = _arch;
if (!r_arch_use (arch, arch->cfg)) {
r_unref (arch->cfg);
cfg->arch =_arch;
if (!r_arch_use (arch, cfg)) {
r_unref (cfg);
arch->cfg = NULL;
return false;
}

View File

@ -4135,7 +4135,7 @@ static void nextword(RCore *core, RAGraph *g, const char *word) {
gh->word_nth = 0;
}
struct r_agraph_location *pos = r_vector_index_ptr (&gh->word_list, gh->word_nth);
struct r_agraph_location *pos = r_vector_at (&gh->word_list, gh->word_nth);
gh->word_nth++;
if (pos) {
can->sx = -pos->x + can->w / 2;

View File

@ -3543,6 +3543,7 @@ static bool anal_block_cb(RAnalBlock *bb, BlockRecurseCtx *ctx) {
#else
pos = (opaddr - bb->addr);
if (r_anal_op (core->anal, &op, opaddr, buf + pos, bb->size - pos, mask) < 1) {
r_anal_op_fini (&op);
break;
}
#endif

View File

@ -9559,7 +9559,7 @@ static void cmd_anal_hint(RCore *core, const char *input) {
}
}
if (!offimm) {
RAnalValue *dst = r_vector_index_ptr (&op.dsts, 0);
RAnalValue *dst = r_vector_at (&op.dsts, 0);
if (dst) {
if (dst->imm) {
offimm = dst->imm;

View File

@ -1004,7 +1004,7 @@ R_API void r_core_link_stroff(RCore *core, RAnalFunction *fcn) {
src_imm = src->delta;
}
}
RAnalValue *dst = r_vector_index_ptr (&aop.dsts, 0);
RAnalValue *dst = r_vector_at (&aop.dsts, 0);
if (dst && dst->reg && dst->reg->name) {
dst_addr = r_reg_getv (esil->anal->reg, dst->reg->name) + index;
dst_imm = dst->delta;

View File

@ -3004,7 +3004,6 @@ R_API bool r_core_init(RCore *core) {
core->ev = r_event_new (core);
r_event_hook (core->ev, R_EVENT_ALL, cb_event_handler, NULL);
core->max_cmd_depth = R_CONS_CMD_DEPTH + 1;
core->lock = r_th_lock_new (true);
core->sdb = sdb_new (NULL, "r2kv.sdb", 0); // XXX: path must be in home?
core->lastsearch = NULL;
core->cmdfilter = NULL;

View File

@ -419,13 +419,13 @@ R_API void r_debug_free(RDebug *dbg) {
r_list_free (dbg->maps_user);
r_list_free (dbg->threads);
r_num_free (dbg->num);
sdb_free (dbg->sgnls);
r_tree_free (dbg->tree);
sdb_foreach (dbg->tracenodes, (SdbForeachCallback)free_tracenodes_entry, dbg);
sdb_free (dbg->tracenodes);
r_list_free (dbg->plugins);
r_list_free (dbg->call_frames);
free (dbg->btalgo);
r_debug_signal_fini (dbg);
r_debug_trace_free (dbg->trace);
r_debug_session_free (dbg->session);
r_anal_op_free (dbg->cur_op);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2014-2020 - pancake */
/* radare - LGPL - Copyright 2014-2022 - pancake */
#include <r_debug.h>
@ -50,6 +50,10 @@ static struct {
{ NULL }
};
R_API void r_debug_signal_fini(RDebug *dbg) {
sdb_free (DB);
}
R_API void r_debug_signal_init(RDebug *dbg) {
int i;
// XXX

View File

@ -485,6 +485,7 @@ R_API ut64 r_debug_get_baddr(RDebug *dbg, const char *file);
/* send signals */
R_API void r_debug_signal_init(RDebug *dbg);
R_API void r_debug_signal_fini(RDebug *dbg);
R_API int r_debug_signal_send(RDebug *dbg, int num);
R_API int r_debug_signal_what(RDebug *dbg, int num);
R_API int r_debug_signal_resolve(RDebug *dbg, const char *signame);

View File

@ -454,7 +454,7 @@ beach:
}
SDB_API bool sdb_text_check(Sdb *s, const char *file) {
char buf[64];
char buf[64] = {0};
int fd = open (file, O_RDONLY | O_BINARY);
if (fd < 0) {
return false;
@ -466,6 +466,9 @@ SDB_API bool sdb_text_check(Sdb *s, const char *file) {
}
int count = read (fd, buf, R_MIN (st.st_size, (off_t)sizeof (buf)));
close (fd);
if (count < 1) {
return false;
}
bool is_ascii = true;
bool has_eq = false;
bool has_nl = false;