From 2832b4a2ae75658101a4c2765f77b69a1b51a1d2 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Fri, 24 Mar 2023 17:04:57 -0300 Subject: [PATCH] flow/manager: fix coverity divide_by_zero warning Updated all cases where flow_config.prealloc was used in a division. *** CID 1524506: Integer handling issues (DIVIDE_BY_ZERO) /src/flow-manager.c: 858 in FlowManager() 852 "flow_spare_q status: %" PRIu32 "%% flows at the queue", 853 spare_pool_len, flow_config.prealloc, 854 spare_pool_len * 100 / flow_config.prealloc); 855 856 /* only if we have pruned this "emergency_recovery" percentage 857 * of flows, we will unset the emergency bit */ >>> CID 1524506: Integer handling issues (DIVIDE_BY_ZERO) >>> In expression "spare_pool_len * 100U / flow_config.prealloc", division by expression "flow_config.prealloc" which may be zero has undefined behavior. 858 if (spare_pool_len * 100 / flow_config.prealloc > flow_config.emergency_recovery) { 859 emerg_over_cnt++; 860 } else { 861 emerg_over_cnt = 0; 862 } Related to Bug #5919 (cherry picked from commit 754d2803dd5f5956b7f2ae947f933ef03cf1d15b) --- src/flow-manager.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/flow-manager.c b/src/flow-manager.c index e9b37c2948..2b2b2b4a7a 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2020 Open Information Security Foundation +/* Copyright (C) 2007-2023 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -901,11 +901,11 @@ static TmEcode FlowManager(ThreadVars *th_v, void *thread_data) if (emerg == true) { SCLogDebug("flow_sparse_q.len = %"PRIu32" prealloc: %"PRIu32 "flow_spare_q status: %"PRIu32"%% flows at the queue", - len, flow_config.prealloc, len * 100 / flow_config.prealloc); + len, flow_config.prealloc, len * 100 / MAX(flow_config.prealloc, 1)); /* only if we have pruned this "emergency_recovery" percentage * of flows, we will unset the emergency bit */ - if (len * 100 / flow_config.prealloc > flow_config.emergency_recovery) { + if (len * 100 / MAX(flow_config.prealloc, 1) > flow_config.emergency_recovery) { emerg_over_cnt++; } else { emerg_over_cnt = 0; @@ -923,7 +923,7 @@ static TmEcode FlowManager(ThreadVars *th_v, void *thread_data) " FLOW_EMERGENCY bit (ts.tv_sec: %"PRIuMAX", " "ts.tv_usec:%"PRIuMAX") flow_spare_q status(): %"PRIu32 "%% flows at the queue", (uintmax_t)ts.tv_sec, - (uintmax_t)ts.tv_usec, len * 100 / flow_config.prealloc); + (uintmax_t)ts.tv_usec, len * 100 / MAX(flow_config.prealloc, 1)); StatsIncr(th_v, ftd->cnt.flow_emerg_mode_over); } -- 2.47.2