From 662c59a80a886f9a6741eb95229ee2aca2e06fad Mon Sep 17 00:00:00 2001 From: Steven Luong Date: Wed, 12 Oct 2022 17:08:12 -0700 Subject: l2: coverity complains dead codes Coverity complains dead codes in 2 places due to a recent commit as pointed out in Fixes. The dead codes are if (seed < L2_BD_ID_MAX % 2) is_seed_low = 1; and if (is_seed_low) seed += (2 * (i % 2) - 1) * i; seed can never be less than (L2_BD_ID_MAX % 2). Consequently, is_seed_low is always 0. There is also other problem. The inner loop is iterating only once. The fix is to greatly simplify the code to generate a random bd_id. Type: fix Fixes: Ieb6919f958f437fc603d5e1f48cab01de780951d Signed-off-by: Steven Luong Change-Id: I318773b9a59950920e051548ef14e36054ebd5e6 --- src/vnet/l2/l2_bd.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/vnet/l2/l2_bd.c') diff --git a/src/vnet/l2/l2_bd.c b/src/vnet/l2/l2_bd.c index a8429170788..9c81b0f81a0 100644 --- a/src/vnet/l2/l2_bd.c +++ b/src/vnet/l2/l2_bd.c @@ -1608,28 +1608,25 @@ VLIB_CLI_COMMAND (bd_create_cli, static) = { * Returns an unused bridge domain id, and ~0 if it can't find one. */ u32 -bd_get_unused_id () +bd_get_unused_id (void) { bd_main_t *bdm = &bd_main; int i, j; - int is_seed_low = 0; static u32 seed = 0; + /* limit to 1M tries */ for (j = 0; j < 1 << 10; j++) { - seed = random_u32 (&seed) & L2_BD_ID_MAX; - if (seed == 0) - continue; - if (seed < L2_BD_ID_MAX % 2) - is_seed_low = 1; - for (i = 0; i < L2_BD_ID_MAX % 2; i++) + seed = random_u32 (&seed); + for (i = 0; i < 1 << 10; i++) { - /* look around randomly generated id */ - if (is_seed_low) - seed += (2 * (i % 2) - 1) * i; - else - seed -= (2 * (i % 2) - 1) * i; - if (seed == ~0 || seed == 0) + /* + * iterate seed+0, seed+1, seed-1, seed+2, seed-2, ... to generate id + */ + seed += (2 * (i % 2) - 1) * i; + /* bd_id must be (1 <= bd_id <= L2_BD_ID_MAX) */ + seed &= L2_BD_ID_MAX; + if (seed == 0) continue; if (bd_find_index (bdm, seed) == ~0) return seed; -- cgit 1.2.3-korg