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(); }