Skip to content

Commit 46b7a50

Browse files
committed
Fix copy construction and assignment of Pieces
Pointers stored in Pieces::pieces_vector refer to objects with lifetimes tied to Pieces::handle. gcc/rust/ChangeLog: * ast/rust-fmt.cc (Pieces::Pieces): Reconstruct pieces_vector when copying, remove move constructor. (Pieces::operator=): Reconstruct pieces_vector when copying. * ast/rust-fmt.h (Pieces::Pieces): Remove move constructor. Signed-off-by: Owen Avery <[email protected]>
1 parent 5b7a0d7 commit 46b7a50

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

gcc/rust/ast/rust-fmt.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,26 @@ Pieces::collect (const std::string &to_parse, bool append_newline,
4949

5050
Pieces::~Pieces () { ffi::destroy_pieces (handle); }
5151

52-
Pieces::Pieces (const Pieces &other) : pieces_vector (other.pieces_vector)
52+
Pieces::Pieces (const Pieces &other) : handle (ffi::clone_pieces (other.handle))
5353
{
54-
handle = ffi::clone_pieces (other.handle);
54+
// reconstruct
55+
pieces_vector = std::vector<ffi::Piece> (handle.piece_slice.base_ptr,
56+
handle.piece_slice.base_ptr
57+
+ handle.piece_slice.len);
5558
}
5659

5760
Pieces &
5861
Pieces::operator= (const Pieces &other)
5962
{
6063
handle = ffi::clone_pieces (other.handle);
61-
pieces_vector = other.pieces_vector;
64+
65+
// reconstruct
66+
pieces_vector = std::vector<ffi::Piece> (handle.piece_slice.base_ptr,
67+
handle.piece_slice.base_ptr
68+
+ handle.piece_slice.len);
6269

6370
return *this;
6471
}
6572

66-
Pieces::Pieces (Pieces &&other)
67-
: pieces_vector (std::move (other.pieces_vector)),
68-
handle (clone_pieces (other.handle))
69-
{}
70-
7173
} // namespace Fmt
7274
} // namespace Rust

gcc/rust/ast/rust-fmt.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ struct Pieces
286286
Pieces (const Pieces &other);
287287
Pieces &operator= (const Pieces &other);
288288

289-
Pieces (Pieces &&other);
290-
291289
const std::vector<ffi::Piece> &get_pieces () const { return pieces_vector; }
292290

293291
// {

0 commit comments

Comments
 (0)