shithub: neatroff

Download patch

ref: 3229a5cd02f387a5718a086afefa22d92d87a43f
parent: 2f74eab4dfbc869fbbf2f02388ae91ddb64b9310
author: Ali Gholami Rudi <[email protected]>
date: Sat Sep 17 13:21:50 EDT 2022

eval: prevent integer overflow for fixed point numbers

--- a/eval.c
+++ b/eval.c
@@ -38,6 +38,10 @@
 	int n = 0;		/* the result */
 	int mag = 0;		/* n should be divided by mag */
 	while (isdigit((unsigned char) *s) || *s == '.') {
+		if (mag == MAXFRAC || (mag > 0 && n > 200000000u)) {
+			s++;
+			continue;
+		}
 		if (*s == '.') {
 			mag = 1;
 			s++;
@@ -45,10 +49,6 @@
 		}
 		mag *= 10;
 		n = n * 10 + *s++ - '0';
-	}
-	if (mag > MAXFRAC) {
-		n /= mag / MAXFRAC;
-		mag /= mag / MAXFRAC;
 	}
 	n = readunit(*s && strchr(SCHAR, *s) ? *s++ : defunit, n);
 	*_s = s;