From c6bc60668f932154cdedfb5b40d243edd2bfcad1 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Wed, 5 Jul 2023 15:32:57 +0200 Subject: [PATCH 1/2] profiling: fix check to compute average bytes --- src/util-profiling-prefilter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util-profiling-prefilter.c b/src/util-profiling-prefilter.c index 861079a101b9..280d5144e220 100644 --- a/src/util-profiling-prefilter.c +++ b/src/util-profiling-prefilter.c @@ -121,7 +121,7 @@ static void DoDump(SCProfilePrefilterDetectCtx *rules_ctx, FILE *fp, const char avgticks = (double)(ticks / d->called); } double avgbytes = 0; - if (d->total_bytes && d->called) { + if (d->total_bytes && d->bytes_called) { avgbytes = (double)(d->total_bytes / d->bytes_called); } double ticks_per_byte = 0; From 0a91705bb348721a5b6b12a8550df6ac3b50bb4b Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Fri, 30 Jun 2023 10:21:57 +0200 Subject: [PATCH 2/2] src: checks to avoid divisions by zero Ticket: #5920 --- src/flow.c | 5 +++-- src/source-dpdk.c | 6 ++++++ src/source-windivert.c | 6 ++++-- src/util-device.c | 5 +++-- src/util-pool.c | 14 ++++++++------ src/util-profiling-rules.c | 2 +- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/flow.c b/src/flow.c index 75e77d8ed93b..891a5fb9b94d 100644 --- a/src/flow.c +++ b/src/flow.c @@ -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) diff --git a/src/source-dpdk.c b/src/source-dpdk.c index cc9dcd6f6e62..0af4fbb5500b 100644 --- a/src/source-dpdk.c +++ b/src/source-dpdk.c @@ -151,12 +151,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; } diff --git a/src/source-windivert.c b/src/source-windivert.c index f0d956973de5..97d879c8d762 100644 --- a/src/source-windivert.c +++ b/src/source-windivert.c @@ -233,8 +233,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)); diff --git a/src/util-device.c b/src/util-device.c index 74a51c9f1069..6952ae812cf9 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -318,10 +318,11 @@ int LiveDeviceListClean(void) TAILQ_FOREACH_SAFE(pd, &live_devices, next, tpd) { if (live_devices_stats) { + uint64_t pkts = SC_ATOMIC_GET(pd->pkts); SCLogNotice("%s: packets: %" PRIu64 ", drops: %" PRIu64 " (%.2f%%), invalid chksum: %" PRIu64, - pd->dev, SC_ATOMIC_GET(pd->pkts), SC_ATOMIC_GET(pd->drop), - 100 * ((double)SC_ATOMIC_GET(pd->drop)) / (double)SC_ATOMIC_GET(pd->pkts), + pd->dev, pkts, SC_ATOMIC_GET(pd->drop), + pkts ? 100 * ((double)SC_ATOMIC_GET(pd->drop)) / (double)pkts : 0, SC_ATOMIC_GET(pd->invalid_checksums)); } diff --git a/src/util-pool.c b/src/util-pool.c index 26d6480099e9..348c679741ce 100644 --- a/src/util-pool.c +++ b/src/util-pool.c @@ -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))); + } } /* diff --git a/src/util-profiling-rules.c b/src/util-profiling-rules.c index 90c1b8e57f48..7a14c93b7731 100644 --- a/src/util-profiling-rules.c +++ b/src/util-profiling-rules.c @@ -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; }