btreplay: fix memory corruption caused by CPU_ZERO_S
[blktrace.git] / btreplay / btreplay.c
index 54440103e25fdb6efa430df2c2f46efd26e47a8e..2a1525e046620ecae71d2b8eea88b8da921c7e93 100644 (file)
@@ -502,8 +502,8 @@ static inline void start_iter(void)
  */
 static void get_ncpus(void)
 {
-#ifdef _SC_NPROCESSORS_CONF
-       ncpus = sysconf(_SC_NPROCESSORS_CONF);
+#ifdef _SC_NPROCESSORS_ONLN
+       ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 #else
        int nrcpus = 4096;
        cpu_set_t * cpus;
@@ -516,7 +516,7 @@ realloc:
        if (sched_getaffinity(getpid(), size, cpus)) {
                if( errno == EINVAL && nrcpus < (4096<<4) ) {
                        CPU_FREE(cpus);
-                       nrcpus <= 1;
+                       nrcpus <<= 1;
                        goto realloc;
                }
                fatal("sched_getaffinity", ERR_SYSCALL, "Can't get CPU info\n");
@@ -550,7 +550,7 @@ static void pin_to_cpu(struct thr_info *tip)
 
        assert(0 <= tip->cpu && tip->cpu < ncpus);
 
-       CPU_ZERO_S(ncpus, cpus);
+       CPU_ZERO_S(size, cpus);
        CPU_SET_S(tip->cpu, size, cpus);
        if (sched_setaffinity(getpid(), size, cpus)) {
                fatal("sched_setaffinity", ERR_SYSCALL, "Failed to pin CPU\n");
@@ -646,7 +646,7 @@ static void find_input_devs(char *idir)
 static void read_map_devs(char *file_name)
 {
        FILE *fp;
-       char *from_dev, *to_dev;
+       char from_dev[256], to_dev[256];
 
        fp = fopen(file_name, "r");
        if (!fp) {
@@ -654,7 +654,7 @@ static void read_map_devs(char *file_name)
                /*NOTREACHED*/
        }
 
-       while (fscanf(fp, "%as %as", &from_dev, &to_dev) == 2) {
+       while (fscanf(fp, "%s %s", from_dev, to_dev) == 2) {
                struct map_dev *mdp = malloc(sizeof(*mdp));
 
                mdp->from_dev = from_dev;