[m-rev.] Bugfix for string__to_float

Ralph Becket rafe at cs.mu.OZ.AU
Mon Feb 11 18:41:25 AEDT 2002


Estimated hours taken: 1
Branches: main

library/string.m:
	Fixed a bug in string__to_float/2 which did not fail when it
	should have (i.e. it now also fails if the string argument
	has preceding whitespace or any trailing characters.)

Index: string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.165
diff -u -r1.165 string.m
--- string.m	20 Jan 2002 07:32:26 -0000	1.165
+++ string.m	11 Feb 2002 07:33:00 -0000
@@ -1634,6 +1634,7 @@
 % The remaining routines are implemented using the C interface.
 
 :- pragma c_header_code("
+#include <ctype.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -1675,12 +1676,19 @@
 :- pragma foreign_proc("C",
 	string__to_float(FloatString::in, FloatVal::out),
 		[will_not_call_mercury, promise_pure, thread_safe], "{
-	/* use a temporary, since we can't don't know whether FloatVal
-	   is a double or float */
-	double tmp;
-	SUCCESS_INDICATOR = (sscanf(FloatString, ""%lf"", &tmp) == 1);
+	/*
+	** Use a temporary, since we can't don't know whether FloatVal is a
+	** double or float.  The %c checks for any erroneous characters
+	** appearing after the float; if there are then sscanf() will
+	** return 2 rather than 1.
+	*/
+	double tmpf;
+	char   tmpc;
+	SUCCESS_INDICATOR =
+		(!MR_isspace(FloatString[0])) &&
+		(sscanf(FloatString, ""%lf%c"", &tmpf, &tmpc) == 1);
 		/* TRUE if sscanf succeeds, FALSE otherwise */
-	FloatVal = tmp;
+	FloatVal = tmpf;
 }").
 
 :- pragma foreign_proc("MC++",
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list