diff --git a/libr/anal/sign.c b/libr/anal/sign.c index 567c0fd1b9dd8..af2d84ba5f7d5 100644 --- a/libr/anal/sign.c +++ b/libr/anal/sign.c @@ -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; @@ -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; @@ -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); } } break; diff --git a/libr/anal/value.c b/libr/anal/value.c index 3aa19664a4eb6..c10234c5759ee 100644 --- a/libr/anal/value.c +++ b/libr/anal/value.c @@ -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); diff --git a/libr/anal/var.c b/libr/anal/var.c index 0c1f4a69a4f94..91b335580c7af 100644 --- a/libr/anal/var.c +++ b/libr/anal/var.c @@ -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; } @@ -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; } diff --git a/libr/arch/arch_value.c b/libr/arch/arch_value.c index f24a08513ae05..5f4f654650a89 100644 --- a/libr/arch/arch_value.c +++ b/libr/arch/arch_value.c @@ -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); diff --git a/libr/arch/p/8051/8051_disas.c b/libr/arch/p/8051/8051_disas.c index 83a4bd1115fcf..e2fc4cc86517e 100644 --- a/libr/arch/p/8051/8051_disas.c +++ b/libr/arch/p/8051/8051_disas.c @@ -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: diff --git a/libr/arch/p/dalvik/plugin.c b/libr/arch/p/dalvik/plugin.c index be29eaab0a523..7bee6d30bafcb 100644 --- a/libr/arch/p/dalvik/plugin.c +++ b/libr/arch/p/dalvik/plugin.c @@ -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; } @@ -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; } diff --git a/libr/arch/p/pyc/opcode_arg_fmt.c b/libr/arch/p/pyc/opcode_arg_fmt.c index 9974e5a0ca4cc..ead4bef322a19 100644 --- a/libr/arch/p/pyc/opcode_arg_fmt.c +++ b/libr/arch/p/pyc/opcode_arg_fmt.c @@ -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; } @@ -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) { diff --git a/libr/arch/p/pyc/pyc_dis.c b/libr/arch/p/pyc/pyc_dis.c index 43bbecb843eec..0ab97c5acc7bc 100644 --- a/libr/arch/p/pyc/pyc_dis.c +++ b/libr/arch/p/pyc/pyc_dis.c @@ -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; @@ -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) { @@ -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); diff --git a/libr/bin/format/mach0/mach0.c b/libr/bin/format/mach0/mach0.c index 46127f363289e..23bdbf72909a8 100644 --- a/libr/bin/format/mach0/mach0.c +++ b/libr/bin/format/mach0/mach0.c @@ -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 } diff --git a/libr/bin/format/xnu/r_cf_dict.c b/libr/bin/format/xnu/r_cf_dict.c index 6952982125735..a1686885839ca 100644 --- a/libr/bin/format/xnu/r_cf_dict.c +++ b/libr/bin/format/xnu/r_cf_dict.c @@ -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) { @@ -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 { diff --git a/libr/bin/p/bin_lua.c b/libr/bin/p/bin_lua.c index f1b80d5d1c84f..a18c0c33fa671 100644 --- a/libr/bin/p/bin_lua.c +++ b/libr/bin/p/bin_lua.c @@ -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; diff --git a/libr/core/anal_tp.c b/libr/core/anal_tp.c index ef42754374571..3be06b244347e 100644 --- a/libr/core/anal_tp.c +++ b/libr/core/anal_tp.c @@ -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); diff --git a/libr/core/canal.c b/libr/core/canal.c index 60e3f77b3689d..0e70b2c28fadc 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -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); @@ -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); diff --git a/libr/core/cbin.c b/libr/core/cbin.c index d698c23575314..8b7f07cdce9f0 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -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]) { diff --git a/libr/core/cmd.c b/libr/core/cmd.c index d6d299e8518a3..0cffd7ce54028 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -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); diff --git a/libr/core/cmd_anal.inc.c b/libr/core/cmd_anal.inc.c index 98f691e6210fb..5d51712d15238 100644 --- a/libr/core/cmd_anal.inc.c +++ b/libr/core/cmd_anal.inc.c @@ -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; } diff --git a/libr/core/cmd_cmp.inc.c b/libr/core/cmd_cmp.inc.c index 418df4c98dd16..8012f153819f7 100644 --- a/libr/core/cmd_cmp.inc.c +++ b/libr/core/cmd_cmp.inc.c @@ -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; diff --git a/libr/core/cmd_debug.inc.c b/libr/core/cmd_debug.inc.c index fe5d5dcf8d48e..ff1e3ef43afe9 100644 --- a/libr/core/cmd_debug.inc.c +++ b/libr/core/cmd_debug.inc.c @@ -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; diff --git a/libr/core/cmd_help.inc.c b/libr/core/cmd_help.inc.c index 361ecd557bf3a..5544dcfe574fb 100644 --- a/libr/core/cmd_help.inc.c +++ b/libr/core/cmd_help.inc.c @@ -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; @@ -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); diff --git a/libr/core/cmd_zign.inc.c b/libr/core/cmd_zign.inc.c index 09ff2181bf13b..94a7d8aace351 100644 --- a/libr/core/cmd_zign.inc.c +++ b/libr/core/cmd_zign.inc.c @@ -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; } @@ -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; } diff --git a/libr/core/core.c b/libr/core/core.c index 6191d323d8fb9..b4fb5e3689c58 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -1185,12 +1185,12 @@ static void autocomplete_ms_path(RLineCompletion *completion, RCore *core, const char *pwd = strdup (core->rfs->cwd? (const char *)core->rfs->cwd: "."); int n = 0; RFSFile *file; - char *lpath = r_str_new (path); + char *lpath = strdup (path); char *p = (char *)r_str_last (lpath, R_SYS_DIR); if (p) { *p = 0; if (p == lpath) { // /xxx - dirname = r_str_new ("/"); + dirname = strdup ("/"); } else if (lpath[0] == '.') { // ./xxx/yyy dirname = r_str_newf ("%s%s", pwd, R_SYS_DIR); } else if (lpath[0] == '/') { // /xxx/yyy @@ -1202,14 +1202,14 @@ static void autocomplete_ms_path(RLineCompletion *completion, RCore *core, const dirname = r_file_new (pwd, lpath, NULL); } } - basename = r_str_new (p + 1); + basename = strdup (p + 1); } else { // xxx if (strlen (pwd) == 1) { dirname = r_str_newf ("%s", R_SYS_DIR); } else { dirname = r_str_newf ("%s%s", pwd, R_SYS_DIR); } - basename = r_str_new (lpath); + basename = strdup (lpath); } R_FREE (pwd); @@ -1340,7 +1340,7 @@ static void autocomplete_process_path(RLineCompletion *completion, const char *s path++; } #endif - lpath = r_str_new (path); + lpath = strdup (path); #if R2__WINDOWS__ r_str_replace_ch (lpath, '/', '\\', true); #endif @@ -1351,7 +1351,7 @@ static void autocomplete_process_path(RLineCompletion *completion, const char *s #if R2__WINDOWS__ dirname = strdup ("\\.\\"); #else - dirname = r_str_new (R_SYS_DIR); + dirname = strdup (R_SYS_DIR); #endif } else if (lpath[0] == '~' && lpath[1]) { // ~/xxx/yyy dirname = r_file_home (lpath + 2); @@ -1372,10 +1372,10 @@ static void autocomplete_process_path(RLineCompletion *completion, const char *s #endif dirname = r_str_newf (fmt, R_SYS_DIR, lpath, R_SYS_DIR); } - basename = r_str_new (p + 1); + basename = strdup (p + 1); } else { // xxx dirname = r_str_newf (".%s", R_SYS_DIR); - basename = r_str_new (lpath); + basename = strdup (lpath); } if (!dirname || !basename) { @@ -1419,7 +1419,7 @@ static void autocomplete_filename(RLineCompletion *completion, RLineBuffer *buf, char *pipe = strchr (buf->data, '>'); if (pipe) { - args = r_str_new (pipe); + args = strdup (pipe); #if 0 if (pipe[1] == ' ') { // currently unreachable @@ -1427,7 +1427,7 @@ static void autocomplete_filename(RLineCompletion *completion, RLineBuffer *buf, } #endif } else { - args = r_str_new (buf->data); + args = R_STR_DUP (buf->data); } if (!args) { @@ -1439,7 +1439,7 @@ static void autocomplete_filename(RLineCompletion *completion, RLineBuffer *buf, goto out; } - input = r_str_new (r_str_word_get0 (args, narg)); + input = R_STR_DUP (r_str_word_get0 (args, narg)); if (!input) { goto out; } @@ -1608,7 +1608,7 @@ static void autocomplete_sdb(RCore *core, RLineCompletion *completion, const cha if (pipe) { str = r_str_trim_head_ro (pipe + 1); } - lpath = r_str_new (str); + lpath = strdup (str); p1 = strchr (lpath, '/'); if (p1) { *p1 = 0; diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 54955d0e900f3..ba956ab216765 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -1283,7 +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 = r_str_newlen (wc_array[i], bgcolor - wc_array[i]); + word = R_STR_NDUP (wc_array[i], bgcolor - wc_array[i]); ds_highlight_word (ds, word, bgcolor); } } @@ -4856,7 +4856,7 @@ static void ds_print_ptr(RDisasmState *ds, int len, int idx) { f = r_flag_get_i (core->flags, refaddr); if (f) { if (strlen (msg) != 1) { - char *msg2 = r_str_new (msg); + char *msg2 = strdup (msg); if (msg2) { r_str_filter (msg2, 0); if (!strncmp (msg2, "UH..", 4)) { diff --git a/libr/core/fortune.c b/libr/core/fortune.c index e2be4985f89f6..a4ee1673e4195 100644 --- a/libr/core/fortune.c +++ b/libr/core/fortune.c @@ -28,7 +28,7 @@ static bool _push_types(RList *type_list, char *fortune_dir) { char *file; r_list_foreach (files, iter, file) { if (r_str_startswith (file, "fortunes.") && file[9]) { - r_list_push (type_list, r_str_new (file + 9)); + r_list_push (type_list, strdup (file + 9)); } } r_list_free (files); @@ -102,7 +102,7 @@ static char *getrandomline(RCore *core) { return NULL; } const char *file = (const char *)r_list_get_n (types, r_num_rand (r_list_length (types))); - char *type = r_str_new (file); + char *type = R_STR_DUP (file); r_list_free (types); if (!type) { return NULL; diff --git a/libr/core/panels.c b/libr/core/panels.c index ca57f47fae0f0..614702752113d 100644 --- a/libr/core/panels.c +++ b/libr/core/panels.c @@ -514,7 +514,7 @@ static bool __check_panel_type(RPanel *panel, const char *type) { if (!panel || !panel->model->cmd || !type) { return false; } - char *tmp = r_str_new (panel->model->cmd); + char *tmp = strdup (panel->model->cmd); int n = r_str_split (tmp, ' '); if (!n) { free (tmp); @@ -623,14 +623,14 @@ static void __set_decompiler_cache(RCore *core, char *s) { RAnalFunction *func = r_anal_get_fcn_in (core->anal, core->offset, R_ANAL_FCN_TYPE_NULL); if (func) { if (core->panels_root->cur_pdc_cache) { - sdb_ptr_set (core->panels_root->cur_pdc_cache, r_num_as_string (NULL, func->addr, false), r_str_new (s), 0); + sdb_ptr_set (core->panels_root->cur_pdc_cache, r_num_as_string (NULL, func->addr, false), strdup (s), 0); } else { Sdb *sdb = sdb_new0 (); const char *pdc_now = r_config_get (core->config, "cmd.pdc"); - sdb_ptr_set (sdb, r_num_as_string (NULL, func->addr, false), r_str_new (s), 0); + sdb_ptr_set (sdb, r_num_as_string (NULL, func->addr, false), strdup (s), 0); core->panels_root->cur_pdc_cache = sdb; if (!sdb_exists (core->panels_root->pdc_caches, pdc_now)) { - sdb_ptr_set (core->panels_root->pdc_caches, r_str_new (pdc_now), sdb, 0); + sdb_ptr_set (core->panels_root->pdc_caches, strdup (pdc_now), sdb, 0); } } } @@ -639,7 +639,7 @@ static void __set_decompiler_cache(RCore *core, char *s) { static void __set_read_only(RCore *core, RPanel *p, char *s) { free (p->model->readOnly); - p->model->readOnly = r_str_new (s); + p->model->readOnly = strdup (s); __set_dcb (core, p); __set_pcb (p); } @@ -2294,7 +2294,7 @@ static int __show_all_decompiler_cb(void *user) { r_config_set (core->config, "cmd.pdc", opt); RPanel *panel = __get_panel (panels, i++); panels->n_panels = i; - panel->model->title = r_str_new (opt); + panel->model->title = strdup (opt); __set_read_only (core, panel, r_core_cmd_str (core, opt)); } __layout_equal_hor (panels); @@ -2317,7 +2317,7 @@ static void __init_modal_db(RCore *core) { SdbList *sdb_list = sdb_foreach_list (core->panels->db, true); ls_foreach (sdb_list, sdb_iter, kv) { const char *key = sdbkv_key (kv); - sdb_ptr_set (db, r_str_new (key), &__create_panel_db, 0); + sdb_ptr_set (db, strdup (key), &__create_panel_db, 0); } sdb_ptr_set (db, "Search strings in data sections", &__search_strings_data_create, 0); sdb_ptr_set (db, "Search strings in the whole bin", &__search_strings_bin_create, 0); @@ -2597,7 +2597,7 @@ static void __handle_tab_new_with_cur_panel(RCore *core) { RPanel *new_panel = __get_panel (new_panels, 0); __init_panel_param (core, new_panel, cur->model->title, cur->model->cmd); new_panel->model->cache = cur->model->cache; - new_panel->model->funcName = r_str_new (cur->model->funcName); + new_panel->model->funcName = strdup (cur->model->funcName); __set_cmd_str_cache (core, new_panel, cur->model->cmdStrCache); __maximize_panel_size (new_panels); @@ -3923,7 +3923,7 @@ static char *__get_word_from_canvas_for_menu(RCore *core, RPanels *panels, int x tmp++; i++; } - char *ret = r_str_newlen (pos += strlen (padding), i - strlen (padding)); + char *ret = R_STR_NDUP (pos += strlen (padding), i - strlen (padding)); if (!ret) { ret = strdup (pos); } @@ -4685,7 +4685,7 @@ static void __print_decompiler_cb(void *user, void *p) { return; #if 0 if (core->panels_root->cur_pdc_cache) { - cmdstr = r_str_new ((char *)sdb_ptr_get (core->panels_root->cur_pdc_cache, + cmdstr = R_STR_DUP ((char *)sdb_ptr_get (core->panels_root->cur_pdc_cache, r_num_as_string (NULL, func->addr, false), 0)); if (R_STR_ISNOTEMPTY (cmdstr)) { __set_cmd_str_cache (core, panel, cmdstr); @@ -5233,7 +5233,7 @@ static void __add_menu(RCore *core, const char *parent, const char *name, RPanel } item->n_sub = 0; item->selectedIndex = 0; - item->name = name ? r_str_new (name) : NULL; + item->name = R_STR_DUP (name); item->sub = NULL; item->cb = cb; item->p = R_NEW0 (RPanel); @@ -6569,7 +6569,7 @@ static char *__parse_panels_config(const char *cfg, int len) { if (R_STR_ISEMPTY (cfg) || len < 2) { return NULL; } - char *tmp = r_str_newlen (cfg, len + 1); + char *tmp = R_STR_NDUP (cfg, len + 1); if (!tmp) { return NULL; } @@ -7564,10 +7564,10 @@ R_API bool r_core_panels_root(RCore *core, RPanelsRoot *panels_root) { } const char *pdc_now = r_config_get (core->config, "cmd.pdc"); if (sdb_exists (panels_root->pdc_caches, pdc_now)) { - panels_root->cur_pdc_cache = sdb_ptr_get (panels_root->pdc_caches, r_str_new (pdc_now), 0); + panels_root->cur_pdc_cache = sdb_ptr_get (panels_root->pdc_caches, R_STR_DUP (pdc_now), 0); } else { Sdb *sdb = sdb_new0(); - sdb_ptr_set (panels_root->pdc_caches, r_str_new (pdc_now), sdb, 0); + sdb_ptr_set (panels_root->pdc_caches, R_STR_DUP (pdc_now), sdb, 0); panels_root->cur_pdc_cache = sdb; } } diff --git a/libr/core/visual.c b/libr/core/visual.c index f29023f4b028a..abb9e74f99733 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -218,7 +218,7 @@ static const char *__core_visual_print_command(RCore *core) { } if (r_config_get_b (core->config, "scr.dumpcols")) { free (core->stkcmd); - core->stkcmd = r_str_new (stackPrintCommand (core)); + core->stkcmd = R_STR_DUP (stackPrintCommand (core)); return printfmtColumns[PIDX]; } return printfmtSingle[PIDX]; diff --git a/libr/include/r_util/r_str.h b/libr/include/r_util/r_str.h index bface5c64901a..b3940d593030d 100644 --- a/libr/include/r_util/r_str.h +++ b/libr/include/r_util/r_str.h @@ -65,7 +65,6 @@ 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_array(x,y) ((y >= 0 && y < (sizeof (x) / sizeof (*(x))))?(x)[(y)]: "") R_API RCharset *r_charset_new(void); @@ -301,6 +300,9 @@ R_UNUSED static const char *r_str_skip_prefix(const char *str, const char *prefi } return str; } +static inline char *R_STR_NDUP(R_NULLABLE const char *x, int len) { + int _len = len; return (_len > 0) ? r_str_ndup (x, _len) : NULL; +} R_API bool r_str_endswith(const char *str, const char *needle); R_API bool r_str_isnumber(const char *str); R_API const char *r_str_last(const char *in, const char *ch); diff --git a/libr/lang/p/lib.c b/libr/lang/p/lib.c index 37388bb2718f6..761f03951c670 100644 --- a/libr/lang/p/lib.c +++ b/libr/lang/p/lib.c @@ -3,9 +3,9 @@ #include static bool lang_lib_file_run(RLangSession *user, const char *file) { - char *libpath; + char *libpath = R_STR_DUP (file); void *lib; - if (!(libpath = r_str_new (file))) { + if (!libpath) { return false; } if (!r_str_startswith (libpath, "/") && !r_str_startswith (libpath, "./")) { diff --git a/libr/main/ravc2.c b/libr/main/ravc2.c index d55df1eeda838..c63e502cd7330 100644 --- a/libr/main/ravc2.c +++ b/libr/main/ravc2.c @@ -139,13 +139,13 @@ R_API int r_main_ravc2(int argc, const char **argv) { free (rp); return 1; } - char *message = r_str_new (opt.argv[opt.ind + 1]); + char *message = R_STR_DUP (opt.argv[opt.ind + 1]); if (message) { RList *files = r_list_new(); if (files) { size_t i; for (i = 2; i < argc - 1; i++) { - char *file = r_str_new(argv[opt.ind + i]); + char *file = strdup (argv[opt.ind + i]); if (!file || !r_list_append (files, file)) { free (message); r_list_free (files); diff --git a/libr/main/rax2.c b/libr/main/rax2.c index a8a1f81d55e48..e07aab28bb225 100644 --- a/libr/main/rax2.c +++ b/libr/main/rax2.c @@ -688,7 +688,7 @@ static bool rax(RNum *num, char *str, int len, int last, RaxActions *flags, RaxM } if (flags->octal2raw) { // -o char *modified_str = (*str == '0') - ? r_str_new (str) + ? strdup (str) : r_str_newf ("0%s", str); const char *errstr = NULL; ut64 n = r_num_calc (num, modified_str, &errstr); diff --git a/libr/util/axml.c b/libr/util/axml.c index d3402ef9210f2..8f3d917eb9fe2 100644 --- a/libr/util/axml.c +++ b/libr/util/axml.c @@ -196,7 +196,7 @@ static char *resource_value(string_pool_t *pool, const ut8 *data, ut64 data_size resource_value_t *value) { switch (value->type) { case RESOURCE_NULL: - return r_str_new (""); + return strdup (""); case RESOURCE_REFERENCE: return r_str_newf ("@0x%x", value->data.d); case RESOURCE_STRING: @@ -213,7 +213,7 @@ static char *resource_value(string_pool_t *pool, const ut8 *data, ut64 data_size R_LOG_WARN ("Resource type is not recognized: %#x", value->type); break; } - return r_str_new ("null"); + return strdup ("null"); } static bool dump_element(PJ *pj, RStrBuf *sb, string_pool_t *pool, namespace_t *namespace, diff --git a/libr/util/file.c b/libr/util/file.c index a7567f7ef837e..86a39fa1935d7 100644 --- a/libr/util/file.c +++ b/libr/util/file.c @@ -1549,11 +1549,11 @@ 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 = r_str_newlen (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 { - path = r_str_newlen (globbed_path, last_slash - globbed_path + 1); + path = R_STR_NDUP (globbed_path, last_slash - globbed_path + 1); } } else { glob_ptr = globbed_path; diff --git a/libr/util/rvc_git.c b/libr/util/rvc_git.c index 15fe860eac054..cb80b74e300b3 100644 --- a/libr/util/rvc_git.c +++ b/libr/util/rvc_git.c @@ -23,7 +23,7 @@ static Rvc *open_git(const char *path) { if (!vc) { return NULL; } - vc->path = r_str_new (path); + vc->path = strdup (path); if (!vc->path) { free (vc); return NULL; @@ -182,7 +182,7 @@ R_API RList *branches_git(Rvc *rvc) { char *name; r_list_foreach (ret, iter, name) { if (*(char *)iter->data == '*') { - iter->data = r_str_new (name + 2); + iter->data = strdup (name + 2); free (name); } diff --git a/libr/util/rvc_rvc.c b/libr/util/rvc_rvc.c index fe9b30986481a..ddb52ef4e8593 100644 --- a/libr/util/rvc_rvc.c +++ b/libr/util/rvc_rvc.c @@ -66,7 +66,7 @@ static Rvc *rvc_rvc_new(const char *path) { R_LOG_ERROR ("Failed to create repo"); return NULL; } - rvc->path = r_str_new (path); + rvc->path = strdup (path); if (!rvc->path) { free (rvc); return NULL; @@ -183,15 +183,15 @@ static bool update_blobs(const RList *ignore, RList *blobs, const RList *nh) { if (strcmp (nh->head->data, blob->fname)) { continue; } - blob->fhash = r_str_new (nh->tail->data); + blob->fhash = R_STR_DUP (nh->tail->data); return (bool) blob->fhash; } blob = R_NEW (RvcBlob); if (!blob) { return false; } - blob->fhash = r_str_new (nh->tail->data); - blob->fname = r_str_new (nh->head->data); + blob->fhash = R_STR_DUP (nh->tail->data); + blob->fname = R_STR_DUP (nh->head->data); if (!blob->fhash || !blob->fname) { goto fail_ret; } @@ -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 = strdup (absp + r_str_len_utf8 (arp)); free (arp); if (!p) { return NULL; @@ -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 = strdup (NULLVAL); if (!ret->fhash) { goto fail_ret; } @@ -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 = strdup (b->fhash); free_blobs (blobs); return bhash; } @@ -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, strdup ((char *)kv->base.key + bplen)) && !ret->head->data) { r_list_free (ret); ret = NULL; @@ -944,7 +944,7 @@ R_API char *curbranch_rvc(Rvc *rvc) { if (!rvc->db) { return NULL; } - char *ret = r_str_new (sdb_const_get (rvc->db, CURRENTB, 0) + char *ret = R_STR_DUP (sdb_const_get (rvc->db, CURRENTB, 0) + r_str_len_utf8 (BPREFIX)); return ret; } @@ -1162,7 +1162,7 @@ static RList *uncommited_rvc(Rvc *rvc) { continue; } free (rfp); - char *append = r_str_new (file); + char *append = R_STR_DUP (file); if (!append) { goto fail_ret; } diff --git a/libr/util/str.c b/libr/util/str.c index 950d8aafd470e..6378de5e0de96 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -672,7 +672,7 @@ 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 = r_str_ndup (str, len); if (buf && len > 4) { strcpy (buf + len - 4, "..."); } @@ -817,7 +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 = r_str_newlen (string, slen); + char *msg = R_STR_NDUP (string, slen); // only fails for token.c char *ret = r_str_append (ptr, msg); free (msg); return ret; @@ -2417,7 +2417,7 @@ R_API bool r_str_glob(const char* str, const char *glob) { needle_end++; } // Find the pattern in between wildcards - char* needle = r_str_ndup(glob, needle_end - glob); + char* needle = r_str_ndup (glob, needle_end - glob); const char *advance_to = strstr (str, needle); free (needle); if (!advance_to) { diff --git a/libr/util/strbuf.c b/libr/util/strbuf.c index 855b5f3165336..05c9020f7ebaa 100644 --- a/libr/util/strbuf.c +++ b/libr/util/strbuf.c @@ -130,7 +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 = r_str_newlen (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/libr/util/syscmd.c b/libr/util/syscmd.c index 32fc642e73615..238f1862fa20b 100644 --- a/libr/util/syscmd.c +++ b/libr/util/syscmd.c @@ -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 = strdup (field); char *first = strchr (str1, ' '); char *second = strchr (str2, ' '); out = r_str_append (out, r_str_get_fail (first, " ")); diff --git a/libr/util/table.c b/libr/util/table.c index efb82615f660d..8ef8885ddacb9 100644 --- a/libr/util/table.c +++ b/libr/util/table.c @@ -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, strdup (r_str_bool (va_arg (ap, int)))); break; case 'i': case 'd': diff --git a/shlr/ar/ar.c b/shlr/ar/ar.c index 77a9421cba18d..24172012206cb 100644 --- a/shlr/ar/ar.c +++ b/shlr/ar/ar.c @@ -64,7 +64,7 @@ static char *name_from_table(ut64 off, filetable *tbl) { if (i == off) { return NULL; } - return r_str_newlen (buf + off, i - off - 1); + return R_STR_NDUP (buf + off, i - off - 1); } #define VERIFY_AR_NUM_FIELD(x, s) \ diff --git a/test/unit/test_str.c b/test/unit/test_str.c index aac979e735652..e7d5e616c07ec 100644 --- a/test/unit/test_str.c +++ b/test/unit/test_str.c @@ -671,6 +671,15 @@ bool test_r_mem_to_binstring(void) { mu_end; } +bool test_r_str_ndup_zero_len (void) { + char str[] = "deadbeef"; + + mu_assert_null (R_STR_NDUP (str, 0), "uppercase yields NULL"); + mu_assert_streq (r_str_ndup (str, 0), "", "lowercase yields empty string"); + + mu_end; +} + bool all_tests(void) { mu_run_test (test_r_str_wrap); mu_run_test (test_r_str_newf); @@ -707,6 +716,7 @@ bool all_tests(void) { mu_run_test (test_r_str_tok_r); mu_run_test (test_r_mem_from_binstring); mu_run_test (test_r_mem_to_binstring); + mu_run_test (test_r_str_ndup_zero_len); return tests_passed != tests_run; }