From 671cae2c7c1dee7feebb991670ea1c49f218feea Mon Sep 17 00:00:00 2001 From: Jonas Olsson Date: Thu, 14 Jun 2018 09:57:39 +0200 Subject: [PATCH] Handle FileRotatingStreams with long file names Bug: webrtc:9392 Change-Id: I7b42b1a6ed1b646c244bc64f1bad92a2f38e5539 Reviewed-on: https://webrtc-review.googlesource.com/83162 Commit-Queue: Jonas Olsson Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#23608} --- rtc_base/filerotatingstream.cc | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/rtc_base/filerotatingstream.cc b/rtc_base/filerotatingstream.cc index dcb18eec93..7108d9d281 100644 --- a/rtc_base/filerotatingstream.cc +++ b/rtc_base/filerotatingstream.cc @@ -327,22 +327,15 @@ std::vector FileRotatingStream::GetFilesWithPrefix() const { std::string FileRotatingStream::GetFilePath(size_t index, size_t num_files) const { RTC_DCHECK_LT(index, num_files); - char buf[1024]; - rtc::SimpleStringBuilder file_name(buf); - // The format will be "_%zu". We want to zero pad the index so - // that it will sort nicely. - size_t max_digits = ((num_files - 1) / 10) + 1; - size_t num_digits = (index / 10) + 1; - RTC_DCHECK_LE(num_digits, max_digits); - size_t padding = max_digits - num_digits; - file_name << file_prefix_ << "_"; - for (size_t i = 0; i < padding; ++i) { - file_name << "0"; - } - file_name << index; + const size_t buffer_size = 32; + char file_postfix[buffer_size]; + // We want to zero pad the index so that it will sort nicely. + const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1); + RTC_DCHECK_LT(1 + max_digits, buffer_size); + std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index); - Pathname file_path(dir_path_, file_name.str()); + Pathname file_path(dir_path_, file_prefix_ + file_postfix); return file_path.pathname(); }