Skip to content

Commit b8f4be5

Browse files
acceleratedmfontanini
authored andcommitted
Increase buffer construction requirements (#88)
* Fix crash in Buffer with null pointer and non-zero length * Throw inside Buffer constructor instead
1 parent 9a20b58 commit b8f4be5

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

Diff for: include/cppkafka/buffer.h

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <iosfwd>
3636
#include <algorithm>
3737
#include "macros.h"
38+
#include "exceptions.h"
3839

3940
namespace cppkafka {
4041

@@ -75,6 +76,9 @@ class CPPKAFKA_API Buffer {
7576
Buffer(const T* data, size_t size)
7677
: data_(reinterpret_cast<const DataType*>(data)), size_(size) {
7778
static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
79+
if ((data_ == nullptr) && (size_ > 0)) {
80+
throw Exception("Invalid buffer configuration");
81+
}
7882
}
7983

8084
/**

Diff for: tests/buffer_test.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ TEST_CASE("conversions", "[buffer]") {
1515
const Buffer buffer(data);
1616
const Buffer empty_buffer;
1717

18+
SECTION("construction") {
19+
CHECK_THROWS_AS(Buffer((const char*)nullptr, 5), Exception);
20+
}
1821

1922
SECTION("bool conversion") {
2023
CHECK(!!buffer == true);

0 commit comments

Comments
 (0)