[PATCH] Potential overflow and unused variable
[fio.git] / init.c
diff --git a/init.c b/init.c
index c799bf5f4b917607783cffae07b3998719aa21b4..732cceb993755a7ac2c5b6f3672c13c5950664b8 100644 (file)
--- a/init.c
+++ b/init.c
@@ -432,6 +432,9 @@ static struct thread_data *get_new_job(int global, struct thread_data *parent)
 
 static void put_job(struct thread_data *td)
 {
+       if (td == &def_thread)
+               return;
+
        memset(&threads[td->thread_number - 1], 0, sizeof(*td));
        thread_number--;
 }
@@ -442,11 +445,6 @@ static void put_job(struct thread_data *td)
  */
 static void fixup_options(struct thread_data *td)
 {
-       if (!td->min_bs)
-               td->min_bs = td->bs;
-       if (!td->max_bs)
-               td->max_bs = td->bs;
-
        if (!td->rwmixread && td->rwmixwrite)
                td->rwmixread = 100 - td->rwmixwrite;
 
@@ -455,6 +453,32 @@ static void fixup_options(struct thread_data *td)
                free(td->write_iolog_file);
                td->write_iolog_file = NULL;
        }
+
+       if (td->io_ops->flags & FIO_SYNCIO)
+               td->iodepth = 1;
+       else {
+               if (!td->iodepth)
+                       td->iodepth = td->nr_files;
+       }
+
+       /*
+        * only really works for sequential io for now, and with 1 file
+        */
+       if (td->zone_size && !td->sequential && td->nr_files == 1)
+               td->zone_size = 0;
+
+       /*
+        * Reads can do overwrites, we always need to pre-create the file
+        */
+       if (td_read(td) || td_rw(td))
+               td->overwrite = 1;
+
+       if (!td->min_bs)
+               td->min_bs = td->bs;
+       if (!td->max_bs)
+               td->max_bs = td->bs;
+       if (td_read(td) && !td_rw(td))
+               td->verify = 0;
 }
 
 /*
@@ -483,8 +507,6 @@ static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
        }
 #endif
 
-       fixup_options(td);
-
        /*
         * the def_thread is just for options, it's not a real job
         */
@@ -502,24 +524,7 @@ static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
                }
        }
 
-       if (td->io_ops->flags & FIO_SYNCIO)
-               td->iodepth = 1;
-       else {
-               if (!td->iodepth)
-                       td->iodepth = td->nr_files;
-       }
-
-       /*
-        * only really works for sequential io for now, and with 1 file
-        */
-       if (td->zone_size && !td->sequential && td->nr_files == 1)
-               td->zone_size = 0;
-
-       /*
-        * Reads can do overwrites, we always need to pre-create the file
-        */
-       if (td_read(td) || td_rw(td))
-               td->overwrite = 1;
+       fixup_options(td);
 
        td->filetype = FIO_TYPE_FILE;
        if (!stat(jobname, &sb)) {
@@ -578,13 +583,6 @@ static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
        td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
        td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
 
-       if (td->min_bs == -1U)
-               td->min_bs = td->bs;
-       if (td->max_bs == -1U)
-               td->max_bs = td->bs;
-       if (td_read(td) && !td_rw(td))
-               td->verify = 0;
-
        if (td->stonewall && td->thread_number > 1)
                groupid++;
 
@@ -845,7 +843,7 @@ int parse_jobs_ini(char *file, int stonewall_flag)
 {
        unsigned int global;
        struct thread_data *td;
-       char *string, *name, *tmpbuf;
+       char *string, *name;
        fpos_t off;
        FILE *f;
        char *p;
@@ -859,7 +857,7 @@ int parse_jobs_ini(char *file, int stonewall_flag)
 
        string = malloc(4096);
        name = malloc(256);
-       tmpbuf = malloc(4096);
+       memset(name, 0, 256);
 
        stonewall = stonewall_flag;
        while ((p = fgets(string, 4096, f)) != NULL) {
@@ -867,7 +865,7 @@ int parse_jobs_ini(char *file, int stonewall_flag)
                        break;
                if (is_empty_or_comment(p))
                        continue;
-               if (sscanf(p, "[%s]", name) != 1)
+               if (sscanf(p, "[%255s]", name) != 1)
                        continue;
 
                global = !strncmp(name, "global", 6);
@@ -918,7 +916,6 @@ int parse_jobs_ini(char *file, int stonewall_flag)
 
        free(string);
        free(name);
-       free(tmpbuf);
        fclose(f);
        return ret;
 }
@@ -938,8 +935,8 @@ static int fill_def_thread(void)
        def_thread.ddir = DDIR_READ;
        def_thread.iomix = 0;
        def_thread.bs = DEF_BS;
-       def_thread.min_bs = -1;
-       def_thread.max_bs = -1;
+       def_thread.min_bs = 0;
+       def_thread.max_bs = 0;
        def_thread.odirect = DEF_ODIRECT;
        def_thread.ratecycle = DEF_RATE_CYCLE;
        def_thread.sequential = DEF_SEQUENTIAL;
@@ -985,7 +982,7 @@ static void usage(void)
 static int parse_cmd_line(int argc, char *argv[])
 {
        struct thread_data *td = NULL;
-       int c, ini_idx = 0, lidx;
+       int c, ini_idx = 0, lidx, ret;
 
        while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
                switch (c) {
@@ -1019,13 +1016,23 @@ static int parse_cmd_line(int argc, char *argv[])
                        const char *opt = long_options[lidx].name;
                        char *val = optarg;
 
+                       if (!strncmp(opt, "name", 4) && td) {
+                               ret = add_job(td, td->name ?: "fio", 0);
+                               if (ret) {
+                                       put_job(td);
+                                       return 0;
+                               }
+                               td = NULL;
+                       }
                        if (!td) {
-                               td = get_new_job(0, &def_thread);
+                               int global = !strncmp(val, "global", 6);
+
+                               td = get_new_job(global, &def_thread);
                                if (!td)
                                        return 0;
                        }
-                       if (parse_cmd_option(opt, val, options, td))
-                               printf("foo\n");
+
+                       parse_cmd_option(opt, val, options, td);
                        break;
                }
                default:
@@ -1035,13 +1042,7 @@ static int parse_cmd_line(int argc, char *argv[])
        }
 
        if (td) {
-               const char *name = td->name;
-               int ret;
-
-               if (!name)
-                       name = "fio";
-
-               ret = add_job(td, name, 0);
+               ret = add_job(td, td->name ?: "fio", 0);
                if (ret)
                        put_job(td);
        }