Skip to content

Commit

Permalink
access_log: support beginning of epoch in START_TIME. (envoyproxy#4254)
Browse files Browse the repository at this point in the history
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 <htuch@google.com>
  • Loading branch information
htuch authored Aug 26, 2018
1 parent 28d5f41 commit 8567460
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion source/common/common/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ std::string DateFormatter::fromTime(const SystemTime& time) const {
// Copy the current cached formatted format string, then replace its subseconds part (when it has
// non-zero width) by correcting its position using prepared subseconds offsets.
std::string formatted_str = formatted.str;
const std::string nanoseconds = fmt::FormatInt(epoch_time_ns.count()).str();
std::string nanoseconds = fmt::FormatInt(epoch_time_ns.count()).str();
// Special case handling for beginning of time, we should never need to do this outside of
// tests or a time machine.
if (nanoseconds.size() < 10) {
nanoseconds = std::string(10 - nanoseconds.size(), '0') + nanoseconds;
}

for (size_t i = 0; i < specifiers_.size(); ++i) {
const auto& specifier = specifiers_.at(i);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
format: "%START_TIME(%f)%"
14 changes: 14 additions & 0 deletions test/common/access_log/access_log_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ TEST(AccessLogFormatterTest, CompositeFormatterSuccess) {
formatter.format(request_header, response_header, response_trailer, request_info));
}

{
// This tests the beginning of time.
const std::string format = "%START_TIME(%Y/%m/%d)%|%START_TIME(%s)%|%START_TIME(bad_format)%|"
"%START_TIME%|%START_TIME(%f.%1f.%2f.%3f)%";

const time_t test_epoch = 0;
const SystemTime time = std::chrono::system_clock::from_time_t(test_epoch);
EXPECT_CALL(request_info, startTime()).WillRepeatedly(Return(time));
FormatterImpl formatter(format);

EXPECT_EQ("1970/01/01|0|bad_format|1970-01-01T00:00:00.000Z|000000000.0.00.000",
formatter.format(request_header, response_header, response_trailer, request_info));
}

{
// This tests multiple START_TIMEs.
const std::string format =
Expand Down

0 comments on commit 8567460

Please sign in to comment.