[master] 810dcd936 Enforce the RFC8941 number ranges centrally.

Poul-Henning Kamp phk at FreeBSD.org
Fri May 28 08:32:05 UTC 2021


commit 810dcd9368c2218e4716480d641dfb9857aa7c4e
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 28 08:31:35 2021 +0000

    Enforce the RFC8941 number ranges centrally.

diff --git a/bin/varnishtest/tests/i00001.vtc b/bin/varnishtest/tests/i00001.vtc
index 52c5a1a02..a56300eb7 100644
--- a/bin/varnishtest/tests/i00001.vtc
+++ b/bin/varnishtest/tests/i00001.vtc
@@ -1,17 +1,38 @@
 varnishtest "SF-decimal/SF-integer ranges"
 
+varnish v1 -errvcl {Too many digits for integer.} {
+	sub vcl_recv { set req.http.foo = 1234567890123456; }
+}
+
+varnish v1 -errvcl {Too many digits for real.} {
+	sub vcl_recv { set req.http.foo = 1234567890123.; }
+}
+
+varnish v1 -errvcl {Too many digits for real.} {
+	sub vcl_recv { set req.http.foo = 123456789012.1234; }
+}
+
+varnish v1 -errvcl {Too many digits for real.} {
+	sub vcl_recv { set req.http.foo = 0.1234; }
+}
+
 server s1 {
 	rxreq
 	txresp
 } -start
 
 varnish v1 -vcl+backend {
+	sub vcl_recv {
+		set req.http.foo1 = 123456789012345;
+		set req.http.foo2 = 123456789012.;
+		set req.http.foo3 = 123456789012.123;
+	}
 	sub vcl_deliver {
 		if (req.http.foo) {
 			set resp.http.foo = obj.ttl * 10000000000;
 		}
 		if (req.http.bar) {
-			set resp.http.bar = storage.Transient.free_space * 1000000000000000;
+			set resp.http.bar = storage.Transient.free_space * 10000000 * 100000000;
 		}
 	}
 } -start
diff --git a/lib/libvcc/vcc_token.c b/lib/libvcc/vcc_token.c
index cc8392196..05f8d3627 100644
--- a/lib/libvcc/vcc_token.c
+++ b/lib/libvcc/vcc_token.c
@@ -437,6 +437,41 @@ vcc_delim_token(struct vcc *tl, struct source *sp, const char *p,
 	return (1);
 }
 
+/*--------------------------------------------------------------------
+ * Lex a number, either CNUM or FNUM.
+ * We enforce the RFC8941 restrictions on number of digits here.
+ */
+
+static const char *
+vcc_lex_number(struct vcc *tl, struct source *sp, const char *p)
+{
+	const char *q, *r;
+
+	for (q = p; q < sp->e; q++)
+		if (!vct_isdigit(*q))
+			break;
+	if (*q != '.') {
+		vcc_addtoken(tl, CNUM, sp, p, q);
+		if (q - p > 15) {
+			VSB_cat(tl->sb, "Too many digits for integer.\n");
+			vcc_ErrWhere(tl, tl->t);
+			return (NULL);
+		}
+		return (q);
+	}
+	r = ++q;
+	for (; r < sp->e; r++)
+		if (!vct_isdigit(*r))
+			break;
+	vcc_addtoken(tl, FNUM, sp, p, r);
+	if (q - p > 13 || r - q > 3) {
+		VSB_cat(tl->sb, "Too many digits for real.\n");
+		vcc_ErrWhere(tl, tl->t);
+		return(NULL);
+	}
+	return (r);
+}
+
 /*--------------------------------------------------------------------
  * Lexical analysis and token generation
  */
@@ -588,19 +623,9 @@ vcc_Lexer(struct vcc *tl, struct source *sp)
 
 		/* Match numbers { [0-9]+ } */
 		if (vct_isdigit(*p)) {
-			for (q = p; q < sp->e; q++)
-				if (!vct_isdigit(*q))
-					break;
-			if (*q != '.') {
-				vcc_addtoken(tl, CNUM, sp, p, q);
-				p = q;
-				continue;
-			}
-			for (++q; q < sp->e; q++)
-				if (!vct_isdigit(*q))
-					break;
-			vcc_addtoken(tl, FNUM, sp, p, q);
-			p = q;
+			p = vcc_lex_number(tl, sp, p);
+			if (p == NULL)
+				return;
 			continue;
 		}
 		vcc_addtoken(tl, EOI, sp, p, p + 1);


More information about the varnish-commit mailing list