[master] c50ff59c1 Switch std_conversion onto SF_number functions

Poul-Henning Kamp phk at FreeBSD.org
Tue Jun 1 09:15:09 UTC 2021


commit c50ff59c1d0fa9a7369e9f994e60bd0f431499ce
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jun 1 09:13:50 2021 +0000

    Switch std_conversion onto SF_number functions

diff --git a/bin/varnishtest/tests/m00015.vtc b/bin/varnishtest/tests/m00015.vtc
index 3aebc7e4d..2df533331 100644
--- a/bin/varnishtest/tests/m00015.vtc
+++ b/bin/varnishtest/tests/m00015.vtc
@@ -98,7 +98,7 @@ client c1 {
 	txreq -hdr "foo: 999999999999.999" \
 	      -hdr "bytes: 999999999999b" \
 	      -hdr "duration: 999999999999.999s" \
-	      -hdr "integer: 999999999999.999" \
+	      -hdr "integer: 999999999999.000" \
 	      -hdr "time: 999999999999.999"
 	rxresp
 	expect resp.http.converted == 999999999999.999
diff --git a/vmod/vmod_std_conversions.c b/vmod/vmod_std_conversions.c
index ab7f61cb4..9aaf9a8cf 100644
--- a/vmod/vmod_std_conversions.c
+++ b/vmod/vmod_std_conversions.c
@@ -147,8 +147,8 @@ vmod_bytes(VRT_CTX, struct VARGS(bytes) *a)
 VCL_INT v_matchproto_(td_std_integer)
 vmod_integer(VRT_CTX, struct VARGS(integer) *a)
 {
-	const char *e;
-	double r;
+	const char *p, *errtxt = NULL;
+	double r, tmp;
 	int nargs;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
@@ -167,9 +167,11 @@ vmod_integer(VRT_CTX, struct VARGS(integer) *a)
 		return (a->bytes);
 
 	if (a->valid_s && a->s != NULL) {
-		r = VNUMpfx(a->s, &e);
-		if (e != NULL)
-			r = NAN;
+		p = a->s;
+		r = SF_Parse_Number(&p, &errtxt);
+		if (!errno && *p == '\0' && modf(r, &tmp) == 0.0)
+			return (r);
+		r = NAN;
 	}
 
 	if (a->valid_duration)
@@ -190,7 +192,10 @@ vmod_integer(VRT_CTX, struct VARGS(integer) *a)
 	if (a->valid_fallback)
 		return (a->fallback);
 
-	VRT_fail(ctx, "std.integer: conversion failed");
+	if (errtxt != NULL)
+		VRT_fail(ctx, "std.integer: conversion failed: %s", errtxt);
+	else
+		VRT_fail(ctx, "std.integer: conversion failed");
 	return (0);
 }
 
@@ -234,6 +239,7 @@ VCL_REAL v_matchproto_(td_std_real)
 vmod_real(VRT_CTX, struct VARGS(real) *a)
 {
 	VCL_REAL r;
+	const char *p, *errtxt = NULL;
 	int nargs;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
@@ -260,15 +266,19 @@ vmod_real(VRT_CTX, struct VARGS(real) *a)
 		return ((VCL_REAL)a->time);
 
 	if (a->valid_s && a->s != NULL) {
-		r = VNUM(a->s);
-		if (!isnan(r))
+		p = a->s;
+		r = SF_Parse_Decimal(&p, &errtxt);
+		if (!errno && *p == '\0')
 			return (r);
 	}
 
 	if (a->valid_fallback)
 		return (a->fallback);
 
-	VRT_fail(ctx, "std.real: conversion failed");
+	if (errtxt != NULL)
+		VRT_fail(ctx, "std.real: conversion failed: %s", errtxt);
+	else
+		VRT_fail(ctx, "std.real: conversion failed");
 	return (0);
 }
 


More information about the varnish-commit mailing list