rib: fix undefined behavior

Message ID 1607446804-160076-1-git-send-email-vladimir.medvedkin@intel.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series rib: fix undefined behavior |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/travis-robot warning Travis build: failed

Commit Message

Vladimir Medvedkin Dec. 8, 2020, 5 p.m. UTC
  According to GCC documentation for __builtin_clz:
Returns the number of leading 0-bits in x,
starting at the most significant bit position.
If x is 0, the result is undefined.
__builtin_clz will be called with 0 if the existing
prefix address matches the one we want to insert.

Fixes: 5a5793a5ffa2 ("rib: add RIB library")
Cc: vladimir.medvedkin@intel.com
Cc: stable@dpdk.org

Reported-by: David Marchand <david.marchand@redhat.com>
Signed-off-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>
---
 lib/librte_rib/rte_rib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

David Marchand Dec. 15, 2020, 9:08 a.m. UTC | #1
On Tue, Dec 8, 2020 at 6:00 PM Vladimir Medvedkin
<vladimir.medvedkin@intel.com> wrote:
>
> According to GCC documentation for __builtin_clz:
> Returns the number of leading 0-bits in x,
> starting at the most significant bit position.
> If x is 0, the result is undefined.
> __builtin_clz will be called with 0 if the existing
> prefix address matches the one we want to insert.
>
> Fixes: 5a5793a5ffa2 ("rib: add RIB library")
> Cc: stable@dpdk.org
>
> Reported-by: David Marchand <david.marchand@redhat.com>
> Signed-off-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>

Applied, thanks.
  

Patch

diff --git a/lib/librte_rib/rte_rib.c b/lib/librte_rib/rte_rib.c
index 2a370d7..6c29e1c 100644
--- a/lib/librte_rib/rte_rib.c
+++ b/lib/librte_rib/rte_rib.c
@@ -301,7 +301,7 @@  rte_rib_insert(struct rte_rib *rib, uint32_t ip, uint8_t depth)
 	/* closest node found, new_node should be inserted in the middle */
 	common_depth = RTE_MIN(depth, (*tmp)->depth);
 	common_prefix = ip ^ (*tmp)->ip;
-	d = __builtin_clz(common_prefix);
+	d = (common_prefix == 0) ? 32 : __builtin_clz(common_prefix);
 
 	common_depth = RTE_MIN(d, common_depth);
 	common_prefix = ip & rte_rib_depth_to_mask(common_depth);