Skip to content

Commit 147d631

Browse files
derekmaurocopybara-github
authored andcommitted
Use absl::StripLeadingAsciiWhitespace instead of a lambda using std::isspace
As written the lambda doesn't correctly handle sign-extension. Closes #2061 PiperOrigin-RevId: 921439321 Change-Id: I952e9f29c957546e91d3c91d9df0bbc45b7a2888
1 parent 635c53f commit 147d631

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

absl/time/format.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <cctype>
1818
#include <cstdint>
1919
#include <utility>
20-
20+
#include "absl/strings/ascii.h"
2121
#include "absl/strings/match.h"
2222
#include "absl/strings/string_view.h"
2323
#include "absl/time/internal/cctz/include/cctz/time_zone.h"
@@ -99,13 +99,6 @@ bool ParseTime(absl::string_view format, absl::string_view input,
9999
// the fields with respect to the given TimeZone.
100100
bool ParseTime(absl::string_view format, absl::string_view input,
101101
absl::TimeZone tz, absl::Time* time, std::string* err) {
102-
auto strip_leading_space = [](absl::string_view* sv) {
103-
while (!sv->empty()) {
104-
if (!std::isspace(sv->front())) return;
105-
sv->remove_prefix(1);
106-
}
107-
};
108-
109102
// Portable toolchains means we don't get nice constexpr here.
110103
struct Literal {
111104
const char* name;
@@ -116,12 +109,12 @@ bool ParseTime(absl::string_view format, absl::string_view input,
116109
{kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
117110
{kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
118111
};
119-
strip_leading_space(&input);
112+
input = StripLeadingAsciiWhitespace(input);
120113
for (const auto& lit : literals) {
121114
if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
122115
absl::string_view tail = input;
123116
tail.remove_prefix(lit.size);
124-
strip_leading_space(&tail);
117+
tail = StripLeadingAsciiWhitespace(tail);
125118
if (tail.empty()) {
126119
*time = lit.value;
127120
return true;

absl/time/format_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ TEST(ParseTime, ErrorCases) {
250250
EXPECT_FALSE(
251251
absl::ParseTime("%Y", std::string("2026\0payload", 12), &t, &err));
252252
EXPECT_THAT(err, HasSubstr("Illegal trailing data"));
253+
254+
// High-bit character test for sign-extension bugs.
255+
for (int i = 128; i < 256; ++i) {
256+
char c = static_cast<char>(i);
257+
std::string input = std::string(1, c) + "2015-01-02";
258+
EXPECT_FALSE(absl::ParseTime("%Y-%m-%d", input, &t, &err));
259+
}
253260
}
254261

255262
TEST(ParseTime, ExtendedSeconds) {

0 commit comments

Comments
 (0)