Skip to content

Commit 644aedb

Browse files
committed
Update logger to prevent truncation.
1 parent 49d6735 commit 644aedb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/log.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ Log::~Log() {
100100
}
101101

102102
void Log::Write(const char *fmt, ...) {
103-
char buf[1024];
104103
va_list args;
104+
105+
va_start(args, fmt);
106+
int buf_len = vsnprintf(nullptr, 0, fmt, args);
107+
va_end(args);
108+
109+
// +1 for the trailing \0.
110+
std::vector<char> buf(buf_len + 1);
105111
va_start(args, fmt);
106-
vsnprintf(buf, sizeof(buf), fmt, args);
112+
vsnprintf(buf.data(), buf.size(), fmt, args);
107113
va_end(args);
108114

109-
const std::string line = buf;
115+
const std::string line(buf.begin(), buf.end());
110116
if (options_ & OPTIONS_IMMEDIATE) {
111117
impl_->WriteLine(line);
112118
} else {

0 commit comments

Comments
 (0)