From ec66427904188875cdda2f72a64e3e6769d55f8b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Jun 2013 19:36:53 -0700 Subject: [PATCH] Cut down on CHECK() and FLOG() in Buffer. Also add a buffer copy (would rather not read into the wrong buffer to start, but it happens...) --- base/buffer.cpp | 28 +++++++++++++++++++++------- base/buffer.h | 5 +++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/base/buffer.cpp b/base/buffer.cpp index f618727c02..38900e0017 100644 --- a/base/buffer.cpp +++ b/base/buffer.cpp @@ -36,6 +36,12 @@ void Buffer::Append(const char *str) { memcpy(dest, str, len); } +void Buffer::Append(const Buffer &other) { + size_t len = other.size(); + char *dest = Append(len); + memcpy(dest, &other.data_[0], len); +} + void Buffer::AppendValue(int value) { char buf[16]; // This is slow. @@ -44,8 +50,11 @@ void Buffer::AppendValue(int value) { } void Buffer::Take(size_t length, std::string *dest) { - CHECK_LE(length, data_.size()); - dest->resize(length); + if (length > data_.size()) { + ELOG("Truncating length in Buffer::Take()"); + length = data_.size(); + } + dest->resize(length); if (length > 0) { memcpy(&(*dest)[0], &data_[0], length); data_.erase(data_.begin(), data_.begin() + length); @@ -64,7 +73,11 @@ int Buffer::TakeLineCRLF(std::string *dest) { } void Buffer::Skip(size_t length) { - data_.erase(data_.begin(), data_.begin() + length); + if (length > data_.size()) { + ELOG("Truncating length in Buffer::Skip()"); + length = data_.size(); + } + data_.erase(data_.begin(), data_.begin() + length); } int Buffer::SkipLineCRLF() { @@ -87,18 +100,19 @@ int Buffer::OffsetToAfterNextCRLF() { } void Buffer::Printf(const char *fmt, ...) { - char buffer[512]; + char buffer[2048]; va_list vl; va_start(vl, fmt); ssize_t retval = vsnprintf(buffer, sizeof(buffer), fmt, vl); if (retval >= (ssize_t)sizeof(buffer)) { // Output was truncated. TODO: Do something. - FLOG("Buffer::Printf truncated output"); + ELOG("Buffer::Printf truncated output"); + } + if (retval < 0) { + ELOG("Buffer::Printf failed"); } - CHECK_GE(retval, 0); va_end(vl); char *ptr = Append(retval); - CHECK(ptr); memcpy(ptr, buffer, retval); } diff --git a/base/buffer.h b/base/buffer.h index ff6fa9e1ab..0ebf7e3f2b 100644 --- a/base/buffer.h +++ b/base/buffer.h @@ -19,9 +19,10 @@ class Buffer { char *Append(ssize_t length); char *Append(size_t length) { return Append((ssize_t)length); } - // These work pretty much like you'd expect. + // These work pretty much like you'd expect. void Append(const char *str); // str null-terminated. The null is not copied. - void Append(const std::string &str); + void Append(const std::string &str); + void Append(const Buffer &other); // Various types. Useful for varz etc. Appends a string representation of the // value, rather than a binary representation.