From b8f4be5e1b654b45ff518a1e67a50409a14ab0bd Mon Sep 17 00:00:00 2001 From: Alex Damian Date: Mon, 18 Jun 2018 12:09:48 -0400 Subject: [PATCH] Increase buffer construction requirements (#88) * Fix crash in Buffer with null pointer and non-zero length * Throw inside Buffer constructor instead --- include/cppkafka/buffer.h | 4 ++++ tests/buffer_test.cpp | 3 +++ 2 files changed, 7 insertions(+) diff --git a/include/cppkafka/buffer.h b/include/cppkafka/buffer.h index 43e71a7..821e182 100644 --- a/include/cppkafka/buffer.h +++ b/include/cppkafka/buffer.h @@ -35,6 +35,7 @@ #include #include #include "macros.h" +#include "exceptions.h" namespace cppkafka { @@ -75,6 +76,9 @@ public: Buffer(const T* data, size_t size) : data_(reinterpret_cast(data)), size_(size) { static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)"); + if ((data_ == nullptr) && (size_ > 0)) { + throw Exception("Invalid buffer configuration"); + } } /** diff --git a/tests/buffer_test.cpp b/tests/buffer_test.cpp index a709faf..6659d3b 100644 --- a/tests/buffer_test.cpp +++ b/tests/buffer_test.cpp @@ -15,6 +15,9 @@ TEST_CASE("conversions", "[buffer]") { const Buffer buffer(data); const Buffer empty_buffer; + SECTION("construction") { + CHECK_THROWS_AS(Buffer((const char*)nullptr, 5), Exception); + } SECTION("bool conversion") { CHECK(!!buffer == true);