-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Deprecate r_str_new and R_STR_DUP ##refactor #23119
Conversation
I created a new macro I am thinking about creating another macro called |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use only R_STRDUP when we can't control if the string passed can be null, use strdup() instead of r_str_new. the idea behind R_STR_DUP instead of R_STRDUP was to follow the R_STR_
prefix like it is for r_str
for consistency.
libr/arch/arch_value.c
Outdated
@@ -108,7 +108,7 @@ R_API bool r_arch_value_set_ut64(RArchValue *val, RReg *reg, RIOBind *iob, ut64 | |||
R_API char *r_arch_value_tostring(RArchValue *value) { | |||
char *out = NULL; | |||
if (value) { | |||
out = r_str_new (""); | |||
out = R_STRDUP (""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be just strdup(), there's no need for a null check here
libr/arch/p/8051/8051_disas.c
Outdated
@@ -31,7 +31,7 @@ static char *r_8051_disas(ut64 pc, const ut8 *buf, int len, int *olen) { | |||
// op @Ri; op Rn | |||
disasm = r_str_newf (name, buf[0] & mask); | |||
} else { | |||
disasm = r_str_new (name); | |||
disasm = R_STRDUP (name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think name cant be null here
libr/arch/p/dalvik/plugin.c
Outdated
@@ -761,9 +761,9 @@ static int dalvik_disassemble(RArchSession *as, RAnalOp *op, ut64 addr, const ut | |||
R_FREE (strasm); | |||
size = 2; | |||
} | |||
op->mnemonic = r_str_new (r_str_get_fail (strasm, "invalid")); | |||
op->mnemonic = R_STRDUP (r_str_get_fail (strasm, "invalid")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strdup no null check here
libr/arch/p/dalvik/plugin.c
Outdated
} else if (len > 0) { | ||
op->mnemonic = r_str_new ("invalid"); | ||
op->mnemonic = R_STRDUP ("invalid"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
the story about strndup (and R_STR_NDUP) is because strndup is NOT portable. so that's why we have r_str_ndup and imho this function should be checking for null at ocmpile time with an R_RETURN statement |
Maybe I would just remove |
Sounds like a reasonable change 👍 |
6e53eb2
to
48702f7
Compare
Don't look at that. I've just realized that I shouldn't be using |
@@ -343,7 +343,7 @@ R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char | |||
if (token[0] != 0) { | |||
it->hash = R_NEW0 (RSignHash); | |||
if (it->hash) { | |||
it->hash->bbhash = r_str_new (token); | |||
it->hash->bbhash = strdup (token); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats not the same, r_str_new checks for null and returns null. so the behaviour may differ because the code expects to have bbhash=NULL
R_API char *r_str_new(const char *str) {
return str? strdup (str): NULL;
}
libr/anal/sign.c
Outdated
@@ -248,7 +248,7 @@ R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char | |||
} | |||
|
|||
it->space = r_spaces_add (&a->zign_spaces, r_str_word_get0 (k2, 1)); | |||
it->name = r_str_new (r_str_word_get0 (k2, 2)); | |||
it->name = strdup (r_str_word_get0 (k2, 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's why it is better to be careful when making changes like this. Previously when I was digging with it I messed up to such an extend that even r2 -v
segfaulted XD
1bed56b
to
7f0fd4f
Compare
@trufae I finally managed to fix the CI/CD and I think it is ready to merge. The only thing I think about is to whether depreciate |
shlr/ar/ar.c
Outdated
return r_str_newlen (buf + off, i - off - 1); | ||
if (i - off - 1) { | ||
return r_str_ndup (buf + off, i - off - 1); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not else after a return
libr/util/table.c
Outdated
@@ -267,7 +267,7 @@ R_API void r_table_add_rowf(RTable *t, const char *fmt, ...) { | |||
r_list_append (list, r_str_newf ("%.03lf", va_arg (ap, double))); | |||
break; | |||
case 'b': | |||
r_list_append (list, r_str_new (r_str_bool (va_arg (ap, int)))); | |||
r_list_append (list, R_STR_DUP (r_str_bool (va_arg (ap, int)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strdup is fine, this cant be null
@@ -672,7 +672,10 @@ R_API char *r_str_trunc_ellipsis(const char *str, int len) { | |||
if (strlen (str) < len) { | |||
return strdup (str); | |||
} | |||
char *buf = r_str_newlen (str, len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r_str_newlen is deprecated too, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe r_str_ndup should handle negative length in the same way or we shuol dhave a macro for that, as it seems the alternative is made in so many different places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
676c58c
to
6945855
Compare
libr/anal/sign.c
Outdated
@@ -227,8 +227,8 @@ R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char | |||
R_RETURN_VAL_IF_FAIL (a && it && k && v, false); | |||
|
|||
bool success = true; | |||
char *k2 = r_str_new (k); | |||
char *v2 = r_str_new (v); | |||
char *k2 = R_STR_DUP (k); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K and v cant be null
libr/core/canal.c
Outdated
@@ -2442,7 +2442,7 @@ static void add_single_addr_xrefs(RCore *core, ut64 addr, RGraph *graph) { | |||
r_return_if_fail (graph); | |||
RFlagItem *f = r_flag_get_at (core->flags, addr, false); | |||
char *me = (f && f->offset == addr) | |||
? r_str_new (f->name) | |||
? R_STR_DUP (f->name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fname cant be null is strdup
libr/core/cmd_cmp.inc.c
Outdated
@@ -112,7 +112,7 @@ R_API bool r_core_cmpwatch_add(RCore *core, ut64 addr, int size, const char *cmd | |||
found = true; | |||
} | |||
cmpw->size = size; | |||
cmpw->cmd = r_str_new (cmd); | |||
cmpw->cmd = R_STR_DUP (cmd); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here use strdup imho. We should use more R_NULLABLE
libr/core/cmd_debug.inc.c
Outdated
@@ -2163,7 +2163,7 @@ R_API void r_core_debug_rr(RCore *core, RReg *reg, int mode) { | |||
valuestr = r_str_newf ("%s0x%"PFMT64x"%s", color, value, colorend); | |||
r_cons_print (Color_RESET); | |||
} else { | |||
namestr = r_str_new (r->name); | |||
namestr = R_STR_DUP (r->name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strdup
libr/core/cmd_zign.inc.c
Outdated
@@ -1099,7 +1099,7 @@ static bool diff_zig(void *data, const char *input) { | |||
return false; | |||
} | |||
|
|||
char *argv = r_str_new (input); | |||
char *argv = R_STR_DUP (input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cant be null. Its asserted in the rreturn statement
libr/util/syscmd.c
Outdated
@@ -483,7 +483,7 @@ R_API char *r_syscmd_join(const char *file1, const char *file2) { | |||
} | |||
r_list_foreach (list2, iter2, str2) { | |||
if (r_str_startswith (str2, field)) { | |||
char *out = r_str_new (field); | |||
char *out = R_STR_DUP (field); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strdup cant be null here
libr/util/strbuf.c
Outdated
@@ -130,7 +130,10 @@ 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 = r_str_newlen (fr, to - fr); | |||
char *r = NULL; | |||
if (to - fr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (to - fr) { | |
if (to > fr) { |
libr/util/str.c
Outdated
@@ -672,7 +672,10 @@ R_API char *r_str_trunc_ellipsis(const char *str, int len) { | |||
if (strlen (str) < len) { | |||
return strdup (str); | |||
} | |||
char *buf = r_str_newlen (str, len); | |||
char *buf = NULL; | |||
if (len) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (len) { | |
if (len > 0) { |
libr/util/rvc_rvc.c
Outdated
@@ -716,7 +716,7 @@ R_API RList *branches_rvc(Rvc *rvc) { | |||
if (!r_str_startswith ((char *)kv->base.key, BPREFIX)) { | |||
continue; | |||
} | |||
if (!r_list_append (ret, r_str_new ((char *)kv->base.key + bplen)) | |||
if (!r_list_append (ret, R_STR_DUP ((char *)kv->base.key + bplen)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strdup
libr/util/rvc_rvc.c
Outdated
@@ -325,7 +325,7 @@ static char *absp2rp(Rvc *rvc, const char *absp) { | |||
free (arp); | |||
return NULL; | |||
} | |||
char *p = r_str_new (absp + r_str_len_utf8 (arp)); | |||
char *p = R_STR_DUP (absp + r_str_len_utf8 (arp)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
6945855
to
8274a6a
Compare
Done but I am scared that it messed up the tests 😨 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slowly getting there 💪
libr/anal/sign.c
Outdated
@@ -343,7 +343,7 @@ R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char | |||
if (token[0] != 0) { | |||
it->hash = R_NEW0 (RSignHash); | |||
if (it->hash) { | |||
it->hash->bbhash = r_str_new (token); | |||
it->hash->bbhash = R_STR_DUP (token); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it->hash->bbhash = R_STR_DUP (token); | |
it->hash->bbhash = strdup (token); |
libr/anal/var.c
Outdated
@@ -334,7 +334,10 @@ R_API RList *r_anal_var_deserialize(const char *ser) { | |||
} | |||
nxt++; | |||
} | |||
v->name = r_str_newlen (ser, i); | |||
v->name = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont assign twice. use else or the ternary operator here. also cbetter check for i > 0 instead of just 'i'
libr/anal/var.c
Outdated
@@ -345,7 +348,10 @@ R_API RList *r_anal_var_deserialize(const char *ser) { | |||
for (i = 0; *nxt && *nxt != ','; i++) { | |||
nxt++; | |||
} | |||
v->type = r_str_newlen (ser, i); | |||
v->type = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
libr/core/cmd.c
Outdated
@@ -2522,7 +2522,7 @@ static int cmd_kuery(void *data, const char *input) { | |||
RLine *line = core->cons->line; | |||
if (!line->sdbshell_hist) { | |||
line->sdbshell_hist = r_list_newf (free); | |||
r_list_append (line->sdbshell_hist, r_str_new ("\0")); | |||
r_list_append (line->sdbshell_hist, strdup ("\0")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r_list_append (line->sdbshell_hist, strdup ("\0")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont understand this line at all. i think is better to remove it. also "\0" is the same as "". so appending an empty line to the history should be the same as doing nothing. so lets remove the whole line and see if anything breaks fix it somewhere else
libr/util/rvc_rvc.c
Outdated
@@ -352,7 +352,7 @@ static RvcBlob *bfadd(Rvc *rvc, const char *fname) { | |||
return NULL; | |||
} | |||
if (!r_file_exists (absp)) { | |||
ret->fhash = r_str_new (NULLVAL); | |||
ret->fhash = R_STR_DUP (NULLVAL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ret->fhash = R_STR_DUP (NULLVAL); | |
ret->fhash = strdup (NULLVAL); |
this is never null
libr/util/rvc_rvc.c
Outdated
@@ -642,7 +642,7 @@ static char *find_blob_hash(Rvc *rvc, const char *fname) { | |||
RvcBlob *b; | |||
r_list_foreach_prev (blobs, i, b) { | |||
if (!strcmp (b->fname, fname)) { | |||
char *bhash = r_str_new (b->fhash); | |||
char *bhash = R_STR_DUP (b->fhash); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
char *bhash = R_STR_DUP (b->fhash); | |
char *bhash = strdup (b->fhash); |
@@ -672,7 +672,10 @@ R_API char *r_str_trunc_ellipsis(const char *str, int len) { | |||
if (strlen (str) < len) { | |||
return strdup (str); | |||
} | |||
char *buf = r_str_newlen (str, len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe r_str_ndup should handle negative length in the same way or we shuol dhave a macro for that, as it seems the alternative is made in so many different places
libr/util/strbuf.c
Outdated
@@ -130,7 +130,10 @@ 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 = r_str_newlen (fr, to - fr); | |||
char *r = NULL; | |||
if (to > fr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use ternary operator. dont compute to-fr twice, old api looks better imho
shlr/ar/ar.c
Outdated
@@ -64,7 +64,11 @@ static char *name_from_table(ut64 off, filetable *tbl) { | |||
if (i == off) { | |||
return NULL; | |||
} | |||
return r_str_newlen (buf + off, i - off - 1); | |||
char *res = NULL; | |||
if (i - off - 1 > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same ehre, dont compute length twice
Thanks for the review. The biggest problem with #define R_STR_NDUP_NULL(str, len) ((len) ? r_str_ndup (str, len) : NULL) or should I use ternary operators instead? I am a bit confused what to do with it 😅 |
8274a6a
to
040c7cf
Compare
libr/bin/p/bin_lua.c
Outdated
@@ -144,7 +144,10 @@ static void addString(const ut8 *buf, ut64 offset, ut64 length, ParseStruct *par | |||
return; | |||
} | |||
|
|||
binstring->string = r_str_newlen ((char *) buf + offset, length); | |||
binstring->string = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assign this once. use ternary operator line binstring->string = (length > 0)? ...: NULL;
b5d113b
to
1d99bd4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully the last ones
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
Co-authored-by: pancake <pancake@nowsecure.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..
Description
Partially solves issue #20959