Skip to content

Commit

Permalink
Remove some more of sdb_fmt() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Jan 24, 2024
1 parent 488a04f commit abe0352
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 28 deletions.
4 changes: 3 additions & 1 deletion librz/core/cagraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ RZ_IPI bool rz_core_agraph_add_shortcut(RzCore *core, RzAGraph *g, RzANode *an,
if (!shortcut) {
return false;
}
sdb_set(g->db, sdb_fmt("agraph.nodes.%s.shortcut", title), shortcut, 0);
char *key = rz_str_newf("agraph.nodes.%s.shortcut", title);
sdb_set(g->db, key, shortcut, 0);
free(key);
// title + "[o{shortcut}]", so w + 3 ?
an->shortcut_w = strlen(shortcut) + 3;
free(shortcut);
Expand Down
25 changes: 16 additions & 9 deletions librz/core/cmd/cmd_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,21 +1778,23 @@ RZ_IPI RzCmdStatus rz_analysis_syscall_show_handler(RzCore *core, int argc, cons
return RZ_CMD_STATUS_OK;
}

static const char *syscallNumber(int n) {
return sdb_fmt(n > 1000 ? "0x%x" : "%d", n);
static const char *syscallNumberFmt(int n) {
return n > 1000 ? "0x%x" : "%d";
}

RZ_IPI RzCmdStatus rz_analysis_syscall_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
char tmpbuf[32] = { 0 };
RzListIter *iter;
RzSyscallItem *si;
RzList *list = rz_syscall_list(core->analysis->syscall);
rz_cmd_state_output_array_start(state);
rz_list_foreach (list, iter, si) {
switch (state->mode) {
case RZ_OUTPUT_MODE_STANDARD:
rz_cons_printf("%s = 0x%02x.%s\n",
si->name, si->swi, syscallNumber(si->num));
case RZ_OUTPUT_MODE_STANDARD: {
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(si->num), si->num));
rz_cons_printf("%s = 0x%02x.%s\n", si->name, si->swi, syscallnum);
break;
}
case RZ_OUTPUT_MODE_JSON:
pj_o(state->d.pj);
pj_ks(state->d.pj, "name", si->name);
Expand All @@ -1816,7 +1818,8 @@ RZ_IPI RzCmdStatus rz_analysis_syscall_name_handler(RzCore *core, int argc, cons
RZ_LOG_ERROR("Cannot resolve syscall: %s\n", argv[1]);
return RZ_CMD_STATUS_ERROR;
}
rz_cons_println(syscallNumber(num));
rz_cons_printf(syscallNumberFmt(num), num);
rz_cons_print("\n");
return RZ_CMD_STATUS_OK;
}

Expand All @@ -1836,14 +1839,17 @@ RZ_IPI RzCmdStatus rz_analysis_syscall_number_handler(RzCore *core, int argc, co
}

static void syscall_dump(RzSyscallItem *si, bool is_c) {
char tmpbuf[32] = { 0 };
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(si->num), si->num);
if (is_c) {
rz_cons_printf("#define SYS_%s %s\n", si->name, syscallNumber(si->num));
rz_cons_printf("#define SYS_%s %s\n", si->name, syscallnum);
} else {
rz_cons_printf(".equ SYS_%s %s\n", si->name, syscallNumber(si->num));
rz_cons_printf(".equ SYS_%s %s\n", si->name, syscallnum);
}
}

