[PATCH] util-linux: readprofile.c: prevent division by zero in trailer output

Anton Moryakov via busybox <[email protected]> Sun, 17 May 2026 17:29:48 +0300
Newsgroups gmane.linux.busybox
Message-ID <[email protected]>
Static analysis (SAST) reported a potential division by zero:

  readprofile.c:262: total/(double)(fn_add-add0)
  Warning: (fn_add - add0) can be zero if fn_add equals add0,
  leading to undefined behaviour (inf/nan in printf).

This can occur with malformed System.map files or edge cases where
the first text symbol has the same address as _stext.

Fix: add conditional check before division. If fn_add == add0,
output 0.0 instead of attempting division. This is safe because
the "total" line is informational, and zero density is a reasonable
fallback when address range is unknown.

Signed-off-by: Anton Moryakov <[email protected]>
---
 util-linux/readprofile.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/util-linux/readprofile.c b/util-linux/readprofile.c
index a7e4dcb73..6fa5c86e5 100644
--- a/util-linux/readprofile.c
+++ b/util-linux/readprofile.c
@@ -258,11 +258,11 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv)
 
 	/* trailer */
 	if (optVerbose)
-		printf("%016x %-40s %6u %8.4f\n",
-			0, "total", total, total/(double)(fn_add-add0));
+		printf("%016x %-40s %6u %8.4f\n", 0, "total", total,
+			(fn_add != add0) ? total/(double)(fn_add-add0) : 0.0);
 	else
-		printf("%6u %-40s %8.4f\n",
-			total, "total", total/(double)(fn_add-add0));
+		printf("%6u %-40s %8.4f\n", total, "total",
+			(fn_add != add0) ? total/(double)(fn_add-add0) : 0.0);
 
 	if (ENABLE_FEATURE_CLEAN_UP) {
 		fclose(map);
-- 
2.39.2