Skip to content

Commit 0d9dff7

Browse files
Abseil Teamcopybara-github
authored andcommitted
Don't pass nullptr as the 1st arg of from_chars
`from_chars` marks them as `nonnull`, and this is the case for empty string. Existing `from_chars` implementation returns `invalid_argument` which results in `return false` from `SimpleAtod`. PiperOrigin-RevId: 820759784 Change-Id: I6635e4eaf339eb178d32f616189f7733808019f5
1 parent ab4f684 commit 0d9dff7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

absl/strings/numbers.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,15 @@ bool SimpleAtof(absl::string_view str, float* absl_nonnull out) {
8181
bool SimpleAtod(absl::string_view str, double* absl_nonnull out) {
8282
*out = 0.0;
8383
str = StripAsciiWhitespace(str);
84+
if (str.empty()) {
85+
// absl::from_chars doesn't accept empty strings.
86+
return false;
87+
}
8488
// std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one
8589
// is present, skip it, while avoiding accepting "+-0" as valid.
86-
if (!str.empty() && str[0] == '+') {
90+
if (str[0] == '+') {
8791
str.remove_prefix(1);
88-
if (!str.empty() && str[0] == '-') {
92+
if (str.empty() || str[0] == '-') {
8993
return false;
9094
}
9195
}

0 commit comments

Comments
 (0)