Skip to content
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

Merged
merged 31 commits into from
Aug 4, 2024

Conversation

satk0
Copy link
Contributor

@satk0 satk0 commented Jul 12, 2024

  • Mark this if you consider it ready to merge
  • I've added tests (optional)
  • I wrote some lines in the book (optional)

Description

Partially solves issue #20959

@satk0
Copy link
Contributor Author

satk0 commented Jul 12, 2024

I created a new macro R_STRDUP that aims to null check strdup.

I am thinking about creating another macro called R_STRNDUP aiming to replace r_str_ndup and r_str_newlen, wdyt?

@satk0 satk0 changed the title Deprecate r_str_new and R_STR_DUP WIP: Deprecate r_str_new and R_STR_DUP ##refactor Jul 12, 2024
Copy link
Collaborator

@trufae trufae left a 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.

@@ -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 ("");
Copy link
Collaborator

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

@@ -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);
Copy link
Collaborator

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

@@ -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"));
Copy link
Collaborator

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

} else if (len > 0) {
op->mnemonic = r_str_new ("invalid");
op->mnemonic = R_STRDUP ("invalid");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@trufae
Copy link
Collaborator

trufae commented Jul 13, 2024

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

@satk0
Copy link
Contributor Author

satk0 commented Jul 13, 2024

Maybe I would just remove R_STRDUP and use R_STR_DUP for consistency as well as create new R_STR_NDUP macro that would do what you wrote above?

@trufae
Copy link
Collaborator

trufae commented Jul 13, 2024

Sounds like a reasonable change 👍

@satk0
Copy link
Contributor Author

satk0 commented Jul 13, 2024

Don't look at that. I've just realized that I shouldn't be using strndup because it is not portable as you wrote. So may I just copy paste r_str_ndup as a R_STR_NDUP macro?

@@ -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);
Copy link
Collaborator

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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor Author

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

@satk0 satk0 force-pushed the deprecate-r-str-func branch 2 times, most recently from 1bed56b to 7f0fd4f Compare July 23, 2024 11:03
@satk0
Copy link
Contributor Author

satk0 commented Jul 23, 2024

@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 R_STR_DUP in favour of a new r_str_dup function (in lowercase) to be consistent with r_str_ndup, what do you think?

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 {
Copy link
Collaborator

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

@@ -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))));
Copy link
Collaborator

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);
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Collaborator

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

Copy link
Collaborator

@trufae trufae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

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);
Copy link
Collaborator

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

@@ -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)
Copy link
Collaborator

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

@@ -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);
Copy link
Collaborator

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

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strdup

@@ -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);
Copy link
Collaborator

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

@@ -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);
Copy link
Collaborator

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

@@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (len) {
if (len > 0) {

@@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strdup

@@ -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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

@satk0
Copy link
Contributor Author

satk0 commented Jul 27, 2024

Done but I am scared that it messed up the tests 😨

Copy link
Collaborator

@trufae trufae left a 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Collaborator

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;
Copy link
Collaborator

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"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r_list_append (line->sdbshell_hist, strdup ("\0"));

Copy link
Collaborator

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/core/cmd_help.inc.c Outdated Show resolved Hide resolved
@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret->fhash = R_STR_DUP (NULLVAL);
ret->fhash = strdup (NULLVAL);

this is never null

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Collaborator

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

@@ -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) {
Copy link
Collaborator

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) {
Copy link
Collaborator

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

@satk0
Copy link
Contributor Author

satk0 commented Jul 31, 2024

Thanks for the review. The biggest problem with r_str_newlen and r_str_ndup is that when length = 0 r_str_ndup returns \0 while r_str_newlen returns NULL. That's why I had to somehow walk around it.
Will macro like this one below do?

#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 😅

libr/arch/p/pyc/pyc_dis.c Outdated Show resolved Hide resolved
libr/bin/p/bin_lua.c Outdated Show resolved Hide resolved
@@ -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;
Copy link
Collaborator

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;

libr/core/cmd_anal.inc.c Outdated Show resolved Hide resolved
libr/core/cmd_help.inc.c Outdated Show resolved Hide resolved
libr/bin/p/bin_lua.c Outdated Show resolved Hide resolved
libr/core/canal.c Outdated Show resolved Hide resolved
libr/core/cmd_help.inc.c Outdated Show resolved Hide resolved
libr/core/cmd_help.inc.c Outdated Show resolved Hide resolved
libr/core/cmd_zign.inc.c Outdated Show resolved Hide resolved
libr/core/core.c Outdated Show resolved Hide resolved
libr/core/core.c Outdated Show resolved Hide resolved
libr/core/core.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@trufae trufae left a 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

satk0 and others added 9 commits August 3, 2024 17:01
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>
Copy link
Collaborator

@trufae trufae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

libr/util/file.c Outdated Show resolved Hide resolved
libr/util/file.c Outdated Show resolved Hide resolved
libr/util/file.c Outdated Show resolved Hide resolved
libr/util/file.c Outdated Show resolved Hide resolved
libr/util/str.c Outdated Show resolved Hide resolved
libr/util/str.c Outdated Show resolved Hide resolved
shlr/ar/ar.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@trufae trufae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..

libr/lang/p/lib.c Outdated Show resolved Hide resolved
shlr/ar/ar.c Outdated Show resolved Hide resolved
libr/util/str.c Outdated Show resolved Hide resolved
@trufae trufae merged commit aac8f33 into radareorg:master Aug 4, 2024
38 checks passed
@satk0 satk0 deleted the deprecate-r-str-func branch August 5, 2024 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants