[PATCH] beep: fix division by zero when -f 0 is passed

Anton Moryakov via busybox <[email protected]> Tue, 26 May 2026 11:14:48 +0300
Newsgroups gmane.linux.busybox
Message-ID <[email protected]>
Static analysis reported potential division by zero at:
    tickrate_div_freq = CLOCK_TICK_RATE / xatou(optarg);

If user passes \"-f 0\", xatou() returns 0, causing undefined behavior.

Fix: validate frequency range (20-50000 Hz) to match CONFIG limits.
Reject values outside range with clear error message.

Also removes the old TODO comment about handling -f 0.

Signed-off-by: Anton Moryakov <[email protected]>
---
 miscutils/beep.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/miscutils/beep.c b/miscutils/beep.c
index 724a666c8..1d713a004 100644
--- a/miscutils/beep.c
+++ b/miscutils/beep.c
@@ -86,8 +86,13 @@ int beep_main(int argc, char **argv)
  */
 		switch (c) {
 		case 'f':
-/* TODO: what "-f 0" should do? */
-			tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg);
+/* Frequency 0 is invalid, rejected above */
+			{	
+				unsigned freq = xatou(optarg);
+				if (freq < 20 || freq > 50000)
+					bb_error_msg_and_die("frequency must be 20-50000 Hz");
+				tickrate_div_freq = CLOCK_TICK_RATE / freq;
+			}
 			continue;
 		case 'l':
 			length = xatou(optarg);
-- 
2.39.2