summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@acm.org>2020-07-02 14:47:01 -0700
committerBart Van Assche <bvanassche@acm.org>2020-07-02 15:27:16 -0700
commit38b00241e3bd97b1aed6b669b357f2ec89743f0c (patch)
tree0966fdc1a632ec726050e2c18d8900df06efae27 /lib
parent797ef7b209d7423f34dfe513beeede21d2efddc4 (diff)
downloadfio-38b00241e3bd97b1aed6b669b357f2ec89743f0c.tar.gz
fio-38b00241e3bd97b1aed6b669b357f2ec89743f0c.tar.bz2
num2str(): Use asprintf() instead of malloc()
This patch reduces the size of the buffer allocated by num2str(). Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/num2str.c15
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/num2str.c b/lib/num2str.c
index 1abe22f3..ce177275 100644
--- a/lib/num2str.c
+++ b/lib/num2str.c
@@ -4,6 +4,7 @@
#include <string.h>
#include "../compiler/compiler.h"
+#include "../oslib/asprintf.h"
#include "num2str.h"
#define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
@@ -39,10 +40,6 @@ char *num2str(uint64_t num, int maxlen, int base, int pow2, enum n2s_unit units)
compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
assert(units < ARRAY_SIZE(unitstr));
- buf = malloc(128);
- if (!buf)
- return NULL;
-
if (pow2)
unitprefix = iecstr;
else
@@ -91,8 +88,9 @@ done:
if (post_index >= ARRAY_SIZE(sistr))
post_index = 0;
- sprintf(buf, "%llu%s%s", (unsigned long long) num,
- unitprefix[post_index], unitstr[units]);
+ if (asprintf(&buf, "%llu%s%s", (unsigned long long) num,
+ unitprefix[post_index], unitstr[units]) < 0)
+ buf = NULL;
return buf;
}
@@ -114,7 +112,8 @@ done:
sprintf(fmt, "%%.%df", (int)(maxlen - strlen(tmp) - 1));
sprintf(tmp, fmt, (double)modulo / (double)thousand);
- sprintf(buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2],
- unitprefix[post_index], unitstr[units]);
+ if (asprintf(&buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2],
+ unitprefix[post_index], unitstr[units]) < 0)
+ buf = NULL;
return buf;
}