[m-rev.] diff: fix string.base_string_to_int succeeding incorrectly

Peter Wang novalazy at gmail.com
Mon Feb 11 11:09:11 AEDT 2013


Branches: main, release
---

Fix string.base_string_to_int succeeding incorrectly.

string.base_string_to_int would incorrectly succeed on an input string
containing a code point of multiple code units.  string.foldl_between
expects the length of the string in code units, but was passed the
length of the string in code points.

library/string.m:
    	As above.

tests/general/base_string_to_int_test.exp:
tests/general/base_string_to_int_test.m:
    	Extend test case.

diff --git a/library/string.m b/library/string.m
index fb3cbdd..884f7f7 100644
--- a/library/string.m
+++ b/library/string.m
@@ -1185,15 +1185,15 @@ string.to_int(String, Int) :-
 
 string.base_string_to_int(Base, String, Int) :-
     string.index(String, 0, Char),
-    Len = string.count_codepoints(String),
+    End = string.count_code_units(String),
     ( Char = ('-') ->
-        Len > 1,
-        foldl_between(accumulate_negative_int(Base), String, 1, Len, 0, Int)
+        End > 1,
+        foldl_between(accumulate_negative_int(Base), String, 1, End, 0, Int)
     ; Char = ('+') ->
-        Len > 1,
-        foldl_between(accumulate_int(Base), String, 1, Len, 0, Int)
+        End > 1,
+        foldl_between(accumulate_int(Base), String, 1, End, 0, Int)
     ;
-        foldl_between(accumulate_int(Base), String, 0, Len, 0, Int)
+        foldl_between(accumulate_int(Base), String, 0, End, 0, Int)
     ).
 
 :- pred accumulate_int(int::in, char::in, int::in, int::out) is semidet.
diff --git a/tests/general/base_string_to_int_test.exp b/tests/general/base_string_to_int_test.exp
index da139bc..a3f915c 100644
--- a/tests/general/base_string_to_int_test.exp
+++ b/tests/general/base_string_to_int_test.exp
@@ -8,4 +8,8 @@ string.base_string_to_int(10, "123abc", _) failed.
 string.base_string_to_int(10, "abc", _) failed.
 string.base_string_to_int(10, "+abc", _) failed.
 string.base_string_to_int(10, "-abc", _) failed.
+string.base_string_to_int(10, "Σ", _) failed.
+string.base_string_to_int(10, "-Σ", _) failed.
+string.base_string_to_int(10, "+Σ", _) failed.
+string.base_string_to_int(10, "123Σ", _) failed.
 min_int ok.
diff --git a/tests/general/base_string_to_int_test.m b/tests/general/base_string_to_int_test.m
index 510ed65..161a5a9 100644
--- a/tests/general/base_string_to_int_test.m
+++ b/tests/general/base_string_to_int_test.m
@@ -36,6 +36,10 @@ main(!IO) :-
     test("abc", !IO),
     test("+abc", !IO),
     test("-abc", !IO),
+    test("Σ", !IO),
+    test("-Σ", !IO),
+    test("+Σ", !IO),
+    test("123Σ", !IO),
     ( int.bits_per_int = 32 ->
         MinIntStr = "-2147483648"
     ; int.bits_per_int = 64 ->




More information about the reviews mailing list