From dd9b0354817b7a06cb79a0fec40ac35a734d6515 Mon Sep 17 00:00:00 2001 From: astralia Date: Sat, 2 Nov 2024 21:40:44 +0100 Subject: [PATCH] Check block APIs nullability for #23490 --- libr/anal/block.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/libr/anal/block.c b/libr/anal/block.c index 908f29a231a76e..f4721262d91de2 100644 --- a/libr/anal/block.c +++ b/libr/anal/block.c @@ -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; @@ -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); @@ -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; @@ -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); } @@ -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); @@ -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); @@ -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; } @@ -217,6 +222,7 @@ 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); @@ -224,6 +230,7 @@ R_API void r_anal_delete_block_at(RAnal *anal, ut64 addr) { } 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; @@ -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; } @@ -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); @@ -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) { @@ -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; } @@ -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); } @@ -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; } @@ -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) { @@ -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; @@ -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; @@ -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 (); @@ -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); @@ -627,6 +640,7 @@ 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); } @@ -634,6 +648,7 @@ R_API void r_anal_block_add_switch_case(RAnalBlock *block, ut64 switch_addr, ut6 } 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; } @@ -693,6 +708,7 @@ static ut64 bb_addr_for(RAnal *a, ut64 n) { } R_API R_NULLABLE RList/**/ *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;