static RzCmdStatus syscalls_dump(RzCore *core, int argc, const char **argv, bool is_c) {
char tmpbuf[32] = { 0 };
if (argc > 1) {
st64 n = rz_num_math(core->num, argv[1]);
if (n < 1) {
Expand All @@ -1852,7 +1858,8 @@ static RzCmdStatus syscalls_dump(RzCore *core, int argc, const char **argv, bool
RZ_LOG_ERROR("Cannot resolve syscall: %s\n", argv[1]);
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf(".equ SYS_%s %s\n", argv[1], syscallNumber(n));
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(n), n);
rz_cons_printf(".equ SYS_%s %s\n", argv[1], syscallnum);
return RZ_CMD_STATUS_OK;
}
RzSyscallItem *si = rz_syscall_get(core->analysis->syscall, n, -1);
Expand Down
3 changes: 2 additions & 1 deletion librz/core/cmd/cmd_magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ static int rz_core_magic_at(RzCore *core, const char *file, ut64 addr, int depth
}
{
const char *searchprefix = rz_config_get(core->config, "search.prefix");
const char *flag = sdb_fmt("%s%d_%d", searchprefix, 0, kw_count++);
char *flag = rz_str_newf("%s%d_%d", searchprefix, 0, kw_count++);
rz_flag_set(core->flags, flag, addr + adelta, 1);
free(flag);
}
// TODO: This must be a callback .. move this into RSearch?
if (!pj) {
Expand Down
8 changes: 4 additions & 4 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ static void annotated_hexdump(RzCore *core, int len) {
int i, j, low, max, here, rows;
bool marks = false, setcolor = true, hascolor = false;
ut8 ch = 0;
char tmpbuf[20] = { 0 };
char *colors[10] = { NULL };
for (i = 0; i < 10; i++) {
colors[i] = rz_cons_rainbow_get(i, 10, false);
Expand Down Expand Up @@ -1347,8 +1348,7 @@ static void annotated_hexdump(RzCore *core, int len) {
bytes = calloc(nb_cons_cols * 40, sizeof(char));
if (!bytes)
goto err_bytes;
#if 1
int addrpadlen = strlen(sdb_fmt("%08" PFMT64x, addr)) - 8;
int addrpadlen = strlen(rz_strf(tmpbuf, "%08" PFMT64x, addr)) - 8;
char addrpad[32];
if (addrpadlen > 0) {
memset(addrpad, ' ', addrpadlen);
Expand All @@ -1360,7 +1360,6 @@ static void annotated_hexdump(RzCore *core, int len) {
addrpadlen = 0;
}
strcpy(bytes + addrpadlen, "- offset - ");
#endif
j = strlen(bytes);
for (i = 0; i < nb_cols; i += 2) {
sprintf(bytes + j, format, (i & 0xf), (i + 1) & 0xf);
Expand Down Expand Up @@ -5409,9 +5408,10 @@ static void analysis_stats_json_info(RzCore *core, RzCoreAnalysisStats *as, RzCo
}

static void analysis_stats_table_info(RzCore *core, RzCoreAnalysisStats *as, RzCoreAnalysisStatsItem *sitem, ut64 blockidx, RzCmdStateOutput *state) {
char tmpbuf[20] = { 0 };
ut64 at = rz_core_analysis_stats_get_block_from(as, blockidx);
if ((sitem->flags) || (sitem->functions) || (sitem->comments) || (sitem->symbols) || (sitem->strings)) {
rz_table_add_rowf(state->d.t, "sddddd", sdb_fmt("0x%09" PFMT64x "", at), sitem->flags,
rz_table_add_rowf(state->d.t, "sddddd", rz_strf(tmpbuf, "0x%09" PFMT64x "", at), sitem->flags,
sitem->functions, sitem->comments, sitem->symbols, sitem->strings);
}
}
Expand Down
5 changes: 3 additions & 2 deletions librz/core/cmd/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -1993,12 +1993,13 @@ static bool do_analysis_search(RzCore *core, struct search_parameters *param, co
}
break;
case 's': { // "/als"
char tmpbuf[16] = { 0 };
RzListIter *iter;
RzSyscallItem *si;
RzList *list = rz_syscall_list(core->analysis->syscall);
rz_list_foreach (list, iter, si) {
rz_cons_printf("%s = 0x%02x.%s\n",
si->name, si->swi, syscallNumber(si->num));
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(si->num), si->num);
rz_cons_printf("%s = 0x%02x.%s\n", si->name, si->swi, syscallnum);
}
rz_list_free(list);
break;
Expand Down
11 changes: 7 additions & 4 deletions librz/core/csyscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <rz_core.h>

static const char *syscallNumber(int n) {
return sdb_fmt(n > 1000 ? "0x%x" : "%d", n);
static const char *syscallNumberFmt(int n) {
return n > 1000 ? "0x%x" : "%d";
}

/**
Expand All @@ -26,6 +26,7 @@ static const char *syscallNumber(int n) {
RZ_API RZ_OWN char *rz_core_syscall_as_string(RzCore *core, st64 n, ut64 addr) {
int i;
char str[64];
char tmpbuf[32] = { 0 };
st64 N = n;
int defVector = rz_syscall_get_swi(core->analysis->syscall);
if (defVector > 0) {
Expand All @@ -43,9 +44,11 @@ RZ_API RZ_OWN char *rz_core_syscall_as_string(RzCore *core, st64 n, ut64 addr) {
item = rz_syscall_get(core->analysis->syscall, N, -1);
}
if (!item) {
return rz_str_newf("%s = unknown ()", syscallNumber(n));
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(n), n);
return rz_str_newf("%s = unknown ()", syscallnum);
}
char *res = rz_str_newf("%s = %s (", syscallNumber(item->num), item->name);
const char *syscallnum = rz_strf(tmpbuf, syscallNumberFmt(item->num), item->num);
char *res = rz_str_newf("%s = %s (", syscallnum, item->name);
// TODO: move this to rz_syscall
const char *cc = rz_analysis_syscc_default(core->analysis);
// TODO replace the hardcoded CC with the sdb ones
Expand Down
3 changes: 2 additions & 1 deletion librz/io/p/io_winedbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ static struct winedbg_x86_32 regState(void) {
}

static char *__system(RzIO *io, RzIODesc *fd, const char *cmd) {
char tmpbuf[64];
if (!strcmp(cmd, "")) {
return NULL;
}
Expand Down Expand Up @@ -316,7 +317,7 @@ static char *__system(RzIO *io, RzIODesc *fd, const char *cmd) {
} else if (!strncmp(cmd, "dr", 2)) {
printcmd(io, "info reg");
} else if (!strncmp(cmd, "db ", 3)) {
free(runcmd(sdb_fmt("break *%x", rz_num_get(NULL, cmd + 3) || io->off)));
free(runcmd(rz_strf(tmpbuf, "break *%x", rz_num_get(NULL, cmd + 3) || io->off)));
} else if (!strncmp(cmd, "ds", 2)) {
free(runcmd("stepi"));
} else if (!strncmp(cmd, "dc", 2)) {
Expand Down
9 changes: 5 additions & 4 deletions librz/syscall/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,19 @@ RZ_API RzSyscallItem *rz_syscall_get(RzSyscall *s, int num, int swi) {
return NULL;
}
const char *ret, *ret2, *key;
char tmpbuf[64] = { 0 };
swi = getswi(s, swi);
if (swi < 16) {
key = sdb_fmt("%d.%d", swi, num);
key = rz_strf(tmpbuf, "%d.%d", swi, num);
} else {
key = sdb_fmt("0x%02x.%d", swi, num);
key = rz_strf(tmpbuf, "0x%02x.%d", swi, num);
}
ret = sdb_const_get(s->db, key, 0);
if (!ret) {
key = sdb_fmt("0x%02x.0x%02x", swi, num); // Workaround until Syscall SDB is fixed
key = rz_strf(tmpbuf, "0x%02x.0x%02x", swi, num); // Workaround until Syscall SDB is fixed
ret = sdb_const_get(s->db, key, 0);
if (!ret) {
key = sdb_fmt("0x%02x.%d", num, swi); // Workaround until Syscall SDB is fixed
key = rz_strf(tmpbuf, "0x%02x.%d", num, swi); // Workaround until Syscall SDB is fixed
ret = sdb_const_get(s->db, key, 0);
if (!ret) {
return NULL;
Expand Down
6 changes: 4 additions & 2 deletions subprojects/rzgdb/src/gdbclient/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ int gdbr_connect(libgdbr_t *g, const char *host, int port) {
int i;
int ret = -1;
void *bed = NULL;
char tmpbuf[16];

if (!g || !host) {
return -1;
Expand All @@ -179,7 +180,7 @@ int gdbr_connect(libgdbr_t *g, const char *host, int port) {
if (*host == '/') {
ret = rz_socket_connect_serial(g->sock, host, port, 1);
} else {
ret = rz_socket_connect_tcp(g->sock, host, sdb_fmt("%d", port), 1);
ret = rz_socket_connect_tcp(g->sock, host, rz_strf(tmpbuf, "%d", port), 1);
}
rz_cons_sleep_end(bed);
rz_cons_break_push(gdbr_break_process, g);
Expand Down Expand Up @@ -929,14 +930,15 @@ int gdbr_write_memory(libgdbr_t *g, ut64 address, const uint8_t *data, ut64 len)
int gdbr_step(libgdbr_t *g, int tid) {
int ret = -1;
char thread_id[64] = { 0 };
char tmpbuf[20] = { 0 };

if (!gdbr_lock_enter(g)) {
goto end;
}

if (tid <= 0 || write_thread_id(thread_id, sizeof(thread_id) - 1, g->pid, tid, g->stub_features.multiprocess) < 0) {
send_vcont(g, "vCont?", NULL);
send_vcont(g, sdb_fmt("Hc%d", tid), NULL);
send_vcont(g, rz_strf(tmpbuf, "Hc%d", tid), NULL);
ret = send_vcont(g, CMD_C_STEP, NULL);
goto end;
}
Expand Down

0 comments on commit abe0352

Please sign in to comment.