Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Changed

- The documentation now always checks tag and wire type together using `tag_and_type()`, `next(tag, type)`, or `wire_type()` instead of switching on `tag()` alone.
- `pbf_reader` and `pbf_message` can no longer be constructed from a temporary `std::string`. The reader only stores a pointer into the string, so it pointed to freed memory as soon as the temporary was destroyed. Keep the string in a variable instead.
- The packed varint iterators (`const_varint_iterator`, `const_svarint_iterator`) now only store a single pointer instead of two, which speeds up decoding by 30-150%, depending on varint length. The constructor now validates once that the byte range ends on a varint boundary and stores only the data pointer. A packed field whose last byte still has its continuation bit set (i.e. a truncated trailing varint, which is illegal) now throws `end_of_buffer_exception` when the iterator range is created (from `get_packed_*()`) rather than later during iteration.

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ macro `PROTOZERO_STRICT_API` in which case Protozero will compile without the
code used for backwards compatibilty. You will then get compile errors for
older API usages.

## Upgrading from *v1.8* to *v1.9*

* `pbf_reader` and `pbf_message` can no longer be constructed from a
temporary `std::string`, because the reader would point to freed memory.
If this doesn't compile any more, store the string in a variable that
outlives the reader.

## Upgrading from *v1.6* to *v1.7*

* The `pbf_writer` class is now a typedef for `basic_pbf_writer<std::string>`
Expand Down
3 changes: 2 additions & 1 deletion include/protozero/pbf_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class pbf_message : public pbf_reader {
* Construct a pbf_message. All arguments are forwarded to the pbf_reader
* parent class.
*/
template <typename... Args>
template <typename... Args,
std::enable_if_t<std::is_constructible<pbf_reader, Args&&...>::value, int> = 0>
pbf_message(Args&&... args) noexcept : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
pbf_reader{std::forward<Args>(args)...} {
}
Expand Down
6 changes: 6 additions & 0 deletions include/protozero/pbf_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ class pbf_reader {
m_end{data.data() + data.size()} {
}

/**
* A pbf_reader can not be constructed from a temporary std::string,
* because it would point to the string's data after it was destroyed.
*/
explicit pbf_reader(const std::string&& data) = delete;

/**
* pbf_reader can be default constructed and behaves like it has an empty
* buffer.
Expand Down
7 changes: 7 additions & 0 deletions test/unit/test_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ static_assert(movable_not_copyable<protozero::packed_field_sfixed64>::value, "pa
static_assert(movable_not_copyable<protozero::packed_field_float>::value, "packed_field_float should be nothrow movable, but not copyable");
static_assert(movable_not_copyable<protozero::packed_field_double>::value, "packed_field_double should be nothrow movable, but not copyable");

static_assert(std::is_constructible<protozero::pbf_reader, const std::string&>::value, "pbf_reader should be constructible from a std::string");
static_assert(!std::is_constructible<protozero::pbf_reader, std::string&&>::value, "pbf_reader should not be constructible from a temporary std::string");
static_assert(!std::is_constructible<protozero::pbf_reader, const std::string&&>::value, "pbf_reader should not be constructible from a temporary std::string");
static_assert(std::is_constructible<protozero::pbf_message<dummy>, const std::string&>::value, "pbf_message should be constructible from a std::string");
static_assert(!std::is_constructible<protozero::pbf_message<dummy>, std::string&&>::value, "pbf_message should not be constructible from a temporary std::string");
static_assert(!std::is_constructible<protozero::pbf_message<dummy>, const std::string&&>::value, "pbf_message should not be constructible from a temporary std::string");

TEST_CASE("default constructed pbf_reader is okay") {
protozero::pbf_reader item;

Expand Down
Loading