From 1133cb619b81f99121b251d49150af7e9bba870d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 31 Mar 2025 18:45:51 +0800 Subject: [PATCH] Swap (OpenBSD): fix mem leaks --- src/detection/swap/swap_obsd.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/detection/swap/swap_obsd.c b/src/detection/swap/swap_obsd.c index b64900c9f..414971d39 100644 --- a/src/detection/swap/swap_obsd.c +++ b/src/detection/swap/swap_obsd.c @@ -10,21 +10,24 @@ const char* ffDetectSwap(FFSwapResult* swap) { int nswap = swapctl(SWAP_NSWAP, 0, 0); if (nswap < 0) return "swapctl(SWAP_NSWAP) failed"; - if (nswap == 0) NULL; + if (nswap == 0) return NULL; - struct swapent* swdev = malloc((uint32_t) nswap * sizeof(*swdev)); + FF_AUTO_FREE struct swapent* swdev = malloc((uint32_t) nswap * sizeof(*swdev)); if (swapctl(SWAP_STATS, swdev, nswap) < 0) return "swapctl(SWAP_STATS) failed"; + uint64_t swapTotal = 0, swapUsed = 0; for (int i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { - swap->bytesUsed += (uint64_t) swdev[i].se_inuse * DEV_BSIZE; - swap->bytesTotal += (uint64_t) swdev[i].se_nblks * DEV_BSIZE; + swapUsed += (uint64_t) swdev[i].se_inuse; + swapTotal += (uint64_t) swdev[i].se_nblks; } } + swap->bytesUsed = swapUsed * DEV_BSIZE; + swap->bytesTotal = swapTotal * DEV_BSIZE; return NULL; }