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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
892aafd
Depreciate r_str_new
satk0 Jul 22, 2024
166629d
Depreciate r_str_newlen
satk0 Jul 23, 2024
69f6cac
Avoid R_STR_DUP when possible
satk0 Jul 23, 2024
9462af8
Fix tests
satk0 Jul 23, 2024
a04be5c
Fix linting
satk0 Jul 23, 2024
fe47a2e
Fixes according to previous review
satk0 Jul 23, 2024
7775f4f
Update libr/arch/p/pyc/opcode_arg_fmt.c
trufae Jul 24, 2024
e25725e
Change according to review
satk0 Jul 24, 2024
8156b09
Fix according to review
satk0 Jul 27, 2024
b61f3e4
Use strdup when not nullable
satk0 Aug 1, 2024
48f9f6a
Delete unused code
satk0 Aug 1, 2024
d929fae
Apply suggestions from code review
trufae Aug 1, 2024
1d99bd4
New macro to handle zero length
satk0 Aug 2, 2024
94ad228
Update libr/include/r_util/r_str.h according to the review
satk0 Aug 2, 2024
0a2e7b7
Update libr/include/r_util/r_str.h
trufae Aug 2, 2024
e9d0b75
Update libr/include/r_util/r_str.h
trufae Aug 2, 2024
e4e50d0
Update libr/include/r_util/r_str.h
trufae Aug 2, 2024
94e0dd1
Fix R_STR_NDUP macro to make msvc satisfied
satk0 Aug 2, 2024
2212961
Add test for r_str_ndup 0 len
satk0 Aug 3, 2024
d2e9e3c
Update libr/bin/format/xnu/r_cf_dict.c
satk0 Aug 3, 2024
e57bd7c
Update libr/bin/p/bin_lua.c
satk0 Aug 3, 2024
c283aa8
Update libr/core/canal.c
satk0 Aug 3, 2024
512a5fe
Update libr/core/cmd_help.inc.c
satk0 Aug 3, 2024
8f5fd8a
Update libr/core/cmd_help.inc.c
satk0 Aug 3, 2024
d1c4175
Update libr/core/cmd_zign.inc.c
satk0 Aug 3, 2024
48ecfd8
Update libr/core/core.c
satk0 Aug 3, 2024
34e5b92
Update libr/core/core.c
satk0 Aug 3, 2024
dd1a6b1
Update libr/core/core.c
satk0 Aug 3, 2024
1f658c6
Apply suggestions from code review
trufae Aug 4, 2024
3599ef7
Update shlr/ar/ar.c
trufae Aug 4, 2024
541d12c
Update libr/util/str.c
trufae Aug 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions libr/anal/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = strdup (k);
char *v2 = strdup (v);
if (!k2 || !v2) {
success = false;
goto out;
Expand All @@ -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 = R_STR_DUP (r_str_word_get0 (k2, 2));

// remove newline at end
char *save_ptr = NULL;
Expand Down Expand Up @@ -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;
}

}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion libr/anal/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ R_API const char *r_anal_value_type_tostring(RAnalValue *value) {
R_API char *r_anal_value_tostring(RAnalValue *value) {
char *out = NULL;
if (value) {
out = r_str_new ("");
out = strdup ("");
if (!value->base && !value->reg) {
if (value->imm != -1LL) {
out = r_str_appendf (out, "0x%"PFMT64x, value->imm);
Expand Down
4 changes: 2 additions & 2 deletions libr/anal/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ R_API RList *r_anal_var_deserialize(const char *ser) {
}
nxt++;
}
v->name = r_str_newlen (ser, i);
v->name = R_STR_NDUP (ser, i);
if (!v->name) {
goto bad_serial;
}
Expand All @@ -345,7 +345,7 @@ 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 = R_STR_NDUP (ser, i);
if (!v->type) {
goto bad_serial;
}
Expand Down
2 changes: 1 addition & 1 deletion libr/arch/arch_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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 = strdup ("");
if (!value->base && !value->reg) {
if (value->imm != -1LL) {
out = r_str_appendf (out, "0x%"PFMT64x, value->imm);
Expand Down
2 changes: 1 addition & 1 deletion libr/arch/p/8051/8051_disas.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = strdup (name);
}
break;
case 2:
Expand Down
6 changes: 3 additions & 3 deletions libr/arch/p/dalvik/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = strdup (r_str_get_fail (strasm, "invalid"));
} else if (len > 0) {
op->mnemonic = r_str_new ("invalid");
op->mnemonic = strdup ("invalid");
size = len;
}

Expand Down Expand Up @@ -794,7 +794,7 @@ static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
int sz = dalvik_opcodes[data[0]].len;
if (!op || sz > len) {
if (mask & R_ARCH_OP_MASK_DISASM) {
op->mnemonic = r_str_new ("invalid");
op->mnemonic = strdup ("invalid");
}
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions libr/arch/p/pyc/opcode_arg_fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ char *format_CALL_FUNCTION_KW_36(ut32 oparg) {
}

char *format_CALL_FUNCTION_EX_36(ut32 oparg) {
return r_str_new ((oparg & 0x01)? "keyword args": "");
return strdup ((oparg & 0x01)? "keyword args": "");
}

static const char *MAKE_FUNCTION_FLAGS[] = { "default", "keyword-only", "annotation", "closure" };

char *format_MAKE_FUNCTION_arg_36(ut32 oparg) {
size_t i;
char *ret = r_str_new (" ");
char *ret = strdup (" ");
for (i = 0; i < sizeof (MAKE_FUNCTION_FLAGS) / sizeof (char *); ++i) {
if (oparg & 0x1) {
ret = r_str_appendf (ret, ", %s", MAKE_FUNCTION_FLAGS[i]);
} else {
free (ret);
ret = r_str_new (MAKE_FUNCTION_FLAGS[i]);
ret = R_STR_DUP (MAKE_FUNCTION_FLAGS[i]);
}
oparg >>= 1;
}
Expand All @@ -60,7 +60,7 @@ char *format_value_flags_36(ut32 oparg) {
// empty fmt_spec.
ret = "";
}
return r_str_new (ret);
return R_STR_DUP (ret);
}

char *format_extended_arg_36(ut32 oparg) {
Expand Down
8 changes: 4 additions & 4 deletions libr/arch/p/pyc/pyc_dis.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ static char *parse_arg(pyc_opcode_object *op, ut32 oparg, pyc_code_object *cobj,
arg = r_str_newf ("'%s'", (char *)t->data);
break;
default:
arg = r_str_new (t->data);
arg = R_STR_DUP (t->data);
}
}
if (op->type & HASNAME) {
if (names) {
t = (pyc_object *)r_list_get_n (names, oparg);
if (t) {
return r_str_new (t->data);
return R_STR_DUP (t->data);
}
}
return NULL;
Expand All @@ -167,7 +167,7 @@ static char *parse_arg(pyc_opcode_object *op, ut32 oparg, pyc_code_object *cobj,
if (oparg < 0 || oparg >= CMP_OP_SIZE) {
return NULL;
}
arg = r_str_new (cmp_op[oparg]);
arg = strdup (cmp_op[oparg]);
}
if (op->type & HASFREE) {
if (!cellvars || !freevars) {
Expand All @@ -184,7 +184,7 @@ static char *parse_arg(pyc_opcode_object *op, ut32 oparg, pyc_code_object *cobj,
if (!t) {
return NULL;
}
arg = r_str_new (t->data);
arg = R_STR_DUP (t->data);
}
if (op->type & (HASVARGS | HASNARGS)) {
arg = r_str_newf ("%u", oparg);
Expand Down
4 changes: 2 additions & 2 deletions libr/bin/format/mach0/mach0.c
Original file line number Diff line number Diff line change
Expand Up @@ -4184,9 +4184,9 @@ ut64 MACH0_(get_baddr)(struct MACH0_(obj_t) *mo) {

char *MACH0_(get_class)(struct MACH0_(obj_t) *mo) {
#if R_BIN_MACH064
return r_str_new ("MACH064");
return strdup ("MACH064");
#else
return r_str_new ("MACH0");
return strdup ("MACH0");
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions libr/bin/format/xnu/r_cf_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ static RCFValue *r_cf_value_clone(RCFValue *value) {
RListIter *iter;
RCFKeyValue *item;
r_list_foreach (((RCFValueDict *)value)->pairs, iter, item) {
char *key = r_str_new (item->key);
char *key = strdup (item->key);
if (key) {
RCFValue *clone = r_cf_value_clone (item->value);
if (clone) {
Expand Down Expand Up @@ -714,7 +714,7 @@ static RCFValue *r_cf_value_clone(RCFValue *value) {
case R_CF_STRING: {
RCFValueString *string = R_NEW0 (RCFValueString);
if (string) {
string->value = r_str_new (((RCFValueString *)value)->value);
string->value = R_STR_DUP (((RCFValueString *)value)->value);
if (string->value) {
copy = (RCFValue *)string;
} else {
Expand Down
2 changes: 1 addition & 1 deletion libr/bin/p/bin_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static void addString(const ut8 *buf, ut64 offset, ut64 length, ParseStruct *par
return;
}

binstring->string = r_str_newlen ((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
2 changes: 1 addition & 1 deletion libr/core/anal_tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ static void type_match(RCore *core, char *fcn_name, ut64 addr, ut64 baddr, const
break;
}
const String *type_ = RVecString_at (&types, pos++);
type = type_ ? r_str_new (*type_) : NULL;
type = type_ ? R_STR_DUP (*type_) : NULL;
DD R_LOG_INFO ("TYPE (%s)", type);
} else {
type = r_type_func_args_type (TDB, fcn_name, arg_num);
Expand Down
4 changes: 2 additions & 2 deletions libr/core/canal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2452,7 +2452,7 @@ static void add_single_addr_xrefs(RCore *core, ut64 addr, RGraph *graph) {
}
RFlagItem *f = r_flag_get_at (core->flags, addr, false);
char *me = (f && f->offset == addr)
? r_str_new (f->name)
? strdup (f->name)
: r_str_newf ("0x%" PFMT64x, addr);

RGraphNode *curr_node = r_graph_add_node_info (graph, me, NULL, addr);
Expand All @@ -2472,7 +2472,7 @@ static void add_single_addr_xrefs(RCore *core, ut64 addr, RGraph *graph) {
continue;
}
RFlagItem *item = r_flag_get_i (core->flags, ref->addr);
char *src = item? r_str_new (item->name): r_str_newf ("0x%08" PFMT64x, ref->addr);
char *src = item? strdup (item->name): r_str_newf ("0x%08" PFMT64x, ref->addr);
RGraphNode *reference_from = r_graph_add_node_info (graph, src, NULL, ref->addr);
free (src);
r_graph_add_edge (graph, reference_from, curr_node);
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2969,7 +2969,7 @@ static bool bin_map_sections_to_segments(RBin *bin, PJ *pj, int mode) {

r_list_foreach (segments, iter, segment) {
RInterval segment_itv = (RInterval){segment->vaddr, segment->size};
char *tmp2 = r_str_new ("");
char *tmp2 = strdup ("");
r_list_foreach (sections, iter2, section) {
RInterval section_itv = (RInterval){section->vaddr, section->size};
if (r_itv_begin (section_itv) >= r_itv_begin (segment_itv) && r_itv_end (section_itv) <= r_itv_end (segment_itv) && section->name[0]) {
Expand Down
1 change: 0 additions & 1 deletion libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,6 @@ 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"));
}
RList *sdb_hist = line->sdbshell_hist;
r_line_set_hist_callback (line, &r_line_hist_sdb_up, &r_line_hist_sdb_down);
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10183,7 +10183,7 @@ static char *get_buf_asm(RCore *core, ut64 from, ut64 addr, RAnalFunction *fcn,
buf_asm = r_print_colorize_opcode (core->print, str,
core->cons->context->pal.reg, core->cons->context->pal.num, false, fcn ? fcn->addr : 0);
} else {
buf_asm = r_str_new (str);
buf_asm = strdup (str);
}
return buf_asm;
}
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_cmp.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = strdup (cmd);
if (!cmpw->cmd) {
free (cmpw);
return false;
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_debug.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = strdup (r->name);
valuestr = r_str_newf ("0x%"PFMT64x, value);
}
ut64 o_offset = core->offset;
Expand Down
4 changes: 2 additions & 2 deletions libr/core/cmd_help.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ static char *filterFlags(RCore *core, const char *msg) {
// find }
end = strchr (dollar + 2, '}');
if (end) {
word = r_str_newlen (dollar+2, end-dollar-2);
word = r_str_ndup (dollar + 2, end - dollar - 2);
end++;
} else {
msg = dollar + 1;
Expand All @@ -471,7 +471,7 @@ static char *filterFlags(RCore *core, const char *msg) {
if (!end) {
end = dollar + strlen (dollar);
}
word = r_str_newlen (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
4 changes: 2 additions & 2 deletions libr/core/cmd_zign.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ static double get_zb_threshold(RCore *core) {
static bool bestmatch_fcn(RCore *core, const char *input, bool json) {
r_return_val_if_fail (input && core, false);

char *argv = r_str_new (input);
char *argv = strdup (input);
if (!argv) {
return false;
}
Expand Down Expand Up @@ -1099,7 +1099,7 @@ static bool diff_zig(void *data, const char *input) {
return false;
}

char *argv = r_str_new (input);
char *argv = strdup (input);
if (!argv) {
return false;
}
Expand Down
Loading
Loading