Fixed grammar and improved commit message (sorry)

From c9795aa1591c88b0c7154b968b56125e1b9d187c Mon Sep 17 00:00:00 2001
From: Alexander Schwinn <[email protected]>
Date: Tue, 19 Sep 2023 16:10:18 +0200
Subject: [PATCH] Improve step-size out of bound handling

Will now print a message to stderr and fallback to default stepsize '1'
---
 entry.c | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/entry.c b/entry.c
index 03273a3..cc1e2ab 100644
--- a/entry.c
+++ b/entry.c
@@ -362,26 +362,26 @@ get_range(bits, low, high, names, ch, file)
 	 */
 
 	register int	i;
-	auto int	num1, num2, num3;
+	auto int	low_, high_, step;
 
 	Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
 
 	if (ch == '*') {
 		/* '*' means "first-last" but can still be modified by /step
 		 */
-		num1 = low;
-		num2 = high;
+		low_ = low;
+		high_ = high;
 		ch = get_char(file);
 		if (ch == EOF)
 			return EOF;
 	} else {
-		if (EOF == (ch = get_number(&num1, low, names, ch, file)))
+		if (EOF == (ch = get_number(&low_, low, names, ch, file)))
 			return EOF;
 
 		if (ch != '-') {
 			/* not a range, it's a single number.
 			 */
-			if (EOF == set_element(bits, low, high, num1))
+			if (EOF == set_element(bits, low, high, low_))
 				return EOF;
 			return ch;
 		} else {
@@ -393,7 +393,7 @@ get_range(bits, low, high, names, ch, file)
 
 			/* get the number following the dash
 			 */
-			ch = get_number(&num2, low, names, ch, file);
+			ch = get_number(&high_, low, names, ch, file);
 			if (ch == EOF)
 				return EOF;
 		}
@@ -413,21 +413,42 @@ get_range(bits, low, high, names, ch, file)
 		 * element id, it's a step size.  'low' is
 		 * sent as a 0 since there is no offset either.
 		 */
-		ch = get_number(&num3, 0, PPC_NULL, ch, file);
+		ch = get_number(&step, 0, PPC_NULL, ch, file);
+
+		/* Make sure the step size makes any sense */
+		if (step > 1) {
+			if (high == high_ && low == low_) {
+				if (step > (high_ - low_)) {
+					fprintf(stderr, "Step size of '%i' will be ignored, since it exceeds the maximum possible step size of '%i' for the specified entry'\n", step, high_ - low_);
+					step = 1;
+				}
+			}
+			else { /* a range was specified */
+				if (high_ == low_) {
+					fprintf(stderr, "Step size of '%i' will be ignored for this entry, since it exceeds the maximum possible step size of '1' for the specifed range of '%i-%i'\n", step, low_, high_);
+					step = 1;
+				}
+				else if (step > (high_ - low_)) {
+					fprintf(stderr, "Step size of '%i' will be ignored for this entry, since it exceeds the maximum possible step size of '%i' for the specifed range of '%i-%i'\n", step, high_ - low_, low_, high_);
+					step = 1;
+				}
+			}
+		}
+
 		if (ch == EOF)
 			return EOF;
 	} else {
 		/* no step.  default==1.
 		 */
-		num3 = 1;
+		step = 1;
 	}
 
-	/* range. set all elements from num1 to num2, stepping
-	 * by num3.  (the step is a downward-compatible extension
+	/* range. set all elements from low_ to high_, stepping
+	 * by 'step'.  (the step is a downward-compatible extension
 	 * proposed conceptually by bob@acornrc, syntactically
 	 * designed then implmented by paul vixie).
 	 */
-	for (i = num1;  i <= num2;  i += num3)
+	for (i = low_;  i <= high_;  i += step)
 		if (EOF == set_element(bits, low, high, i))
 			return EOF;
 
-- 
2.39.2

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to