From eaea8c2ab079b09be88787567b2514f1c3dcdbb8 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 19 Oct 2023 21:00:35 -0700 Subject: [PATCH] Buffer: add explicit/defensive coding checks. --- src/sl/Buffer.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/sl/Buffer.cc b/src/sl/Buffer.cc index 6b5bd4f..fd907ef 100644 --- a/src/sl/Buffer.cc +++ b/src/sl/Buffer.cc @@ -124,7 +124,15 @@ Buffer::Append(const uint8_t *data, const size_t datalen) resized = true; } + // If newCap is > 0, memory will be allocated for this-> + // contents, and if contents was null, then Resize should force + // it to be allocated. Still, a little defensive coding never + // hurt. assert(this->contents != nullptr); + if (this->contents == nullptr) { + return false; + } + memcpy(this->contents + this->length, data, datalen); this->length += datalen; return resized;