diff --git a/Changes.md b/Changes.md index 31df57f5..5a36b01e 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,8 @@ libhxcpp 6.1.0 (unreleased) ------------------------------------------------------------ +* Fixed a String in a Dynamic, taken as an Int, giving different results on different platforms for a number past the Int range: "10000000000" gave 2147483647 on Windows and 1410065408 on Linux and macOS. It went through the C library's atoi, whose result there is undefined. Such a number saturates to the Int range on every platform now, as a decimal one does in Std.parseInt, in compiled code and in cppia. Numbers in the range, and text with no number, convert as before + libhxcpp 6.0.4 ------------------------------------------------------------ diff --git a/src/String.cpp b/src/String.cpp index 1a294efd..b9bbf2f6 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -3458,6 +3458,34 @@ static String sStringFields[] = { String(null()) }; +// A String in a Dynamic converts to an Int as atoi reads it: spaces, a sign +// and decimal digits, and 0 if there are none. atoi's result for a number past +// an Int is undefined, and the C libraries differ: MSVC's saturates, glibc's +// and Apple's go through a 64-bit long and wrap, so "10000000000" gave +// 2147483647 on Windows and 1410065408 elsewhere. A number past an Int +// saturates on every platform here, as a decimal one does in Std.parseInt. +// The spaces are those isspace has in the C locale, whatever the locale is +static int hxStringToInt(const char *inStr) +{ + while (*inStr==' ' || (*inStr>='\t' && *inStr<='\r')) + inStr++; + bool negative = *inStr=='-'; + if (*inStr=='-' || *inStr=='+') + inStr++; + // Digits past the range leave the value past it, and no larger + long long value = 0; + for(; *inStr>='0' && *inStr<='9'; inStr++) + if (value<=0x80000000LL) + value = value*10 + (*inStr-'0'); + if (negative) + value = -value; + if (value>0x7fffffffLL) + return 0x7fffffff; + if (value< -0x80000000LL) + return -0x7fffffff-1; + return (int)value; +} + namespace hx { @@ -3506,7 +3534,7 @@ class StringData : public hx::Object int __ToInt() const HXCPP_OVERRIDE { if (!mValue.raw_ptr()) return 0; - return atoi(mValue.utf8_str()); + return hxStringToInt(mValue.utf8_str()); } int __Compare(const hx::Object *inRHS) const HXCPP_OVERRIDE diff --git a/test/regression/DynamicStringToInt/Main.hx b/test/regression/DynamicStringToInt/Main.hx new file mode 100644 index 00000000..b99e9fb7 --- /dev/null +++ b/test/regression/DynamicStringToInt/Main.hx @@ -0,0 +1,123 @@ +// A String in a Dynamic, taken as an Int, is read as the C library's atoi reads +// it: spaces, a sign and decimal digits, and 0 if there are none. A number past +// an Int saturates to the Int range on every platform, as a decimal one does in +// Std.parseInt. It was atoi's, which is undefined there: MSVC's saturated, +// glibc's and Apple's wrapped, so "10000000000" gave 2147483647 on Windows and +// 1410065408 on Linux and macOS. +// The same cases run as compiled code, then as a script with the JIT off and on, +// and print the same lines each time. +function main() { + Sys.println("-- compiled"); + Compiled.run(); + var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]); + for (jit in [false, true]) { + Sys.println(jit ? "-- script, JIT on" : "-- script, JIT off"); + cpp.cppia.Host.enableJit(jit); + try { + cpp.cppia.Host.runFile(script); + } catch (e:Dynamic) { + Sys.println("error: " + e); + } + } +} + +// The same cases as Script.hx, compiled into the host +class CompiledHolder { + public var value:Int = 0; + + public function new() {} +} + +class Compiled { + static var allAgree = true; + + static function viaLocal(d:Dynamic):Int { + var i:Int = d; + return i; + } + + static function takeInt(i:Int):Int + return i; + + static function viaArgument(d:Dynamic):Int + return takeInt(d); + + static function viaField(d:Dynamic):Int { + var h = new CompiledHolder(); + h.value = d; + return h.value; + } + + static function viaArray(d:Dynamic):Int { + var a:Array = [0]; + a[0] = d; + return a[0]; + } + + static function viaReturn(d:Dynamic):Int + return d; + + // Each way of taking the Dynamic as an Int, which all give the same + static function convert(s:String):Int { + var d:Dynamic = s; + var all = [viaLocal(d), viaArgument(d), viaField(d), viaArray(d), viaReturn(d)]; + for (r in all) + if (r != all[0]) { + Sys.stderr().writeString('"$s" converts differently: $all\n'); + allAgree = false; + } + return all[0]; + } + + static function claim(text:String, cases:Array<{s:String, i:Int}>) { + var ok = true; + for (c in cases) { + var got = convert(c.s); + if (got != c.i) { + Sys.stderr().writeString('"${c.s}" gives $got, not ${c.i}\n'); + ok = false; + } + } + Sys.println('$text: $ok'); + } + + public static function run() { + allAgree = true; + claim("a number in the Int range converts as written", [ + {s: "0", i: 0}, {s: "12", i: 12}, {s: "-7", i: -7}, {s: "007", i: 7}, + {s: "2147483647", i: 2147483647}, {s: "-2147483648", i: -2147483647 - 1} + ]); + claim("spaces, a sign and text after the number are read as before", [ + {s: " 42", i: 42}, {s: "\t\n\r\x0B\x0C9", i: 9}, {s: "+5", i: 5}, {s: "12abc", i: 12}, + {s: "-3.75", i: -3}, {s: "1e3", i: 1}, {s: "- 4", i: 0}, {s: "+-4", i: 0} + ]); + claim("a String with no number converts to 0", [ + {s: "", i: 0}, {s: " ", i: 0}, {s: "abc", i: 0}, {s: "-", i: 0}, {s: "x12", i: 0} + ]); + claim("only decimal digits are read", [{s: "0x1F", i: 0}, {s: "0X10", i: 0}, {s: "1_000", i: 1}]); + claim("a number past an Int saturates", [ + {s: "2147483648", i: 2147483647}, {s: "-2147483649", i: -2147483647 - 1}, + {s: "4294967296", i: 2147483647}, {s: "4294967297", i: 2147483647}, + {s: "10000000000", i: 2147483647}, {s: "-10000000000", i: -2147483647 - 1}, + {s: "21474836470", i: 2147483647}, {s: "9223372036854775808", i: 2147483647}, + {s: "99999999999999999999999999", i: 2147483647}, {s: "-99999999999999999999999999", i: -2147483647 - 1}, + {s: " +3000000000xyz", i: 2147483647} + ]); + claim("leading zeros do not count toward the range", [ + {s: "0000000000002147483647", i: 2147483647}, {s: "-000000000002147483648", i: -2147483647 - 1}, + {s: "00000000000000000000000012", i: 12}, {s: "00000000000000000000000000", i: 0} + ]); + var big:Float = 1e10; + var huge = haxe.Int64.make(1, 0); + var max = 2147483647; + claim("a String made from a number past an Int saturates", [ + {s: big + "null", i: 2147483647}, {s: "-" + big + "x", i: -2147483647 - 1}, + {s: Std.string(huge) + "x", i: 2147483647}, {s: "12" + max, i: 2147483647} + ]); + claim("a UTF-16 String's number is read", [ + {s: "123é", i: 123}, {s: "-45€", i: -45}, {s: "é1", i: 0}, + {s: "12", i: 0}, {s: "99999999999é", i: 2147483647} + ]); + Sys.println('every way of taking a Dynamic as an Int agrees: $allAgree'); + } +} diff --git a/test/regression/DynamicStringToInt/Script.hx b/test/regression/DynamicStringToInt/Script.hx new file mode 100644 index 00000000..52a4d56a --- /dev/null +++ b/test/regression/DynamicStringToInt/Script.hx @@ -0,0 +1,103 @@ +// The cases of Main.hx's Compiled, run as a script with the JIT off and on +class ScriptHolder { + public var value:Int = 0; + + public function new() {} +} + +class Script { + static var allAgree = true; + + static function viaLocal(d:Dynamic):Int { + var i:Int = d; + return i; + } + + static function takeInt(i:Int):Int + return i; + + static function viaArgument(d:Dynamic):Int + return takeInt(d); + + static function viaField(d:Dynamic):Int { + var h = new ScriptHolder(); + h.value = d; + return h.value; + } + + static function viaArray(d:Dynamic):Int { + var a:Array = [0]; + a[0] = d; + return a[0]; + } + + static function viaReturn(d:Dynamic):Int + return d; + + // Each way of taking the Dynamic as an Int, which all give the same + static function convert(s:String):Int { + var d:Dynamic = s; + var all = [viaLocal(d), viaArgument(d), viaField(d), viaArray(d), viaReturn(d)]; + for (r in all) + if (r != all[0]) { + Sys.stderr().writeString('"$s" converts differently: $all\n'); + allAgree = false; + } + return all[0]; + } + + static function claim(text:String, cases:Array<{s:String, i:Int}>) { + var ok = true; + for (c in cases) { + var got = convert(c.s); + if (got != c.i) { + Sys.stderr().writeString('"${c.s}" gives $got, not ${c.i}\n'); + ok = false; + } + } + Sys.println('$text: $ok'); + } + + static function main() + run(); + + public static function run() { + allAgree = true; + claim("a number in the Int range converts as written", [ + {s: "0", i: 0}, {s: "12", i: 12}, {s: "-7", i: -7}, {s: "007", i: 7}, + {s: "2147483647", i: 2147483647}, {s: "-2147483648", i: -2147483647 - 1} + ]); + claim("spaces, a sign and text after the number are read as before", [ + {s: " 42", i: 42}, {s: "\t\n\r\x0B\x0C9", i: 9}, {s: "+5", i: 5}, {s: "12abc", i: 12}, + {s: "-3.75", i: -3}, {s: "1e3", i: 1}, {s: "- 4", i: 0}, {s: "+-4", i: 0} + ]); + claim("a String with no number converts to 0", [ + {s: "", i: 0}, {s: " ", i: 0}, {s: "abc", i: 0}, {s: "-", i: 0}, {s: "x12", i: 0} + ]); + claim("only decimal digits are read", [{s: "0x1F", i: 0}, {s: "0X10", i: 0}, {s: "1_000", i: 1}]); + claim("a number past an Int saturates", [ + {s: "2147483648", i: 2147483647}, {s: "-2147483649", i: -2147483647 - 1}, + {s: "4294967296", i: 2147483647}, {s: "4294967297", i: 2147483647}, + {s: "10000000000", i: 2147483647}, {s: "-10000000000", i: -2147483647 - 1}, + {s: "21474836470", i: 2147483647}, {s: "9223372036854775808", i: 2147483647}, + {s: "99999999999999999999999999", i: 2147483647}, {s: "-99999999999999999999999999", i: -2147483647 - 1}, + {s: " +3000000000xyz", i: 2147483647} + ]); + claim("leading zeros do not count toward the range", [ + {s: "0000000000002147483647", i: 2147483647}, {s: "-000000000002147483648", i: -2147483647 - 1}, + {s: "00000000000000000000000012", i: 12}, {s: "00000000000000000000000000", i: 0} + ]); + var big:Float = 1e10; + var huge = haxe.Int64.make(1, 0); + var max = 2147483647; + claim("a String made from a number past an Int saturates", [ + {s: big + "null", i: 2147483647}, {s: "-" + big + "x", i: -2147483647 - 1}, + {s: Std.string(huge) + "x", i: 2147483647}, {s: "12" + max, i: 2147483647} + ]); + claim("a UTF-16 String's number is read", [ + {s: "123é", i: 123}, {s: "-45€", i: -45}, {s: "é1", i: 0}, + {s: "12", i: 0}, {s: "99999999999é", i: 2147483647} + ]); + Sys.println('every way of taking a Dynamic as an Int agrees: $allAgree'); + } +} diff --git a/test/regression/DynamicStringToInt/build.hxml b/test/regression/DynamicStringToInt/build.hxml new file mode 100644 index 00000000..d20d36bd --- /dev/null +++ b/test/regression/DynamicStringToInt/build.hxml @@ -0,0 +1,7 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no diff --git a/test/regression/DynamicStringToInt/stdout.txt b/test/regression/DynamicStringToInt/stdout.txt new file mode 100644 index 00000000..d075d55d --- /dev/null +++ b/test/regression/DynamicStringToInt/stdout.txt @@ -0,0 +1,30 @@ +-- compiled +a number in the Int range converts as written: true +spaces, a sign and text after the number are read as before: true +a String with no number converts to 0: true +only decimal digits are read: true +a number past an Int saturates: true +leading zeros do not count toward the range: true +a String made from a number past an Int saturates: true +a UTF-16 String's number is read: true +every way of taking a Dynamic as an Int agrees: true +-- script, JIT off +a number in the Int range converts as written: true +spaces, a sign and text after the number are read as before: true +a String with no number converts to 0: true +only decimal digits are read: true +a number past an Int saturates: true +leading zeros do not count toward the range: true +a String made from a number past an Int saturates: true +a UTF-16 String's number is read: true +every way of taking a Dynamic as an Int agrees: true +-- script, JIT on +a number in the Int range converts as written: true +spaces, a sign and text after the number are read as before: true +a String with no number converts to 0: true +only decimal digits are read: true +a number past an Int saturates: true +leading zeros do not count toward the range: true +a String made from a number past an Int saturates: true +a UTF-16 String's number is read: true +every way of taking a Dynamic as an Int agrees: true