Skip to content

When parsing floating-point from String, underflow to 0, overflow to ∞ #34339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions stdlib/public/SwiftShims/RuntimeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@ SWIFT_RUNTIME_STDLIB_API
void *_swift_objCMirrorSummary(const void * nsObject);

/// Call strtold_l with the C locale, swapping argument and return
/// types so we can operate on Float80. Return NULL on overflow.
/// types so we can operate on Float80.
SWIFT_RUNTIME_STDLIB_API
const char *_swift_stdlib_strtold_clocale(const char *nptr, void *outResult);
/// Call strtod_l with the C locale, swapping argument and return
/// types so we can operate consistently on Float80. Return NULL on
/// overflow.
/// types so we can operate consistently on Float80.
SWIFT_RUNTIME_STDLIB_API
const char *_swift_stdlib_strtod_clocale(const char *nptr, double *outResult);
/// Call strtof_l with the C locale, swapping argument and return
/// types so we can operate consistently on Float80. Return NULL on
/// overflow.
/// types so we can operate consistently on Float80.
SWIFT_RUNTIME_STDLIB_API
const char *_swift_stdlib_strtof_clocale(const char *nptr, float *outResult);
/// Call strtof_l with the C locale, swapping argument and return
/// types so we can operate consistently on Float80. Return NULL on
/// overflow.
/// types so we can operate consistently on Float80.
SWIFT_RUNTIME_STDLIB_API
const char *_swift_stdlib_strtof16_clocale(const char *nptr, __fp16 *outResult);

Expand Down
4 changes: 0 additions & 4 deletions stdlib/public/stubs/Stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,6 @@ static const char *_swift_stdlib_strtoX_clocale_impl(
_swift_set_errno(0);
const auto result = posixImpl(nptr, &EndPtr, getCLocale());
*outResult = result;
if (result == huge || result == -huge || result == 0.0 || result == -0.0) {
if (errno == ERANGE)
EndPtr = nullptr;
}
return EndPtr;
}

Expand Down
16 changes: 5 additions & 11 deletions test/stdlib/NumericParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,11 @@ tests.test("${Self}/Basics") {
expectNil(${Self}("0 ")) // Trailing whitespace
expectNil(${Self}("\u{1D7FF}")) // MATHEMATICAL MONOSPACE DIGIT NINE

// Overflow and underflow. Interleave with other checks to make
// sure we're not abusing errno
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("2e99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("2e-99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("-2e99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("-2e-99999999999999"))
expectEqual(0.0, ${Self}("0"))
// Overflow to infinity, underflow to zero.
expectEqual(.infinity, ${Self}("2e99999999999999"))
expectEqual(0.0, ${Self}("2e-99999999999999"))
expectEqual(-.infinity, ${Self}("-2e99999999999999"))
expectEqual(0.0, ${Self}("-2e-99999999999999"))
}

% if Self == 'Float80':
Expand Down