Skip to content

Commit

Permalink
[libc] Fix strtod hex exponent overflow bug
Browse files Browse the repository at this point in the history
Same issue as was fixed in commit 3d95323, but for hexadecimal floats.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D148545
  • Loading branch information
michaelrj-google committed Apr 19, 2023
1 parent 9a579d6 commit b5f0a87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 4 additions & 5 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,12 @@ hexadecimal_string_to_float(const char *__restrict src,

// If the result is in the valid range, then we use it. The valid range is
// also within the int32 range, so this prevents overflow issues.
if (temp_exponent < fputil::FPBits<T>::MAX_EXPONENT &&
temp_exponent > -fputil::FPBits<T>::MAX_EXPONENT) {
exponent = static_cast<int32_t>(temp_exponent);
} else if (temp_exponent > fputil::FPBits<T>::MAX_EXPONENT) {
if (temp_exponent > fputil::FPBits<T>::MAX_EXPONENT) {
exponent = fputil::FPBits<T>::MAX_EXPONENT;
} else {
} else if (temp_exponent < -fputil::FPBits<T>::MAX_EXPONENT) {
exponent = -fputil::FPBits<T>::MAX_EXPONENT;
} else {
exponent = static_cast<int32_t>(temp_exponent);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions libc/test/src/stdlib/strtod_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ TEST_F(LlvmLibcStrToDTest, FuzzFailures) {
"200000000000000000E608",
1462, uint64_t(0x7ff0000000000000), ERANGE);

// Same as above but for hex.
run_test("0x0164810157p2047", 17, uint64_t(0x7ff0000000000000), ERANGE);

// This bug was in the handling of very large exponents in the exponent
// marker. Previously anything greater than 10,000 would be set to 10,000.
// This caused incorrect behavior if there were more than 10,000 '0's in the
Expand Down

0 comments on commit b5f0a87

Please sign in to comment.