Skip to content

Commit 612a3ec

Browse files
authored
Fix va_arg issue in r_str_newf and r_str_appendf ##core
1 parent 982c950 commit 612a3ec

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

libr/cons/hud.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static RList *hud_filter(RList *list, char *user_input, int top_entry_n, int *cu
125125
p = strdup (current_entry);
126126
// if the filter is empty, print the entry and move on
127127
if (!user_input[0]) {
128-
r_list_append (res, r_str_newf (" %c %s", first_line? '-': ' ', current_entry));
128+
r_list_append (res, r_str_newf (" %c %s", first_line? '-': ' ', p));
129129
} else {
130130
// otherwise we need to emphasize the matching part
131131
if (I (context->color)) {

libr/core/cmd_anal.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,6 @@ static const char *syscallNumber(int n) {
13121312
}
13131313

13141314
R_API char *cmd_syscall_dostr(RCore *core, int n, ut64 addr) {
1315-
char *res = NULL;
13161315
int i;
13171316
char str[64];
13181317
int defVector = r_syscall_get_swi (core->anal->syscall);
@@ -1330,11 +1329,9 @@ R_API char *cmd_syscall_dostr(RCore *core, int n, ut64 addr) {
13301329
? r_syscall_get (core->anal->syscall, n, -1)
13311330
: r_syscall_get (core->anal->syscall, 0, n);
13321331
if (!item) {
1333-
res = r_str_appendf (res, "%s = unknown ()", syscallNumber (n));
1334-
return res;
1332+
return r_str_newf ("%s = unknown ()", syscallNumber (n));
13351333
}
1336-
1337-
res = r_str_appendf (res, "%s = %s (", syscallNumber (item->num), item->name);
1334+
char *res = r_str_newf ("%s = %s (", syscallNumber (item->num), item->name);
13381335
// TODO: move this to r_syscall
13391336
//TODO replace the hardcoded CC with the sdb ones
13401337
for (i = 0; i < item->args; i++) {
@@ -1384,8 +1381,7 @@ R_API char *cmd_syscall_dostr(RCore *core, int n, ut64 addr) {
13841381
}
13851382
}
13861383
r_syscall_item_free (item);
1387-
res = r_str_appendf (res, ")");
1388-
return res;
1384+
return r_str_appendf (res, ")");
13891385
}
13901386

13911387
static void cmd_syscall_do(RCore *core, int n, ut64 addr) {

libr/util/str.c

+22-40
Original file line numberDiff line numberDiff line change
@@ -711,42 +711,24 @@ R_API char *r_str_trunc_ellipsis(const char *str, int len) {
711711
return buf;
712712
}
713713

714-
// Returns a new heap-allocated string that matches the format-string
715-
// specification.
716714
R_API char *r_str_newf(const char *fmt, ...) {
717-
int ret, ret2;
718-
char *tmp, *p, string[1024];
719715
va_list ap, ap2;
716+
720717
va_start (ap, fmt);
721-
va_start (ap2, fmt);
722718
if (!strchr (fmt, '%')) {
723-
va_end (ap2);
724719
va_end (ap);
725720
return strdup (fmt);
726721
}
727-
ret = vsnprintf (string, sizeof (string) - 1, fmt, ap);
728-
if (ret < 1 || ret >= sizeof (string)) {
729-
p = calloc (1, ret + 3);
730-
if (!p) {
731-
va_end (ap2);
732-
va_end (ap);
733-
return NULL;
734-
}
735-
ret2 = vsnprintf (p, ret + 1, fmt, ap2);
736-
if (ret2 < 1 || ret2 > ret + 1) {
737-
free (p);
738-
va_end (ap2);
739-
va_end (ap);
740-
return NULL;
741-
}
742-
tmp = r_str_new (p);
743-
free (p);
744-
} else {
745-
tmp = r_str_new (string);
722+
va_copy (ap2, ap);
723+
int ret = vsnprintf (NULL, 0, fmt, ap2);
724+
ret++;
725+
char *p = calloc (1, ret);
726+
if (p) {
727+
(void)vsnprintf (p, ret, fmt, ap);
746728
}
747729
va_end (ap2);
748730
va_end (ap);
749-
return tmp;
731+
return p;
750732
}
751733

752734
// Secure string copy with null terminator (like strlcpy or strscpy but ours
@@ -908,30 +890,30 @@ R_API char *r_str_append(char *ptr, const char *string) {
908890
}
909891

910892
R_API char *r_str_appendf(char *ptr, const char *fmt, ...) {
911-
int ret;
912-
char string[4096];
913-
va_list ap;
893+
va_list ap, ap2;
894+
914895
va_start (ap, fmt);
915-
ret = vsnprintf (string, sizeof (string), fmt, ap);
916-
if (ret >= sizeof (string)) {
917-
char *p = malloc (ret + 2);
918-
if (!p) {
919-
va_end (ap);
920-
return NULL;
921-
}
922-
vsnprintf (p, ret + 1, fmt, ap);
896+
if (!strchr (fmt, '%')) {
897+
va_end (ap);
898+
return r_str_append (ptr, fmt);
899+
}
900+
va_copy (ap2, ap);
901+
int ret = vsnprintf (NULL, 0, fmt, ap2);
902+
ret++;
903+
char *p = calloc (1, ret);
904+
if (p) {
905+
(void)vsnprintf (p, ret, fmt, ap);
923906
ptr = r_str_append (ptr, p);
924907
free (p);
925-
} else {
926-
ptr = r_str_append (ptr, string);
927908
}
909+
va_end (ap2);
928910
va_end (ap);
929911
return ptr;
930912
}
931913

932914
R_API char *r_str_appendch(char *x, char y) {
933915
char b[2] = { y, 0 };
934-
return r_str_append (x,b);
916+
return r_str_append (x, b);
935917
}
936918

937919
R_API char* r_str_replace(char *str, const char *key, const char *val, int g) {

0 commit comments

Comments
 (0)