backend: Use asprintf() instead of strlen() + sprintf()
authorBart Van Assche <bvanassche@acm.org>
Fri, 4 Sep 2020 23:05:37 +0000 (16:05 -0700)
committerBart Van Assche <bvanassche@acm.org>
Sat, 12 Sep 2020 01:20:32 +0000 (18:20 -0700)
This patch does not change any functionality but makes exec_string() easier
to read.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
backend.c

index c7a027e75f7f60234645cc058732faab1b32758d..f91f3caf9b7d82fe31b0385d5942a7b4f4414318 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -1458,16 +1458,17 @@ static bool keep_running(struct thread_data *td)
        return false;
 }
 
-static int exec_string(struct thread_options *o, const char *string, const char *mode)
+static int exec_string(struct thread_options *o, const char *string,
+                      const char *mode)
 {
-       size_t newlen = strlen(string) + strlen(o->name) + strlen(mode) + 13 + 1;
        int ret;
        char *str;
 
-       str = malloc(newlen);
-       sprintf(str, "%s > %s.%s.txt 2>&1", string, o->name, mode);
+       if (asprintf(&str, "%s > %s.%s.txt 2>&1", string, o->name, mode) < 0)
+               return -1;
 
-       log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
+       log_info("%s : Saving output of %s in %s.%s.txt\n", o->name, mode,
+                o->name, mode);
        ret = system(str);
        if (ret == -1)
                log_err("fio: exec of cmd <%s> failed\n", str);