Skip to content

Saturate a String's number past an Int when a Dynamic is taken as an Int - #514

Open
dimensionscape wants to merge 1 commit into
mainfrom
fix/dynamic-string-to-int
Open

dimensionscape wants to merge 1 commit into
mainfrom
fix/dynamic-string-to-int

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

A String in a Dynamic, taken as an Int, went through the C library's atoi, whose result for a number past the Int range is undefined. The C libraries differ there: MSVC's saturates, while glibc's and Apple's read into a 64-bit long and truncate it. So "10000000000" gave 2147483647 on Windows and 1410065408 on Linux and macOS, and "4294967296" gave 0. Compiled code, the cppia interpreter and the JIT all reach the same StringData::__ToInt, so each platform agreed with itself and not with the others. It showed up in #500, where CppiaJitDynamicNumbers printed different values on CI's Linux and macOS jobs than on Windows.

The conversion now reads what atoi reads: the spaces isspace has in the C locale (whatever the process locale is), a sign and decimal digits, and 0 when there are none. A number past the range saturates to it on every platform, as a decimal number already does in Std.parseInt. Leading zeros do not count toward the range. Every number in the range and every String with no number converts as before, so the only results that change are out-of-range ones, and only off Windows.

Tests

New DynamicStringToInt takes Strings as Ints five ways (a local, an argument, a field, an array element and a return), in compiled code and as a script with the JIT off and on, and checks they agree. Its cases cover numbers in the range, atoi's spaces, signs and trailing text, no hexadecimal, numbers past the range either way, leading zeros, Strings made from large numbers, and UTF-16 Strings.

On Windows its cases passed before this as well, since MSVC's atoi saturates. Breaking the new conversion on purpose four ways each fails it: wrapping through a 64-bit integer as glibc does, accumulating without the guard past the range, skipping only plain spaces, and saturating negatives one short.

On Windows: DynamicStringToInt, test/haxe (2,880) and test/std (696). Other platforms are left to CI.

A String in a Dynamic, taken as an Int (var i:Int = value, an Int
argument, field or array element, or an Int return), converted through
the C library's atoi, whose result for a number past the Int range is
undefined. The C libraries differ there: MSVC's saturates, while
glibc's and Apple's read into a 64-bit long and truncate it, so
"10000000000" gave 2147483647 on Windows and 1410065408 on Linux and
macOS, and "4294967296" gave 0. Compiled code, the cppia interpreter
and the JIT all reach the same conversion, so each platform agreed with
itself and not with the others.

The conversion reads what atoi reads, the spaces isspace has in the C
locale, a sign and decimal digits, and gives 0 when there are none, so
every number in the range and every String with no number converts as
before. A number past the range saturates to it now, on every platform,
as a decimal number already does in Std.parseInt. Leading zeros do not
count toward the range.

DynamicStringToInt takes Strings as Ints five ways, as a local, an
argument, a field, an array element and a return, in compiled code and
as a script with the JIT off and on, and checks they agree: numbers in
the range, atoi's spaces, signs and trailing text, no hexadecimal,
numbers past the range either way, leading zeros, Strings made from
large numbers, and UTF-16 Strings. On Windows its cases passed before
this as well, since MSVC's atoi saturates; CI on Linux and macOS showed
the wrapped values in libhxcpp #500's CppiaJitDynamicNumbers.

Tests: DynamicStringToInt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant