Skip to content

Commit

Permalink
Check block APIs nullability for radareorg#23490
Browse files Browse the repository at this point in the history
  • Loading branch information
astralia committed Nov 2, 2024
1 parent e25d1be commit dd9b035
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions libr/anal/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ static int __bb_addr_cmp(const void *incoming, const RBNode *in_tree, void *user

R_API void r_anal_block_ref(RAnalBlock *bb) {
// XXX we have R_REF for this
if (bb) {
// 0-refd must already be freed.
R_RETURN_IF_FAIL (bb->ref > 0);
bb->ref++;
}
R_RETURN_IF_FAIL (bb);
// 0-refd must already be freed.
R_RETURN_IF_FAIL (bb->ref > 0);
bb->ref++;
}

#define DFLT_NINSTR 3

static RAnalBlock *block_new(RAnal *a, ut64 addr, ut64 size) {
R_RETURN_VAL_IF_FAIL (a, NULL);
RAnalBlock *block = R_NEW0 (RAnalBlock);
if (!block) {
return NULL;
Expand Down Expand Up @@ -88,9 +88,7 @@ static RAnalBlock *block_new(RAnal *a, ut64 addr, ut64 size) {
}

static void block_free(RAnalBlock *bb) {
if (!bb) {
return;
}
R_RETURN_IF_FAIL (bb);
free (bb->esil);
r_anal_cond_free (bb->cond);
free (bb->fingerprint);
Expand All @@ -104,12 +102,14 @@ static void block_free(RAnalBlock *bb) {
}

R_IPI void __block_free_rb(RBNode *node, void *user) {
R_RETURN_IF_FAIL (node && user);
RAnalBlock *block = unwrap (node);
r_anal_block_unref (block);
// block_free (block);
}

R_API void r_anal_block_reset(RAnal *a) {
R_RETURN_IF_FAIL (a);
if (a->bb_tree) {
r_rbtree_free (a->bb_tree, __block_free_rb, NULL);
a->bb_tree = NULL;
Expand Down Expand Up @@ -153,6 +153,7 @@ static bool all_in(RAnalBlock *node, ut64 addr, RAnalBlockCb cb, void *user) {
}

R_API bool r_anal_blocks_foreach_in(RAnal *anal, ut64 addr, RAnalBlockCb cb, void *user) {
R_RETURN_VAL_IF_FAIL (anal && user, false);
return all_in (anal->bb_tree ? unwrap (anal->bb_tree) : NULL, addr, cb, user);
}

Expand All @@ -164,6 +165,7 @@ static bool block_list_cb(RAnalBlock *block, void *user) {
}

R_API RList *r_anal_get_blocks_in(RAnal *anal, ut64 addr) {
R_RETURN_VAL_IF_FAIL (anal, NULL);
RList *list = r_list_newf ((RListFree)r_anal_block_unref);
if (list) {
r_anal_blocks_foreach_in (anal, addr, block_list_cb, list);
Expand Down Expand Up @@ -192,10 +194,12 @@ static void all_intersect(RAnalBlock *node, ut64 addr, ut64 size, RAnalBlockCb c
}

R_API void r_anal_blocks_foreach_intersect(RAnal *anal, ut64 addr, ut64 size, RAnalBlockCb cb, void *user) {
R_RETURN_IF_FAIL (anal && user);
all_intersect (anal->bb_tree ? unwrap (anal->bb_tree) : NULL, addr, size, cb, user);
}

R_API RList *r_anal_get_blocks_intersect(RAnal *anal, ut64 addr, ut64 size) {
R_RETURN_VAL_IF_FAIL (anal, NULL);
RList *list = r_list_newf ((RListFree)r_anal_block_unref);
if (R_LIKELY (list)) {
r_anal_blocks_foreach_intersect (anal, addr, size, block_list_cb, list);
Expand All @@ -204,6 +208,7 @@ R_API RList *r_anal_get_blocks_intersect(RAnal *anal, ut64 addr, ut64 size) {
}

R_API RAnalBlock *r_anal_create_block(RAnal *anal, ut64 addr, ut64 size) {
R_RETURN_VAL_IF_FAIL (anal, NULL);
if (r_anal_get_block_at (anal, addr)) {
return NULL;
}
Expand All @@ -217,13 +222,15 @@ R_API RAnalBlock *r_anal_create_block(RAnal *anal, ut64 addr, ut64 size) {
}

R_API void r_anal_delete_block_at(RAnal *anal, ut64 addr) {
R_RETURN_IF_FAIL (anal);
RAnalBlock *bb = r_anal_get_block_at (anal, addr);
if (bb) {
r_anal_delete_block (bb);
}
}

R_API void r_anal_delete_block(RAnalBlock *bb) {
R_RETURN_IF_FAIL (bb);
r_anal_block_ref (bb);
while (!r_list_empty (bb->fcns)) {
RListIter *iter, *iter2;
Expand All @@ -237,6 +244,7 @@ R_API void r_anal_delete_block(RAnalBlock *bb) {
}

R_API void r_anal_block_set_size(RAnalBlock *block, ut64 size) {
R_RETURN_IF_FAIL (block);
if (block->size == size) {
return;
}
Expand All @@ -256,6 +264,7 @@ R_API void r_anal_block_set_size(RAnalBlock *block, ut64 size) {
}

R_API bool r_anal_block_relocate(RAnalBlock *block, ut64 addr, ut64 size) {
R_RETURN_VAL_IF_FAIL (block, false);
if (block->addr == addr) {
r_anal_block_set_size (block, size);
r_anal_block_update_hash (block);
Expand Down Expand Up @@ -298,6 +307,7 @@ R_API bool r_anal_block_relocate(RAnalBlock *block, ut64 addr, ut64 size) {
}

R_API RAnalBlock *r_anal_block_split(RAnalBlock *bbi, ut64 addr) {
R_RETURN_VAL_IF_FAIL (bbi, NULL);
RAnal *anal = bbi->anal;
R_RETURN_VAL_IF_FAIL (bbi && addr >= bbi->addr && addr < bbi->addr + bbi->size && addr != UT64_MAX, 0);
if (addr == bbi->addr) {
Expand Down Expand Up @@ -363,6 +373,7 @@ R_API RAnalBlock *r_anal_block_split(RAnalBlock *bbi, ut64 addr) {
}

R_API bool r_anal_block_merge(RAnalBlock *a, RAnalBlock *b) {
R_RETURN_VAL_IF_FAIL (a && b, false);
if (!r_anal_block_is_contiguous (a, b)) {
return false;
}
Expand Down Expand Up @@ -400,7 +411,8 @@ R_API bool r_anal_block_merge(RAnalBlock *a, RAnalBlock *b) {
a->fail = b->fail;
if (a->switch_op) {
if (a->anal->verbose) {
R_LOG_INFO ("Dropping switch table at 0x%" PFMT64x " of block at 0x%" PFMT64x, a->switch_op->addr, a->addr);
R_LOG_INFO ("Dropping switch table at 0x%" PFMT64x " of block at 0x%" PFMT64x,
a->switch_op->addr, a->addr);
}
r_anal_switch_op_free (a->switch_op);
}
Expand All @@ -420,9 +432,7 @@ R_API bool r_anal_block_merge(RAnalBlock *a, RAnalBlock *b) {
}

R_API void r_anal_block_unref(RAnalBlock *bb) {
if (!bb) {
return;
}
R_RETURN_IF_FAIL (bb);
if (bb->ref < 1) {
return;
}
Expand All @@ -447,6 +457,8 @@ R_API bool r_anal_block_successor_addrs_foreach(RAnalBlock *block, RAnalAddrCb c
} \
} while (0);

R_RETURN_VAL_IF_FAIL (block && user, false);

CB_ADDR (block->jump);
CB_ADDR (block->fail);
if (block->switch_op && block->switch_op->cases) {
Expand Down Expand Up @@ -483,6 +495,7 @@ static bool block_recurse_successor_cb(ut64 addr, void *user) {
}

R_API bool r_anal_block_recurse(RAnalBlock *block, RAnalBlockCb cb, void *user) {
R_RETURN_VAL_IF_FAIL (block && user, false);
bool breaked = false;
RAnalBlockRecurseContext ctx;
ctx.anal = block->anal;
Expand Down Expand Up @@ -511,6 +524,7 @@ R_API bool r_anal_block_recurse(RAnalBlock *block, RAnalBlockCb cb, void *user)
}

R_API bool r_anal_block_recurse_followthrough(RAnalBlock *block, RAnalBlockCb cb, void *user) {
R_RETURN_VAL_IF_FAIL (block && user, false);
bool breaked = false;
RAnalBlockRecurseContext ctx;
ctx.anal = block->anal;
Expand Down Expand Up @@ -544,9 +558,7 @@ typedef struct {
} RecurseDepthFirstCtx;

R_API bool r_anal_block_recurse_depth_first(RAnalBlock *block, RAnalBlockCb cb, R_NULLABLE RAnalBlockCb on_exit, void *user) {
if (!block) {
return false;
}
R_RETURN_VAL_IF_FAIL (block && user, false);
RVector path;
r_vector_init (&path, sizeof (RecurseDepthFirstCtx), NULL, NULL);
HtUP *visited = ht_up_new0 ();
Expand Down Expand Up @@ -619,6 +631,7 @@ static bool recurse_list_cb(RAnalBlock *block, void *user) {
}

R_API RList *r_anal_block_recurse_list(RAnalBlock *block) {
R_RETURN_VAL_IF_FAIL (block, NULL);
RList *ret = r_list_newf ((RListFree)r_anal_block_unref);
if (ret) {
r_anal_block_recurse (block, recurse_list_cb, ret);
Expand All @@ -627,13 +640,15 @@ R_API RList *r_anal_block_recurse_list(RAnalBlock *block) {
}

R_API void r_anal_block_add_switch_case(RAnalBlock *block, ut64 switch_addr, ut64 case_value, ut64 case_addr) {
R_RETURN_IF_FAIL (block);
if (!block->switch_op) {
block->switch_op = r_anal_switch_op_new (switch_addr, 0, 0, 0);
}
r_anal_switch_op_add_case (block->switch_op, case_addr, case_value, case_addr);
}

R_API bool r_anal_block_op_starts_at(RAnalBlock *bb, ut64 addr) {
R_RETURN_VAL_IF_FAIL (bb, false);
if (!r_anal_block_contains (bb, addr)) {
return false;
}
Expand Down Expand Up @@ -693,6 +708,7 @@ static ut64 bb_addr_for(RAnal *a, ut64 n) {
}

R_API R_NULLABLE RList/*<RAnalBlock *>*/ *r_anal_block_shortest_path(RAnalBlock *block, ut64 dst) {
R_RETURN_VAL_IF_FAIL (block, NULL);
ut64 dstbb_addr = bb_addr_for (block->anal, dst);

RList *ret = NULL;
Expand Down

0 comments on commit dd9b035

Please sign in to comment.