Skip to content

Commit 4eac75a

Browse files
authored
Fix exception/error state when using static_buffer with buffer_adaptor (#33)
1 parent 711396d commit 4eac75a

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

include/hexi/static_buffer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ class static_buffer final {
146146
write_ += bytes;
147147
}
148148

149+
/**
150+
* @brief Resizes the buffer.
151+
*
152+
* @param size The new size of the buffer.
153+
*
154+
*/
155+
void resize(size_type size) {
156+
if(size > buffer_.size()) {
157+
throw exception("attempted to resize static_buffer to larger than capacity");
158+
}
159+
160+
write_ = size;
161+
}
162+
149163
/**
150164
* @brief Clears the container.
151165
*/

single_include/hexi.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ class buffer_adaptor final {
16891689
* @return Pointer to the location within the buffer where the next write
16901690
* will be made.
16911691
*/
1692-
auto write_ptr() const {
1692+
auto write_ptr() const {
16931693
return buffer_.data() + write_;
16941694
}
16951695

@@ -3826,6 +3826,20 @@ class static_buffer final {
38263826
write_ += bytes;
38273827
}
38283828

3829+
/**
3830+
* @brief Resizes the buffer.
3831+
*
3832+
* @param size The new size of the buffer.
3833+
*
3834+
*/
3835+
void resize(size_type size) {
3836+
if(size > buffer_.size()) {
3837+
throw exception("attempted to resize static_buffer to larger than capacity");
3838+
}
3839+
3840+
write_ = size;
3841+
}
3842+
38293843
/**
38303844
* @brief Clears the container.
38313845
*/

tests/binary_stream.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,39 @@ TEST(binary_stream, static_buffer_underrun_noexcept) {
407407
ASSERT_EQ(output, 0);
408408
}
409409

410+
TEST(binary_stream, static_buffer_adaptor_regression) {
411+
hexi::static_buffer<char, 128> buffer;
412+
hexi::buffer_adaptor adaptor(buffer);
413+
hexi::binary_stream stream(buffer);
414+
stream << 1 << 2 << 3;
415+
416+
std::string foo { "foo " };
417+
stream << foo;
418+
419+
std::string_view sv;
420+
stream.skip(sizeof(int) * 3);
421+
stream >> sv;
422+
ASSERT_TRUE(stream);
423+
}
424+
425+
TEST(binary_stream, static_buffer_adaptor_exception_regression) {
426+
hexi::static_buffer<char, 16> buffer;
427+
hexi::buffer_adaptor adaptor(buffer);
428+
hexi::binary_stream stream(buffer);
429+
std::string_view str { "This is a string that is longer than the size of the buffer..." };
430+
ASSERT_THROW(stream << str, hexi::exception);
431+
ASSERT_FALSE(stream);
432+
}
433+
434+
TEST(binary_stream, static_buffer_adaptor_no_exception_regression) {
435+
hexi::static_buffer<char, 16> buffer;
436+
hexi::buffer_adaptor adaptor(buffer);
437+
hexi::binary_stream stream(buffer, hexi::no_throw);
438+
std::string_view str { "This is a string that is longer than the size of the buffer..." };
439+
ASSERT_NO_THROW(stream << str);
440+
ASSERT_FALSE(stream);
441+
442+
}
410443
TEST(binary_stream, put_integral_literals) {
411444
hexi::static_buffer<char, 64> buffer;
412445
hexi::binary_stream stream(buffer);

0 commit comments

Comments
 (0)