[master] d69149c7a Make SF_Parse_(numeric) ignore leading and trailing ows.

Poul-Henning Kamp phk at FreeBSD.org
Mon May 31 17:13:05 UTC 2021


commit d69149c7a0c441fcc8c8c9f35093dbd396888bfc
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon May 31 17:11:28 2021 +0000

    Make SF_Parse_(numeric) ignore leading and trailing ows.

diff --git a/lib/libvarnish/vnum.c b/lib/libvarnish/vnum.c
index 2cf665480..ce66fc7e1 100644
--- a/lib/libvarnish/vnum.c
+++ b/lib/libvarnish/vnum.c
@@ -81,6 +81,8 @@ sf_parse_int(const char **ipp, const char **errtxt, int maxdig)
 		retval *= 10;
 		retval += *(*ipp)++ - 0x30;
 	}
+	while (vct_isows(*(*ipp)))
+		(*ipp)++;
 	if (ndig == 0)
 		BAIL(negative ? err_invalid_num : err_miss_num);
 	if (negative)
@@ -98,48 +100,56 @@ SF_Parse_Integer(const char **ipp, const char **errtxt)
 double
 SF_Parse_Decimal(const char **ipp, const char **errtxt)
 {
-	double retval;
+	double retval, scale = 1;
+	int ndig;
 
 	retval = (double)sf_parse_int(ipp, errtxt, 12);
-	if (*(*ipp) != '.')
-		return (retval);
-	(*ipp)++;
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .1 * (*(*ipp)++ - 0x30);
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .01 * (*(*ipp)++ - 0x30);
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .001 * (*(*ipp)++ - 0x30);
-	if (vct_isdigit(*(*ipp)))
-		BAIL(err_fatnum);
+	if (retval < 0)
+		scale = -scale;
+	do {
+		if (*(*ipp) != '.')
+			break;
+		(*ipp)++;
+		for(ndig = 0; ndig < 3; ndig++) {
+			scale *= .1;
+			if (!vct_isdigit(*(*ipp)))
+				break;
+			retval += scale * (*(*ipp)++ - 0x30);
+		}
+		if (vct_isdigit(*(*ipp)))
+			BAIL(err_fatnum);
+	} while (0);
+	while (vct_isows(*(*ipp)))
+		(*ipp)++;
 	return (retval);
 }
 
 double
 SF_Parse_Number(const char **ipp, const char **errtxt)
 {
-	double retval;
+	double retval, scale = 1;
+	int ndig;
 
 	retval = (double)sf_parse_int(ipp, errtxt, 15);
-	if (*(*ipp) != '.')
-		return (retval);
-	if (retval < -999999999999 || retval > 999999999999)
-		BAIL(err_fatnum);
-	(*ipp)++;
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .1 * (*(*ipp)++ - 0x30);
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .01 * (*(*ipp)++ - 0x30);
-	if (!vct_isdigit(*(*ipp)))
-		return (retval);
-	retval += .001 * (*(*ipp)++ - 0x30);
-	if (vct_isdigit(*(*ipp)))
-		BAIL(err_fatnum);
+	if (retval < 0)
+		scale = -scale;
+	do {
+		if (*(*ipp) != '.')
+			break;
+		if (retval < -999999999999 || retval > 999999999999)
+			BAIL(err_fatnum);
+		(*ipp)++;
+		for(ndig = 0; ndig < 3; ndig++) {
+			scale *= .1;
+			if (!vct_isdigit(*(*ipp)))
+				break;
+			retval += scale * (*(*ipp)++ - 0x30);
+		}
+		if (vct_isdigit(*(*ipp)))
+			BAIL(err_fatnum);
+	} while (0);
+	while (vct_isows(*(*ipp)))
+		(*ipp)++;
 	return (retval);
 }
 


More information about the varnish-commit mailing list