Skip to content

Commit

Permalink
New macro to handle zero length
Browse files Browse the repository at this point in the history
  • Loading branch information
satk0 committed Aug 2, 2024
1 parent d929fae commit 1d99bd4
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 53 deletions.
10 changes: 2 additions & 8 deletions libr/anal/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions libr/bin/p/bin_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions libr/core/cmd_help.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions libr/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
10 changes: 2 additions & 8 deletions libr/core/panels.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_util/r_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions libr/util/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "...");
}
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions libr/util/strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions shlr/ar/ar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 1d99bd4

Please sign in to comment.