[master] 0f7f757c4 sensible limits for VCL_INT and VCL_BYTES

Nils Goroll nils.goroll at uplex.de
Wed Feb 27 16:54:09 UTC 2019


commit 0f7f757c478351e7a30c9820f7542b868743b7b8
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Feb 27 15:40:38 2019 +0100

    sensible limits for VCL_INT and VCL_BYTES

diff --git a/include/vrt.h b/include/vrt.h
index 72c9c4d78..2b377947a 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -211,6 +211,21 @@ typedef vtim_real				VCL_TIME;
 typedef struct vcl *				VCL_VCL;
 typedef void					VCL_VOID;
 
+/*
+ * technically, as our VCL_INT is int64_t, its limits are INT64_MIN/INT64_MAX.
+ *
+ * Yet, for conversions, we use VNUMpfx with a double intermediate, so above
+ * 2^53 we see rounding errors. In order to catch a potential floor rounding
+ * error, we make our limit 2^53-1
+ *
+ * Ref: https://stackoverflow.com/a/1848762
+ */
+#define VCL_INT_MAX ((INT64_C(1)<<53)-1)
+#define VCL_INT_MIN (-VCL_INT_MAX)
+
+#define VCL_BYTES_MAX VCL_INT_MAX
+#define VCL_BYTES_MIN 0
+
 struct vrt_type {
 	unsigned			magic;
 #define VRT_TYPE_MAGIC			0xa943bc32
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index a072cf5ef..ed85bed0f 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -81,7 +81,7 @@ vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
 		return (i);
 
 	r = trunc(r);
-	if (r > INT64_MAX || r < INT64_MIN)
+	if (r > VCL_INT_MAX || r < VCL_INT_MIN)
 		return (i);
 
 	return ((VCL_INT)r);
@@ -158,7 +158,7 @@ vmod_real2integer(VRT_CTX, VCL_REAL r, VCL_INT i)
 	if (!isfinite(r))
 		return (i);
 	r = round(r);
-	if (r > INT64_MAX || r < INT64_MIN)
+	if (r > VCL_INT_MAX || r < VCL_INT_MIN)
 		return(i);
 	return ((VCL_INT)r);
 }
@@ -182,7 +182,7 @@ vmod_time2integer(VRT_CTX, VCL_TIME t, VCL_INT i)
 	if (!isfinite(t))
 		return (i);
 	t = round(t);
-	if (t > INT64_MAX || t < INT64_MIN)
+	if (t > VCL_INT_MAX || t < VCL_INT_MIN)
 		return(i);
 	return ((VCL_INT)t);
 }


More information about the varnish-commit mailing list