[master] 9bb8f96 Use VNUM() rather than strtod()

Poul-Henning Kamp phk at FreeBSD.org
Tue Feb 24 09:53:09 CET 2015


commit 9bb8f96223d248beb5962961b493b16dcc8ecc2a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Feb 24 08:52:54 2015 +0000

    Use VNUM() rather than strtod()

diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index a17961d..b498efc 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -50,6 +50,7 @@
 #endif
 
 #include <errno.h>
+#include <math.h>
 #include <fcntl.h>
 #include <poll.h>
 #include <stdint.h>
@@ -61,6 +62,7 @@
 #include "vapi/vsm.h"
 #include "vas.h"
 #include "vcli.h"
+#include "vnum.h"
 #include "vss.h"
 
 #define RL_EXIT(status) \
@@ -464,7 +466,9 @@ main(int argc, char * const *argv)
 			T_arg = optarg;
 			break;
 		case 't':
-			timeout = strtod(optarg, NULL);
+			timeout = VNUM(optarg);
+			if (isnan(timeout))
+				usage();
 			break;
 		default:
 			usage();
diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index f6f9bc8..ce425eb 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -58,6 +58,7 @@
 #include "vapi/voptget.h"
 #include "vas.h"
 #include "vcs.h"
+#include "vnum.h"
 #include "vsb.h"
 #include "vut.h"
 #include "vqueue.h"
@@ -283,16 +284,13 @@ static int __match_proto__(format_f)
 format_time(const struct format *format)
 {
 	double t_start, t_end;
-	char *p;
 	char buf[64];
 	time_t t;
 	struct tm tm;
 
 	CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
 	if (CTX.frag[F_tstart].gen == CTX.gen) {
-		t_start = strtod(CTX.frag[F_tstart].b, &p);
-		if (p != CTX.frag[F_tstart].e)
-			t_start = NAN;
+		t_start = VNUM(CTX.frag[F_tstart].b);
 	} else
 		t_start = NAN;
 	if (isnan(t_start)) {
@@ -305,8 +303,8 @@ format_time(const struct format *format)
 
 	/* Missing t_end defaults to t_start */
 	if (CTX.frag[F_tend].gen == CTX.gen) {
-		t_end = strtod(CTX.frag[F_tend].b, &p);
-		if (p != CTX.frag[F_tend].e)
+		t_end = VNUM(CTX.frag[F_tend].b);
+		if (isnan(t_end))
 			t_end = t_start;
 	} else
 		t_end = t_start;
diff --git a/lib/libvarnishapi/vsl_arg.c b/lib/libvarnishapi/vsl_arg.c
index 376b056..4a9b897 100644
--- a/lib/libvarnishapi/vsl_arg.c
+++ b/lib/libvarnishapi/vsl_arg.c
@@ -36,6 +36,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <math.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -49,6 +50,7 @@
 #include "vapi/vsl.h"
 #include "vapi/vsm.h"
 #include "vbm.h"
+#include "vnum.h"
 #include "vre.h"
 #include "vsl_api.h"
 #include "vsm_api.h"
@@ -343,13 +345,11 @@ VSL_Arg(struct VSL_data *vsl, int opt, const char *arg)
 		vsl->L_opt = (int)l;
 		return (1);
 	case 'T':
-		d = strtod(arg, &p);
-		while (isspace(*p))
-			p++;
-		if (*p != '\0')
-			return (vsl_diag(vsl, "-P: Syntax error"));
+		d = VNUM(arg);
+		if (!isnan(d))
+			return (vsl_diag(vsl, "-T: Syntax error"));
 		if (d < 0.)
-			return (vsl_diag(vsl, "-L: Range error"));
+			return (vsl_diag(vsl, "-T: Range error"));
 		vsl->T_opt = d;
 		return (1);
 	case 'v': vsl->v_opt = 1; return (1);
diff --git a/lib/libvarnishapi/vsl_query.c b/lib/libvarnishapi/vsl_query.c
index 6136115..a271561 100644
--- a/lib/libvarnishapi/vsl_query.c
+++ b/lib/libvarnishapi/vsl_query.c
@@ -29,17 +29,19 @@
  */
 
 #include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
+#include <ctype.h>
+#include <math.h>
 #include <stdint.h>
 #include <string.h>
-#include <ctype.h>
+#include <stdlib.h>
+#include <unistd.h>
 
-#include "vas.h"
 #include "miniobj.h"
+#include "vas.h"
+#include "vbm.h"
+#include "vnum.h"
 #include "vre.h"
 #include "vsb.h"
-#include "vbm.h"
 
 #include "vapi/vsl.h"
 #include "vsl_api.h"
@@ -141,11 +143,10 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
 			/* Can't parse - no match */
 			return (0);
 		case VEX_FLOAT:
-			lhs_float = strtod(b, &p);
-			if (*p == '\0' || isspace(*p))
-				break;
-			/* Can't parse - no match */
-			return (0);
+			lhs_float = VNUM(b);
+			if (isnan(lhs_float))
+				return (0);
+			break;
 		default:
 			WRONG("Wrong RHS type");
 		}
diff --git a/lib/libvarnishapi/vxp_parse.c b/lib/libvarnishapi/vxp_parse.c
index 3b9e1d0..13472a2 100644
--- a/lib/libvarnishapi/vxp_parse.c
+++ b/lib/libvarnishapi/vxp_parse.c
@@ -30,18 +30,19 @@
 
 #include "config.h"
 
+#include <ctype.h>
+#include <errno.h>
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <stdint.h>
 #include <string.h>
-#include <ctype.h>
 #include <stdint.h>
 
+#include "miniobj.h"
 #include "vas.h"
-#include "vsb.h"
 #include "vbm.h"
-#include "miniobj.h"
+#include "vnum.h"
+#include "vsb.h"
 #include "vapi/vsl.h"
 #include "vsl_api.h"
 
@@ -191,10 +192,8 @@ vxp_expr_num(struct vxp *vxp, struct vex_rhs **prhs)
 	AN(*prhs);
 	if (strchr(vxp->t->dec, '.')) {
 		(*prhs)->type = VEX_FLOAT;
-		(*prhs)->val_float = strtod(vxp->t->dec, &endptr);
-		while (isspace(*endptr))
-			endptr++;
-		if (*endptr != '\0') {
+		(*prhs)->val_float = VNUM(vxp->t->dec);
+		if (isnan((*prhs)->val_float)) {
 			VSB_printf(vxp->sb, "Floating point parse error ");
 			vxp_ErrWhere(vxp, vxp->t, -1);
 			return;



More information about the varnish-commit mailing list