From cc8a2b84bed2c8925aafa43bf3683a5af0d3e34c Mon Sep 17 00:00:00 2001 From: radare Date: Mon, 17 Sep 2018 12:27:11 +0200 Subject: [PATCH] Introduce new R_* macros to define direction, null, defaults, etc in function signatures (#11546) - Helps to identify some bugs at compile time - Useful for git grep and understand the usage of the functions - Improve readability - Fix memleak introduced in previous anal/var pr - Help to find unnecessary null checks and start to use r_assert - Deprecate R_TRUFAE R_TRUE R_FALSE and R_ERROR - Add r2api script to ease system-wide git grep for r2 devs - Use more R_OWN and r_return_val_if_fail --- libr/anal/var.c | 8 +++----- libr/include/r_types.h | 22 ++++++++++++++-------- libr/include/r_types_base.h | 6 ++---- libr/include/r_util/r_ctypes.h | 8 ++++---- libr/util/ctype.c | 15 +++++++-------- sys/r2api.sh | 14 ++++++++++++++ 6 files changed, 44 insertions(+), 29 deletions(-) create mode 100755 sys/r2api.sh diff --git a/libr/anal/var.c b/libr/anal/var.c index 7ed78a5f39..c7422054cc 100644 --- a/libr/anal/var.c +++ b/libr/anal/var.c @@ -68,11 +68,9 @@ R_API bool r_anal_var_display(RAnal *anal, int delta, char kind, const char *typ return true; } -R_API bool r_anal_var_add(RAnal *a, ut64 addr, int scope, int delta, char kind, const char *type, int size, - bool isarg, const char *name) { - if (!a) { - return false; - } +R_API bool r_anal_var_add(RAnal *a, ut64 addr, int scope, int delta, char kind, R_IFNULL("int") const char *type, int size, bool isarg, R_NONNULL const char *name) { + r_return_val_if_fail (a, false); + r_return_val_if_fail (name, false); if (!kind) { kind = R_ANAL_VAR_KIND_BPV; } diff --git a/libr/include/r_types.h b/libr/include/r_types.h index 9c1930afbe..29fd5328c5 100644 --- a/libr/include/r_types.h +++ b/libr/include/r_types.h @@ -13,6 +13,20 @@ #undef __UNIX__ #undef __WINDOWS__ +#define R_IN /* do not use, implicit */ +#define R_OWN /* pointer ownership is transferred */ +#define R_OUT /* parameter is written, not read */ +#define R_INOUT /* parameter is read and written */ +#define R_NULLABLE /* pointer can be null */ +#define R_IFNULL(x) /* default value for the pointer when null */ +#ifdef __GNUC__ +#define R_NONNULL __attribute__((__nonnull__)) +#define R_UNUSED __attribute__((__unused__)) +#else +#define R_NONNULL /* nonnull */ +#define R_UNUSED /* unused */ +#endif + #ifdef R_NEW #undef R_NEW #endif @@ -201,14 +215,6 @@ extern "C" { #define __packed __attribute__((__packed__)) #endif -#ifndef UNUSED -#ifdef __GNUC__ -#define UNUSED __attribute__((__unused__)) -#else -#define UNUSED -#endif -#endif - typedef int (*PrintfCallback)(const char *str, ...); // TODO NOT USED. DEPREACATE diff --git a/libr/include/r_types_base.h b/libr/include/r_types_base.h index e2c9c846d9..0702b79ad3 100644 --- a/libr/include/r_types_base.h +++ b/libr/include/r_types_base.h @@ -59,13 +59,11 @@ typedef struct _utX{ #include -#define R_ERROR -2 #define R_FAIL -1 -#define R_FALSE 0 -#define R_TRUE 1 -#define R_TRUFAE 2 +// must be deprecated #define R_NOTNULL (void*)(size_t)1 #define R_EMPTY { 0 } +#define R_EMPTY2 {{ 0 }} /* limits */ #undef UT64_MAX diff --git a/libr/include/r_util/r_ctypes.h b/libr/include/r_util/r_ctypes.h index 973d60c5f0..d89eed995f 100644 --- a/libr/include/r_util/r_ctypes.h +++ b/libr/include/r_util/r_ctypes.h @@ -36,10 +36,10 @@ R_API char *r_type_format(Sdb *TDB, const char *t); R_API int r_type_func_exist(Sdb *TDB, const char *func_name); R_API const char *r_type_func_cc(Sdb *TDB, const char *func_name); R_API const char *r_type_func_ret(Sdb *TDB, const char *func_name); -R_API int r_type_func_args_count(Sdb *TDB, const char *func_name); -R_API char *r_type_func_args_type(Sdb *TDB, const char *func_name, int i); -R_API char *r_type_func_args_name(Sdb *TDB, const char *func_name, int i); -R_API char *r_type_func_guess(Sdb *TDB, char *func_name); +R_API int r_type_func_args_count(Sdb *TDB, R_NONNULL const char *func_name); +R_API R_OWN char *r_type_func_args_type(Sdb *TDB, R_NONNULL const char *func_name, int i); +R_API const char *r_type_func_args_name(Sdb *TDB, R_NONNULL const char *func_name, int i); +R_API R_OWN char *r_type_func_guess(Sdb *TDB, R_NONNULL char *func_name); #ifdef __cplusplus } diff --git a/libr/util/ctype.c b/libr/util/ctype.c index e701a894b3..ab356bfbf4 100644 --- a/libr/util/ctype.c +++ b/libr/util/ctype.c @@ -1,6 +1,6 @@ /* radare - LGPL - Copyright 2013-2018 - pancake, oddcoder, sivaramaaa */ -#include "r_util.h" +#include R_API int r_type_set(Sdb *TDB, ut64 at, const char *field, ut64 val) { const char *kind; @@ -456,7 +456,7 @@ R_API int r_type_func_args_count(Sdb *TDB, const char *func_name) { return sdb_num_get (TDB, query, 0); } -R_API char *r_type_func_args_type(Sdb *TDB, const char *func_name, int i) { +R_API R_OWN char *r_type_func_args_type(Sdb *TDB, R_NONNULL const char *func_name, int i) { const char *query = sdb_fmt ("func.%s.arg.%d", func_name, i); char *ret = sdb_get (TDB, query, 0); if (ret) { @@ -470,7 +470,7 @@ R_API char *r_type_func_args_type(Sdb *TDB, const char *func_name, int i) { return NULL; } -R_API char *r_type_func_args_name(Sdb *TDB, const char *func_name, int i) { +R_API const char *r_type_func_args_name(Sdb *TDB, R_NONNULL const char *func_name, int i) { const char *query = sdb_fmt ("func.%s.arg.%d", func_name, i); const char *get = sdb_const_get (TDB, query, 0); if (get) { @@ -482,7 +482,7 @@ R_API char *r_type_func_args_name(Sdb *TDB, const char *func_name, int i) { #define MIN_MATCH_LEN 4 -static char *type_func_try_guess(Sdb *TDB, char *name) { +static R_OWN char *type_func_try_guess(Sdb *TDB, R_NONNULL char *name) { const char *res; if (r_str_nlen (name, MIN_MATCH_LEN) < MIN_MATCH_LEN) { return NULL; @@ -499,14 +499,13 @@ static char *type_func_try_guess(Sdb *TDB, char *name) { // TODO: // - symbol names are long and noisy, some of them might not be matched due // to additional information added around name -R_API char *r_type_func_guess(Sdb *TDB, char *func_name) { +R_API R_OWN char *r_type_func_guess(Sdb *TDB, R_NONNULL char *func_name) { int offset = 0; char *str = func_name; char *result = NULL; char *first, *last; - if (!func_name) { - return NULL; - } + r_return_val_if_fail (TDB, false); + r_return_val_if_fail (func_name, false); size_t slen = strlen (str); if (slen < MIN_MATCH_LEN) { diff --git a/sys/r2api.sh b/sys/r2api.sh new file mode 100755 index 0000000000..52b6953d0c --- /dev/null +++ b/sys/r2api.sh @@ -0,0 +1,14 @@ +#!/bin/sh +cd $HOME/prg/radare2 +IFS=: +for a in $PATH ; do + if [ -x "$a/radare2" ]; then + D=$(dirname `readlink /usr/local/bin/radare2`) + cd "$D/../.." + if [ -d libr/include ]; then + git grep "$1" libr/include | grep -v '#include' | less -p "$1" -R + exit 0 + fi + fi +done +echo "Cant find r2"