]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
src: checks to avoid divisions by zero
authorPhilippe Antoine <pantoine@oisf.net>
Fri, 30 Jun 2023 08:21:57 +0000 (10:21 +0200)
committerVictor Julien <vjulien@oisf.net>
Tue, 25 Jul 2023 13:10:07 +0000 (15:10 +0200)
Ticket: #5920

src/flow.c
src/source-dpdk.c
src/source-windivert.c
src/util-pool.c
src/util-profiling-rules.c

index 75e77d8ed93b6acdb200e8b3a20db9f97c801ebb..891a5fb9b94d1f53face2f2e5b8a2f3a2207dfd9 100644 (file)
@@ -606,9 +606,10 @@ void FlowInitConfig(bool quiet)
             FatalError("Invalid value for flow.hash-size: NULL");
         }
 
-        if (StringParseUint32(&configval, 10, strlen(conf_val),
-                                    conf_val) > 0) {
+        if (StringParseUint32(&configval, 10, strlen(conf_val), conf_val) > 0 || configval == 0) {
             flow_config.hash_size = configval;
+        } else {
+            FatalError("Invalid value for flow.hash-size");
         }
     }
     if ((ConfGet("flow.prealloc", &conf_val)) == 1)
index 363b718897c326171992fd58c47b0dd53d49240e..1de89b1c4b49f36bb1b1485463cf3b5b5a1494fd 100644 (file)
@@ -152,12 +152,18 @@ static void DPDKFreeMbufArray(struct rte_mbuf **mbuf_array, uint16_t mbuf_cnt, u
 static uint64_t CyclesToMicroseconds(const uint64_t cycles)
 {
     const uint64_t ticks_per_us = rte_get_tsc_hz() / 1000000;
+    if (ticks_per_us == 0) {
+        return 0;
+    }
     return cycles / ticks_per_us;
 }
 
 static uint64_t CyclesToSeconds(const uint64_t cycles)
 {
     const uint64_t ticks_per_s = rte_get_tsc_hz();
+    if (ticks_per_s == 0) {
+        return 0;
+    }
     return cycles / ticks_per_s;
 }
 
index 5625b7f0f56fbd084ef39546492debe4b7126d55..347d2e7a0f2dc623c21dddb6b134e85e01ed4771 100644 (file)
@@ -232,8 +232,10 @@ static SCTime_t WinDivertTimestampToTimeStamp(WinDivertThreadVars *wd_tv, INT64
     struct timeval tv;
 
     int64_t qpc_delta = (int64_t)timestamp_count - wd_tv->qpc_start_count;
-    int64_t unix_usec =
-            wd_tv->qpc_start_time + (qpc_delta / wd_tv->qpc_freq_usec);
+    int64_t unix_usec = wd_tv->qpc_start_time;
+    if (wd_tv->qpc_freq_usec) {
+        unix_usec += qpc_delta / wd_tv->qpc_freq_usec;
+    }
 
     tv.tv_sec = (long)(unix_usec / (1000 * 1000));
     tv.tv_usec = (long)(unix_usec - (int64_t)tv.tv_sec * (1000 * 1000));
index 26d6480099e92eb4090cfdc105b5784a617d1270..348c679741ceed1790b21a985093c53fbfb95e5f 100644 (file)
@@ -378,12 +378,14 @@ void PoolReturn(Pool *p, void *data)
 
 void PoolPrintSaturation(Pool *p)
 {
-    SCLogDebug("pool %p is using %" PRIu32 " out of %" PRIu32 " items (%02.1f%%), max %" PRIu32
-               " (%02.1f%%): pool struct memory %" PRIu64 ".",
-            p, p->outstanding, p->max_buckets,
-            (float)(p->outstanding) / (float)(p->max_buckets) * 100, p->max_outstanding,
-            (float)(p->max_outstanding) / (float)(p->max_buckets) * 100,
-            (uint64_t)(p->max_buckets * sizeof(PoolBucket)));
+    if (p->max_buckets > 0) {
+        SCLogDebug("pool %p is using %" PRIu32 " out of %" PRIu32 " items (%02.1f%%), max %" PRIu32
+                   " (%02.1f%%): pool struct memory %" PRIu64 ".",
+                p, p->outstanding, p->max_buckets,
+                (float)(p->outstanding) / (float)(p->max_buckets) * 100, p->max_outstanding,
+                (float)(p->max_outstanding) / (float)(p->max_buckets) * 100,
+                (uint64_t)(p->max_buckets * sizeof(PoolBucket)));
+    }
 }
 
 /*
index 90c1b8e57f48fcfc0ebadc24e34832d0406fcc58..7a14c93b7731e9c1354edf0a0fe2547128913f96 100644 (file)
@@ -441,7 +441,7 @@ static void *SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx, int file_output)
         summary[i].ticks = rules_ctx->data[i].ticks_match + rules_ctx->data[i].ticks_no_match;
         summary[i].checks = rules_ctx->data[i].checks;
 
-        if (summary[i].ticks > 0) {
+        if (summary[i].checks > 0) {
             summary[i].avgticks = (long double)summary[i].ticks / (long double)summary[i].checks;
         }