diff mbox

[ovs-dev] test-cmap: Avoid shift by full width of type in find_batch().

Message ID 20170527045219.29105-1-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff May 27, 2017, 4:52 a.m. UTC
In C, it is undefined to shift an N-bit value by N.  This fixes the
problem in find_batch() for the case where i == 0.

Found by Coverity.

Reported-at: https://scan3.coverity.com/reports.htm#v16889/p10449/fileInstanceId=14763098&defectInstanceId=4304031&mergedDefectId=68209
Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 tests/test-cmap.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Justin Pettit June 2, 2017, 12:05 a.m. UTC | #1
> On May 26, 2017, at 9:52 PM, Ben Pfaff <blp@ovn.org> wrote:
> 
> In C, it is undefined to shift an N-bit value by N.  This fixes the
> problem in find_batch() for the case where i == 0.
> 
> Found by Coverity.
> 
> Reported-at: https://scan3.coverity.com/reports.htm#v16889/p10449/fileInstanceId=14763098&defectInstanceId=4304031&mergedDefectId=68209
> Signed-off-by: Ben Pfaff <blp@ovn.org>

Acked-by: Justin Pettit <jpettit@ovn.org>

--Justin
Ben Pfaff June 2, 2017, 3:59 a.m. UTC | #2
On Thu, Jun 01, 2017 at 05:05:14PM -0700, Justin Pettit wrote:
> 
> > On May 26, 2017, at 9:52 PM, Ben Pfaff <blp@ovn.org> wrote:
> > 
> > In C, it is undefined to shift an N-bit value by N.  This fixes the
> > problem in find_batch() for the case where i == 0.
> > 
> > Found by Coverity.
> > 
> > Reported-at: https://scan3.coverity.com/reports.htm#v16889/p10449/fileInstanceId=14763098&defectInstanceId=4304031&mergedDefectId=68209
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> 
> Acked-by: Justin Pettit <jpettit@ovn.org>

Thanks, I applied this to master and backported as far as branch-2.6.
diff mbox

Patch

diff --git a/tests/test-cmap.c b/tests/test-cmap.c
index e159a164f315..2c6fa89eb775 100644
--- a/tests/test-cmap.c
+++ b/tests/test-cmap.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2008, 2009, 2010, 2013, 2014, 2016 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2013, 2014, 2016, 2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -412,7 +412,6 @@  find_batch(const struct cmap *cmap, const int value)
 {
     size_t i, ret;
     const size_t end = MIN(n_batch, n_elems - value);
-    unsigned long map = ~0;
     uint32_t hashes[N_BATCH_MAX];
     const struct cmap_node *nodes[N_BATCH_MAX];
 
@@ -431,7 +430,7 @@  find_batch(const struct cmap *cmap, const int value)
 
     ret = i;
 
-    map >>= BITMAP_ULONG_BITS - i; /* Clear excess bits. */
+    unsigned long map = i ? ~0UL >> (BITMAP_ULONG_BITS - i) : 0;
     map = cmap_find_batch(cmap, map, hashes, nodes);
 
     ULLONG_FOR_EACH_1(i, map) {