[ovs-dev] [PATCH] ovs-thread: Fix crash by making count_cpu_count() return type a signed int.

Ben Pfaff blp at nicira.com
Fri Dec 13 22:31:43 UTC 2013


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 at nicira.com>
---
 lib/ovs-thread.c |    2 +-
 lib/ovs-thread.h |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c
index dcaf7ff..aa3ad15 100644
--- a/lib/ovs-thread.c
+++ b/lib/ovs-thread.c
@@ -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;
diff --git a/lib/ovs-thread.h b/lib/ovs-thread.h
index 1f2de72..b6d973f 100644
--- a/lib/ovs-thread.h
+++ b/lib/ovs-thread.h
@@ -505,6 +505,6 @@ bool may_fork(void);
 
 /* Useful functions related to threading. */
 
-unsigned int count_cpu_cores(void);
+int count_cpu_cores(void);
 
 #endif /* ovs-thread.h */
-- 
1.7.10.4




More information about the dev mailing list