Skip to content

Commit 0bb0e91

Browse files
author
accelerated
committed
Fix crash in Buffer with null pointer and non-zero length
1 parent 9a20b58 commit 0bb0e91

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

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 size_ != 0;
70+
return (data_ != nullptr) && (size_ != 0);
7171
}
7272

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

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

Diff for: tests/buffer_test.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ TEST_CASE("conversions", "[buffer]") {
1414
const string data = "Hello world!";
1515
const Buffer buffer(data);
1616
const Buffer empty_buffer;
17+
const Buffer null_data((const char*)nullptr, 5);
18+
const Buffer null_size(data.c_str(), 0);
1719

1820

1921
SECTION("bool conversion") {
2022
CHECK(!!buffer == true);
2123
CHECK(!!empty_buffer == false);
24+
CHECK(!!null_data == false);
25+
CHECK(!!null_size == false);
2226
}
2327

2428
SECTION("string conversion") {
2529
CHECK(static_cast<string>(buffer) == data);
2630
CHECK(static_cast<string>(empty_buffer).empty());
31+
CHECK(static_cast<string>(null_data).empty());
32+
CHECK(static_cast<string>(null_size).empty());
2733
}
2834

2935
SECTION("vector conversion") {

0 commit comments

Comments
 (0)