[master] 9e561a693 Switch more of VCC to RFC8941 number parsing

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


commit 9e561a693460c774b7b5ba66d0cf972e1b468b55
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 21 08:26:27 2021 +0000

    Switch more of VCC to RFC8941 number parsing

diff --git a/include/vnum.h b/include/vnum.h
index 09523f6f3..181ecee7c 100644
--- a/include/vnum.h
+++ b/include/vnum.h
@@ -38,11 +38,8 @@ vtim_dur VNUM_duration(const char *p);
 double VNUM_bytes_unit(double r, const char *b, const char *e, uintmax_t rel);
 const char *VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel);
 
-#if 0
 int64_t SF_Parse_Integer(const char **ipp, const char **errtxt);
 double SF_Parse_Decimal(const char **ipp, const char **errtxt);
-#endif
-
 double SF_Parse_Number(const char **ipp, const char **errtxt);
 
 #define VNUM_LEGAL_DURATION \
diff --git a/lib/libvarnish/vnum.c b/lib/libvarnish/vnum.c
index 207a16297..7397eea41 100644
--- a/lib/libvarnish/vnum.c
+++ b/lib/libvarnish/vnum.c
@@ -86,8 +86,6 @@ sf_parse_int(const char **ipp, const char **errtxt, int maxdig)
 	return (retval);
 }
 
-#if 0
-
 int64_t
 SF_Parse_Integer(const char **ipp, const char **errtxt)
 {
@@ -118,8 +116,6 @@ SF_Parse_Decimal(const char **ipp, const char **errtxt)
 	return (retval);
 }
 
-#endif
-
 double
 SF_Parse_Number(const char **ipp, const char **errtxt)
 {
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index 5c4139153..f97e5b46d 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -433,7 +433,7 @@ void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport,
 double vcc_DurationUnit(struct vcc *);
 void vcc_ByteVal(struct vcc *, VCL_INT *);
 void vcc_Duration(struct vcc *tl, double *);
-unsigned vcc_UintVal(struct vcc *tl);
+uint64_t vcc_UintVal(struct vcc *tl);
 int vcc_IsFlag(struct vcc *tl);
 int vcc_IsFlagRaw(struct vcc *, const struct token *, const struct token *);
 char *vcc_Dup_be(const char *b, const char *e);
diff --git a/lib/libvcc/vcc_utils.c b/lib/libvcc/vcc_utils.c
index 3c6ac8c29..51ed651c7 100644
--- a/lib/libvcc/vcc_utils.c
+++ b/lib/libvcc/vcc_utils.c
@@ -281,39 +281,51 @@ vcc_DurationUnit(struct vcc *tl)
  * The tokenizer made sure we only get digits.
  */
 
-unsigned
+uint64_t
 vcc_UintVal(struct vcc *tl)
 {
-	unsigned d = 0;
-	const char *p;
+	const char *p, *errtxt;
+	int64_t retval;
 
 	if (tl->t->tok != CNUM) {
 		Expect(tl, CNUM);
-	} else {
-		for (p = tl->t->b; p < tl->t->e; p++) {
-			d *= 10;
-			d += *p - '0';
-		}
-		vcc_NextToken(tl);
+		return (0);
+	}
+	p = tl->t->b;
+	retval = SF_Parse_Integer(&p, &errtxt);
+	if (errno) {
+		VSB_printf(tl->sb, "Bad UINT: %s\n", errtxt);
+		vcc_ErrWhere(tl, tl->t);
+		return (0);
+	}
+	if (retval < 0) {
+		VSB_printf(tl->sb, "UINT cannot be negative\n");
+		vcc_ErrWhere(tl, tl->t);
+		return (0);
 	}
-	return (d);
+	vcc_NextToken(tl);
+	return (retval);
 }
 
 static double
 vcc_DoubleVal(struct vcc *tl)
 {
-	const size_t l = tl->t->e - tl->t->b;
-	char buf[l + 1];
+	const char *p, *errtxt;
+	double retval;
 
 	if (tl->t->tok != CNUM && tl->t->tok != FNUM) {
 		Expect(tl, CNUM);
 		return (0);
 	}
-	assert(tl->t->tok == CNUM || tl->t->tok == FNUM);
-	memcpy(buf, tl->t->b, l);
+	p = tl->t->b;
+	retval = SF_Parse_Decimal(&p, &errtxt);
+	if (errno) {
+		VSB_printf(tl->sb, "Bad REAL: %s\n", errtxt);
+		vcc_ErrWhere(tl, tl->t);
+		return (0);
+	}
 	vcc_NextToken(tl);
-	buf[l] = '\0';
-	return (strtod(buf, NULL));
+	return (retval);
 }
 
 /*--------------------------------------------------------------------*/


More information about the varnish-commit mailing list