ovs-thread: Fix crash by making count_cpu_count() return type a signed int.
authorBen Pfaff <blp@nicira.com>
Fri, 13 Dec 2013 22:31:27 +0000 (14:31 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 13 Dec 2013 23:06:37 +0000 (15:06 -0800)
ofproto_set_threads() uses the calculation MAX(count_cpu_cores() - 2, 1)
to decide on the default thread count.  However, count_cpu_cores() returns
0 if it can't count the number of cores, or 1 if there's only one core,
and that causes the calculation to come out as UINT_MAX-2 or UINT_MAX-1,
respectively, which causes a memory allocation failure later.

There are other ways to fix this problem, too, of course.

Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/ovs-thread.c
lib/ovs-thread.h

index dcaf7ff..aa3ad15 100644 (file)
@@ -371,7 +371,7 @@ parse_cpuinfo(long int *n_cores)
  * Tries not to count hyper-threads, but may be inaccurate - particularly on
  * platforms that do not provide /proc/cpuinfo, but also if /proc/cpuinfo is
  * formatted different to the layout that parse_cpuinfo() expects. */
-unsigned int
+int
 count_cpu_cores(void)
 {
     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
index 1f2de72..b6d973f 100644 (file)
@@ -505,6 +505,6 @@ bool may_fork(void);
 \f
 /* Useful functions related to threading. */
 
-unsigned int count_cpu_cores(void);
+int count_cpu_cores(void);
 
 #endif /* ovs-thread.h */