Fix data corruption by LogBuffer

Summary: LogBuffer::AddLogToBuffer() uses vsnprintf() in the wrong way, which might cause buffer overflow when log line is too line. Fix it.

Test Plan: Add a unit test to cover most LogBuffer's most logic.

Reviewers: igor, haobo, dhruba

Reviewed By: igor

CC: ljin, yhchiang, leveldb

Differential Revision: https://reviews.facebook.net/D17103
This commit is contained in:
sdong
2014-03-21 10:26:02 -07:00
parent c21ce14fa5
commit 83ab62e2bb
2 changed files with 80 additions and 1 deletions

View File

@@ -32,10 +32,16 @@ void LogBuffer::AddLogToBuffer(const char* format, va_list ap) {
if (p < limit) {
va_list backup_ap;
va_copy(backup_ap, ap);
p += vsnprintf(p, limit - p, format, backup_ap);
auto n = vsnprintf(p, limit - p, format, backup_ap);
assert(n >= 0);
p += n;
va_end(backup_ap);
}
if (p > limit) {
p = limit;
}
// Add '\0' to the end
*p = '\0';