rz_assert: Add rz_break_if_reached/fail() (#5769)

This commit is contained in:
Khairul Azhar Kasmiran 2026-01-11 10:07:37 +08:00 committed by GitHub
parent d6524f7114
commit 24983985f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 9 deletions

View file

@ -51,7 +51,7 @@ RZ_OWN RzList /*<RzBinSymbol *>*/ *PE_(rz_bin_pe_get_clr_symbols)(RzBinPEObj *bi
Pe_image_metadata_methoddef *methoddef = *it;
if ((type_name || type_namespace) && i >= type_methods_start && i >= type_methods_end) {
rz_goto_if_fail(type_it, loop_end);
rz_break_if_fail(type_it);
// Update class and namespace
free(type_name);
@ -97,7 +97,6 @@ RZ_OWN RzList /*<RzBinSymbol *>*/ *PE_(rz_bin_pe_get_clr_symbols)(RzBinPEObj *bi
rz_list_append(methods, sym);
i++;
}
loop_end:
// Cleanup class / namespace strings
free(type_name);

View file

@ -1769,7 +1769,7 @@ static ut32 fold_variables(RzCore *core, RzDisasmState *ds, RzListIter /*<RzAnal
// fold_var = hide -> group the first two var with ellipsis in tail
ut32 group_num = strcmp(ds->fold_var, "group") ? 2 : 3;
while (iter_mov < group_num) {
rz_goto_if_fail(iter, loop_end);
rz_break_if_fail(iter);
RzAnalysisVar *temp_var = rz_list_iter_get_data(iter);
const RzStackAddr off = temp_var->storage.stack_off;
const char sign = off >= 0 ? '+' : '-';
@ -1777,7 +1777,6 @@ static ut32 fold_variables(RzCore *core, RzDisasmState *ds, RzListIter /*<RzAnal
iter_mov++;
iter = rz_list_next(iter);
}
loop_end:
// remove extra "; " in tail
rz_strbuf_slice(sb, 0, sb->len - 2);
if (!strcmp(ds->fold_var, "hide")) {
@ -1830,10 +1829,9 @@ static void ds_show_fn_vars_lines(
if (iter_mov > 0) {
int cnt = 0;
while (cnt++ < iter_mov - 1) {
rz_goto_if_fail(iter, loop_end);
rz_break_if_fail(iter);
iter = rz_list_next(iter);
}
loop_end:
continue;
}
ds_show_fn_var_line(ds, f, var);

View file

@ -41,7 +41,7 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
} while (0)
/*
* RZ_CHECKS_LEVEL determines the behaviour of the rz_return/goto_* set of functions.
* RZ_CHECKS_LEVEL determines the behaviour of the rz_return/goto/break_* set of functions.
*
* 0: completely disable every function and make them like no-operation
* 1: silently enable checks. Check expressions and do return, but do not log anything
@ -70,11 +70,11 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
do { \
return (val); \
} while (0)
#define rz_goto_if_reached(where) \
do { \
goto where; \
} while (0)
#define rz_goto_if_fail(expr, where) \
do { \
if (!(expr)) { \
@ -82,6 +82,15 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
} \
} while (0)
#define rz_break_if_reached() \
{ \
break; \
}
#define rz_break_if_fail(expr) \
do { \
; \
} while (0)
#elif RZ_CHECKS_LEVEL == 1 || RZ_CHECKS_LEVEL == 2 // RZ_CHECKS_LEVEL
#if RZ_CHECKS_LEVEL == 1
@ -150,6 +159,20 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
} \
} while (0)
#define rz_break_if_reached() \
{ \
H_LOG_(RZ_LOGLVL_ERROR, "file %s: line %d (%s): should not be reached; exiting loop\n", __FILE__, __LINE__, RZ_FUNCTION); \
break; \
}
#define rz_break_if_fail(expr) \
{ \
if (!(expr)) { \
H_LOG_(RZ_LOGLVL_WARN, "%s: assertion '%s' failed (line %d); exiting loop\n", RZ_FUNCTION, #expr, __LINE__); \
break; \
} \
}
#else // RZ_CHECKS_LEVEL
#include <assert.h>
@ -170,12 +193,12 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
do { \
assert(false); \
} while (0)
#define rz_goto_if_reached(where) \
do { \
assert(false); \
goto where; \
} while (0)
#define rz_goto_if_fail(expr, where) \
do { \
assert(expr); \
@ -184,6 +207,19 @@ RZ_API void rz_assert_log(RzLogLevel level, const char *fmt, ...) RZ_PRINTF_CHEC
} \
} while (0)
#define rz_break_if_reached() \
{ \
assert(false); \
break; \
}
#define rz_break_if_fail(expr) \
{ \
assert(expr); \
if (!(expr)) { \
break; \
} \
}
#endif // RZ_CHECKS_LEVEL
#ifdef __cplusplus