From 1d99bd40e8bd312b3a757a7832368a6c9b60b299 Mon Sep 17 00:00:00 2001 From: satk0 Date: Fri, 2 Aug 2024 21:10:08 +0200 Subject: [PATCH] New macro to handle zero length --- libr/anal/var.c | 10 ++-------- libr/bin/p/bin_lua.c | 5 +---- libr/core/cmd_help.inc.c | 10 ++-------- libr/core/disasm.c | 5 +---- libr/core/panels.c | 10 ++-------- libr/include/r_util/r_str.h | 2 +- libr/util/file.c | 5 +---- libr/util/str.c | 10 ++-------- libr/util/strbuf.c | 5 +---- shlr/ar/ar.c | 5 +---- 10 files changed, 14 insertions(+), 53 deletions(-) diff --git a/libr/anal/var.c b/libr/anal/var.c index 183c8ca69e78a..91b335580c7af 100644 --- a/libr/anal/var.c +++ b/libr/anal/var.c @@ -334,10 +334,7 @@ R_API RList *r_anal_var_deserialize(const char *ser) { } nxt++; } - v->name = NULL; - if (i) { - v->name = r_str_ndup (ser, i); - } + v->name = R_STR_NDUP (ser, i); if (!v->name) { goto bad_serial; } @@ -348,10 +345,7 @@ R_API RList *r_anal_var_deserialize(const char *ser) { for (i = 0; *nxt && *nxt != ','; i++) { nxt++; } - v->type = NULL; - if (i) { - v->type = r_str_ndup (ser, i); - } + v->type = R_STR_NDUP (ser, i); if (!v->type) { goto bad_serial; } diff --git a/libr/bin/p/bin_lua.c b/libr/bin/p/bin_lua.c index c1660878a36d8..515402ec0c8ec 100644 --- a/libr/bin/p/bin_lua.c +++ b/libr/bin/p/bin_lua.c @@ -144,10 +144,7 @@ static void addString(const ut8 *buf, ut64 offset, ut64 length, ParseStruct *par return; } - binstring->string = NULL; - if (length > 0) { - binstring->string = r_str_ndup ((char *) buf + offset, length); - } + binstring->string = R_STR_NDUP ((char *) buf + offset, length); binstring->vaddr = binstring->paddr = offset; binstring->ordinal = 0; binstring->size = length; diff --git a/libr/core/cmd_help.inc.c b/libr/core/cmd_help.inc.c index 6976009893d1c..f5ad85e3a4589 100644 --- a/libr/core/cmd_help.inc.c +++ b/libr/core/cmd_help.inc.c @@ -455,10 +455,7 @@ static char *filterFlags(RCore *core, const char *msg) { // find } end = strchr (dollar + 2, '}'); if (end) { - word = NULL; - if (end - dollar - 2 > 0) { - word = r_str_ndup (dollar + 2, end - dollar - 2); - } + word = R_STR_NDUP (dollar + 2, end - dollar - 2); end++; } else { msg = dollar + 1; @@ -474,10 +471,7 @@ static char *filterFlags(RCore *core, const char *msg) { if (!end) { end = dollar + strlen (dollar); } - word = NULL; - if (end - dollar - 1 > 0) { - word = r_str_ndup (dollar+1, end-dollar-1); - } + word = R_STR_NDUP (dollar + 1, end - dollar - 1); } if (end && word) { ut64 val = r_num_math (core->num, word); diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 6ba554f49392e..ba956ab216765 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -1283,10 +1283,7 @@ static void ds_build_op_str(RDisasmState *ds, bool print_color) { char **wc_array = r_str_argv (wcdata, &argc); for (i = 0; i < argc; i++) { bgcolor = strchr (wc_array[i], '\x1b'); - word = NULL; - if (bgcolor - wc_array[i]) { - word = r_str_ndup (wc_array[i], bgcolor - wc_array[i]); - } + word = R_STR_NDUP (wc_array[i], bgcolor - wc_array[i]); ds_highlight_word (ds, word, bgcolor); } } diff --git a/libr/core/panels.c b/libr/core/panels.c index 6cdfb9598ca10..614702752113d 100644 --- a/libr/core/panels.c +++ b/libr/core/panels.c @@ -3923,10 +3923,7 @@ static char *__get_word_from_canvas_for_menu(RCore *core, RPanels *panels, int x tmp++; i++; } - char *ret = NULL; - if (i - strlen (padding)) { - ret = r_str_ndup (pos += strlen (padding), i - strlen (padding)); - } + char *ret = R_STR_NDUP (pos += strlen (padding), i - strlen (padding)); if (!ret) { ret = strdup (pos); } @@ -6572,10 +6569,7 @@ static char *__parse_panels_config(const char *cfg, int len) { if (R_STR_ISEMPTY (cfg) || len < 2) { return NULL; } - char *tmp = NULL; - if (len + 1) { - tmp = r_str_ndup (cfg, len + 1); - } + char *tmp = R_STR_NDUP (cfg, len + 1); if (!tmp) { return NULL; } diff --git a/libr/include/r_util/r_str.h b/libr/include/r_util/r_str.h index bface5c64901a..a592e17a070c3 100644 --- a/libr/include/r_util/r_str.h +++ b/libr/include/r_util/r_str.h @@ -65,8 +65,8 @@ typedef struct r_charset_t { #define R_STR_ISEMPTY(x) (!(x) || !*(x)) #define R_STR_ISNOTEMPTY(x) ((x) && *(x)) -// XXX must deprecate #define R_STR_DUP(x) (((x) != NULL) ? strdup ((x)) : NULL) +#define R_STR_NDUP(x, len) ((len > 0) ? r_str_ndup (x, len) : NULL) #define r_str_array(x,y) ((y >= 0 && y < (sizeof (x) / sizeof (*(x))))?(x)[(y)]: "") R_API RCharset *r_charset_new(void); R_API void r_charset_free(RCharset *charset); diff --git a/libr/util/file.c b/libr/util/file.c index 2fedd6e729d12..2d0e42ff10111 100644 --- a/libr/util/file.c +++ b/libr/util/file.c @@ -1549,10 +1549,7 @@ R_API RList* r_file_glob(const char *_globbed_path, int maxdepth) { if (last_slash) { glob_ptr = last_slash + 1; if (globbed_path[0] == '~') { - char *rpath = NULL; - if (last_slash - globbed_path - 1) { - rpath = r_str_ndup (globbed_path + 2, last_slash - globbed_path - 1); - } + char *rpath = R_STR_NDUP (globbed_path + 2, last_slash - globbed_path - 1); path = r_file_home (r_str_get (rpath)); free (rpath); } else { diff --git a/libr/util/str.c b/libr/util/str.c index ea6aa7773ce9c..d6c3480bac05c 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -672,10 +672,7 @@ R_API char *r_str_trunc_ellipsis(const char *str, int len) { if (strlen (str) < len) { return strdup (str); } - char *buf = NULL; - if (len > 0) { - buf = r_str_ndup (str, len); - } + char *buf = R_STR_NDUP (str, len); if (buf && len > 4) { strcpy (buf + len - 4, "..."); } @@ -820,10 +817,7 @@ R_API char *r_str_prepend(char *ptr, const char *string) { R_API char *r_str_appendlen(char *ptr, const char *string, int slen) { r_return_val_if_fail (string, NULL); - char *msg = NULL; - if (slen) { - msg = r_str_ndup (string, slen); - } + char *msg = R_STR_NDUP (string, slen); char *ret = r_str_append (ptr, msg); free (msg); return ret; diff --git a/libr/util/strbuf.c b/libr/util/strbuf.c index a681b7883ca2c..05c9020f7ebaa 100644 --- a/libr/util/strbuf.c +++ b/libr/util/strbuf.c @@ -130,10 +130,7 @@ R_API bool r_strbuf_slice(RStrBuf *sb, int from, int len) { const char *s = r_strbuf_get (sb); const char *fr = r_str_ansi_chrn (s, from + 1); const char *to = r_str_ansi_chrn (s, from + len + 1); - char *r = NULL; - if (to > fr) { - r = r_str_ndup (fr, to - fr); - } + char *r = R_STR_NDUP (fr, to - fr); r_strbuf_fini (sb); r_strbuf_init (sb); if (from >= len) { diff --git a/shlr/ar/ar.c b/shlr/ar/ar.c index 1b30afae197ec..bf34b6c97a7be 100644 --- a/shlr/ar/ar.c +++ b/shlr/ar/ar.c @@ -64,10 +64,7 @@ static char *name_from_table(ut64 off, filetable *tbl) { if (i == off) { return NULL; } - char *res = NULL; - if (i - off - 1 > 0) { - res = r_str_ndup (buf + off, i - off - 1); - } + char *res = R_STR_NDUP (buf + off, i - off - 1); return res; }