examples/performance-thread: fix out-of-bounds sched array
authorSlawomir Mrozowicz <slawomirx.mrozowicz@intel.com>
Wed, 20 Sep 2017 07:47:34 +0000 (09:47 +0200)
committerThomas Monjalon <thomas@monjalon.net>
Fri, 13 Oct 2017 23:22:24 +0000 (01:22 +0200)
Overrunning array schedcore of 128 8-byte elements at element index 128
using index core id.
Fixed by correct check index lcoreid condition and
change type of lcoreid to unsigned.

Coverity issue: 143459, 143461
Fixes: 116819b9ed0d ("examples/performance-thread: add lthread subsystem")

Signed-off-by: Slawomir Mrozowicz <slawomirx.mrozowicz@intel.com>
Acked-by: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
examples/performance-thread/common/lthread.h
examples/performance-thread/common/lthread_sched.c

index 5c2c1a5..0cde591 100644 (file)
@@ -87,7 +87,7 @@ int _lthread_desched_sleep(struct lthread *lt);
 
 void _lthread_free(struct lthread *lt);
 
-struct lthread_sched *_lthread_sched_get(int lcore_id);
+struct lthread_sched *_lthread_sched_get(unsigned int lcore_id);
 
 struct lthread_stack *_stack_alloc(void);
 
index 9829147..e100c41 100644 (file)
@@ -562,11 +562,14 @@ void lthread_run(void)
  * Return the scheduler for this lcore
  *
  */
-struct lthread_sched *_lthread_sched_get(int lcore_id)
+struct lthread_sched *_lthread_sched_get(unsigned int lcore_id)
 {
-       if (lcore_id > LTHREAD_MAX_LCORES)
-               return NULL;
-       return schedcore[lcore_id];
+       struct lthread_sched *res = NULL;
+
+       if (lcore_id < LTHREAD_MAX_LCORES)
+               res = schedcore[lcore_id];
+
+       return res;
 }
 
 /*
@@ -578,10 +581,9 @@ int lthread_set_affinity(unsigned lcoreid)
        struct lthread *lt = THIS_LTHREAD;
        struct lthread_sched *dest_sched;
 
-       if (unlikely(lcoreid > LTHREAD_MAX_LCORES))
+       if (unlikely(lcoreid >= LTHREAD_MAX_LCORES))
                return POSIX_ERRNO(EINVAL);
 
-
        DIAG_EVENT(lt, LT_DIAG_LTHREAD_AFFINITY, lcoreid, 0);
 
        dest_sched = schedcore[lcoreid];