[master] 2761c43 Let std.integer also do real to integer conversion

Federico G. Schwindt fgsch at lodoss.net
Sat Sep 10 21:27:09 CEST 2016


commit 2761c4362cea5604ff3e27acb5cc1283e503269a
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Sat Sep 10 20:21:14 2016 +0100

    Let std.integer also do real to integer conversion
    
    With input from nigoroll.

diff --git a/bin/varnishtest/tests/m00016.vtc b/bin/varnishtest/tests/m00016.vtc
index a216085..201682e 100644
--- a/bin/varnishtest/tests/m00016.vtc
+++ b/bin/varnishtest/tests/m00016.vtc
@@ -19,6 +19,9 @@ varnish v1 -vcl+backend {
 		set resp.http.x-qux = std.real2integer(
 		    std.real(req.http.foo, 2.0), 2);
 
+		set resp.http.x-xyzzy = std.integer(
+		    std.real(req.http.foo, 2.0), 2);
+
 		# Representation of 9e99, which is larger than what fits in the
 		# 128bit integers on $exotic_platform.
 		set resp.http.x-int-fallback = std.real2integer(9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000, 2);
@@ -31,6 +34,7 @@ client c1 {
 	expect resp.http.x-foo == resp.http.x-bar
 	expect resp.http.x-baz == 1140618699.000
 	expect resp.http.x-qux == 1140618699
+	expect resp.http.x-xyzzy == resp.http.x-qux
 	expect resp.http.x-int-fallback == 2
 } -run
 
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 1506ec9..778fa6f 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -102,28 +102,23 @@ vmod_duration(VRT_CTX, VCL_STRING p, VCL_DURATION d)
 VCL_INT __match_proto__(td_std_integer)
 vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
 {
-	char *e;
-	long r;
+	const char *e;
+	double r;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 
 	if (p == NULL)
 		return (i);
 
-	while(isspace(*p))
-		p++;
-
-	if (*p != '+' && *p != '-' && !isdigit(*p))
+	r = VNUMpfx(p, &e);
+	if (isnan(r) || e != NULL)
 		return (i);
 
-	e = NULL;
-
-	r = strtol(p, &e, 0);
-
-	if (e == NULL || *e != '\0')
+	r = trunc(r);
+	if (r > LONG_MAX || r < LONG_MIN)
 		return (i);
 
-	return (r);
+	return ((long)r);
 }
 
 VCL_IP



More information about the varnish-commit mailing list