Skip to content

Commit 7f30e0d

Browse files
author
accelerated
committed
Throw inside Buffer constructor instead
1 parent 0bb0e91 commit 7f30e0d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Diff for: include/cppkafka/buffer.h

+5
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

@@ -86,6 +87,10 @@ class CPPKAFKA_API Buffer {
8687
Buffer(const std::vector<T>& data)
8788
: data_(data.data()), size_(data.size()) {
8889
static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
90+
if (((data_ == nullptr) && (size_ > 0)) ||
91+
((data_ != nullptr) && (size_ == 0))) {
92+
throw Exception("Invalid buffer configuration");
93+
}
8994
}
9095

9196
// Don't allow construction from temporary vectors

Diff for: src/buffer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ Buffer::const_iterator Buffer::end() const {
6767
}
6868

6969
Buffer::operator bool() const {
70-
return (data_ != nullptr) && (size_ != 0);
70+
return size_ != 0;
7171
}
7272

7373
Buffer::operator string() const {
74-
return (bool)(*this) ? string(data_, data_ + size_) : string();
74+
return string(data_, data_ + size_);
7575
}
7676

7777
ostream& operator<<(ostream& output, const Buffer& rhs) {

0 commit comments

Comments
 (0)