Skip to content

Commit 8567460

Browse files
authored
access_log: support beginning of epoch in START_TIME. (envoyproxy#4254)
This was confusing the fuzz tests, fixes oss-fuzz issue https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=9988. Risk level: Low Testing: Unit test and corpus entry added. Signed-off-by: Harvey Tuch <[email protected]>
1 parent 28d5f41 commit 8567460

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

source/common/common/utility.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ std::string DateFormatter::fromTime(const SystemTime& time) const {
9898
// Copy the current cached formatted format string, then replace its subseconds part (when it has
9999
// non-zero width) by correcting its position using prepared subseconds offsets.
100100
std::string formatted_str = formatted.str;
101-
const std::string nanoseconds = fmt::FormatInt(epoch_time_ns.count()).str();
101+
std::string nanoseconds = fmt::FormatInt(epoch_time_ns.count()).str();
102+
// Special case handling for beginning of time, we should never need to do this outside of
103+
// tests or a time machine.
104+
if (nanoseconds.size() < 10) {
105+
nanoseconds = std::string(10 - nanoseconds.size(), '0') + nanoseconds;
106+
}
107+
102108
for (size_t i = 0; i < specifiers_.size(); ++i) {
103109
const auto& specifier = specifiers_.at(i);
104110

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
format: "%START_TIME(%f)%"

test/common/access_log/access_log_formatter_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,20 @@ TEST(AccessLogFormatterTest, CompositeFormatterSuccess) {
434434
formatter.format(request_header, response_header, response_trailer, request_info));
435435
}
436436

437+
{
438+
// This tests the beginning of time.
439+
const std::string format = "%START_TIME(%Y/%m/%d)%|%START_TIME(%s)%|%START_TIME(bad_format)%|"
440+
"%START_TIME%|%START_TIME(%f.%1f.%2f.%3f)%";
441+
442+
const time_t test_epoch = 0;
443+
const SystemTime time = std::chrono::system_clock::from_time_t(test_epoch);
444+
EXPECT_CALL(request_info, startTime()).WillRepeatedly(Return(time));
445+
FormatterImpl formatter(format);
446+
447+
EXPECT_EQ("1970/01/01|0|bad_format|1970-01-01T00:00:00.000Z|000000000.0.00.000",
448+
formatter.format(request_header, response_header, response_trailer, request_info));
449+
}
450+
437451
{
438452
// This tests multiple START_TIMEs.
439453
const std::string format =

0 commit comments

Comments
 (0)