Skip to content

Commit

Permalink
Initialize Bytes internal control block for all constructors.
Browse files Browse the repository at this point in the history
`Bytes` inherits constructors from its base class. We previously would
not initialize the internal control block for these constructors so some
operations involving iterators on such `Bytes` instances would have
failed, even thought the controlled object was still valid.

With this patch we always set up the control block, no matter which
constructor is used.

Closes #1390.

(cherry picked from commit 71cff50)
  • Loading branch information
bbannier committed Mar 7, 2023
1 parent 5d4ea45 commit e8112b9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions hilti/runtime/include/types/bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class Bytes : protected std::string {
*/
Bytes(std::string s, bytes::Charset cs);

Bytes(Base&& str) : Base(std::move(str)), _control(std::make_shared<Base*>(static_cast<Base*>(this))) {}
Bytes(const Bytes& xs) : Base(xs), _control(std::make_shared<Base*>(static_cast<Base*>(this))) {}
Bytes(Bytes&& xs) noexcept : Base(std::move(xs)), _control(std::make_shared<Base*>(static_cast<Base*>(this))) {}
Bytes(Base&& str) : Base(std::move(str)) {}
Bytes(const Bytes& xs) : Base(xs) {}
Bytes(Bytes&& xs) noexcept : Base(std::move(xs)) {}

/** Replaces the contents of this `Bytes` with another `Bytes`.
*
Expand Down Expand Up @@ -511,7 +511,7 @@ class Bytes : protected std::string {

private:
friend bytes::Iterator;
std::shared_ptr<Base*> _control;
std::shared_ptr<Base*> _control = std::make_shared<Base*>(static_cast<Base*>(this));

void invalidateIterators() { _control = std::make_shared<Base*>(static_cast<Base*>(this)); }
};
Expand Down
1 change: 1 addition & 0 deletions tests/Baseline/hilti.types.bytes.decode/output
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
testing
test��ng�
testüng
123�
1 change: 1 addition & 0 deletions tests/hilti/types/bytes/decode.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ hilti::print(b"testing".decode(hilti::Charset::ASCII));
hilti::print(b"testüng\n".decode(hilti::Charset::ASCII));
hilti::print(b"testüng".decode(hilti::Charset::UTF8));
assert b"testüng".decode(hilti::Charset::UTF8) == b"testüng".decode(); # Check default
hilti::print(b" 123\x01 ".strip().decode(hilti::Charset::ASCII));
}

0 comments on commit e8112b9

Please sign in to comment.