summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-08-24 12:42:18 +0200
committerJo-Philipp Wich <jo@mein.io>2020-08-25 20:38:15 +0200
commit4861601872a5c7f0d10c94251f7a2c3a08da05d4 (patch)
tree1bee86a20c22d1151c89f1e180610057b2de9dc7 /lib.c
parentd86c9ab4c1c7b25cd942c4656626e2feca318857 (diff)
lib.c: use snprint_append() helper for sprintf and printf functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c51
1 files changed, 10 insertions, 41 deletions
diff --git a/lib.c b/lib.c
index 39238ed..9bfa842 100644
--- a/lib.c
+++ b/lib.c
@@ -1338,12 +1338,12 @@ static size_t
ut_printf_common(struct ut_state *s, struct ut_opcode *op, struct json_object *args, char **res)
{
struct json_object *fmt = json_object_array_get_idx(args, 0);
- char *tmp, *fp, sfmt[sizeof("%0- 123456789.123456789%")];
+ char *fp, sfmt[sizeof("%0- 123456789.123456789%")];
union { const char *s; int64_t n; double d; } arg;
size_t len = 0, arglen, argidx;
const char *fstr, *last, *p;
enum json_type t;
- int slen = 0;
+ bool ok;
*res = NULL;
@@ -1357,16 +1357,8 @@ ut_printf_common(struct ut_state *s, struct ut_opcode *op, struct json_object *a
for (last = p = fstr; *p; p++) {
if (*p == '%') {
- if (p > last) {
- tmp = realloc(*res, len + (p - last) + 1);
-
- if (!tmp)
- goto err;
-
- *res = tmp;
- snprintf(*res + len, p - last + 1, "%s", last);
- len += (p - last);
- }
+ if (!snprintf_append(res, &len, "%s", p - last, last))
+ goto err;
last = p++;
@@ -1500,32 +1492,15 @@ ut_printf_common(struct ut_state *s, struct ut_opcode *op, struct json_object *a
*fp = 0;
switch (t) {
- case json_type_int: slen = snprintf(NULL, 0, sfmt, arg.n); break;
- case json_type_double: slen = snprintf(NULL, 0, sfmt, arg.d); break;
- case json_type_string: slen = snprintf(NULL, 0, sfmt, arg.s); break;
- default: slen = snprintf(NULL, 0, sfmt);
+ case json_type_int: ok = sprintf_append(res, &len, sfmt, arg.n); break;
+ case json_type_double: ok = sprintf_append(res, &len, sfmt, arg.d); break;
+ case json_type_string: ok = sprintf_append(res, &len, sfmt, arg.s); break;
+ default: ok = sprintf_append(res, &len, sfmt); break;
}
- if (slen < 0)
- continue;
-
- tmp = realloc(*res, len + slen + 1);
-
- if (!tmp)
+ if (!ok)
goto err;
- *res = tmp;
-
- switch (t) {
- case json_type_int: slen = snprintf(*res + len, slen + 1, sfmt, arg.n); break;
- case json_type_double: slen = snprintf(*res + len, slen + 1, sfmt, arg.d); break;
- case json_type_string: slen = snprintf(*res + len, slen + 1, sfmt, arg.s); break;
- default: slen = snprintf(*res + len, slen + 1, sfmt);
- }
-
- if (slen > 0)
- len += slen;
-
last = p + 1;
next:
@@ -1533,15 +1508,9 @@ next:
}
}
- tmp = realloc(*res, len + (p - last) + 1);
-
- if (!tmp)
+ if (!snprintf_append(res, &len, "%s", p - last, last))
goto err;
- *res = tmp;
- snprintf(*res + len, p - last + 1, "%s", last);
- len += (p - last);
-
return len;
err: