From 41fd2c4bbd0af0854d98a4544b35d705a2fb8103 Mon Sep 17 00:00:00 2001 From: Prince Sunny Date: Sat, 26 Dec 2020 11:13:57 -0800 Subject: [PATCH] [CRM] Safety check for division by 0 (#1569) Check if divisor is 0. If yes, log warning and skip division --- orchagent/crmorch.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/orchagent/crmorch.cpp b/orchagent/crmorch.cpp index 0c02155d4738..14ded73d3946 100644 --- a/orchagent/crmorch.cpp +++ b/orchagent/crmorch.cpp @@ -541,7 +541,16 @@ void CrmOrch::checkCrmThresholds() if (cnt.usedCounter != 0) { - percentageUtil = (uint32_t)((cnt.usedCounter * 100) / (uint64_t)(cnt.usedCounter + cnt.availableCounter)); + uint32_t dvsr = cnt.usedCounter + cnt.availableCounter; + if (dvsr != 0) + { + percentageUtil = (cnt.usedCounter * 100) / dvsr; + } + else + { + SWSS_LOG_WARN("%s Exception occured (div by Zero): Used count %u free count %u", + res.name.c_str(), cnt.usedCounter, cnt.availableCounter); + } } switch (res.thresholdType)