diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-02-21 12:27:41 +0100 |
---|---|---|
committer | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-03-30 13:52:32 +0200 |
commit | 77216f03f283f1f2e36319a68f6cf248e6d4acb3 (patch) | |
tree | 7858d54ee58300e6a33861c5d678c57386c62677 | |
parent | da81fa1f087b70ab291c87170863eab8a8e1e8d3 (diff) | |
download | gcc-77216f03f283f1f2e36319a68f6cf248e6d4acb3.zip gcc-77216f03f283f1f2e36319a68f6cf248e6d4acb3.tar.gz gcc-77216f03f283f1f2e36319a68f6cf248e6d4acb3.tar.bz2 |
libproc_macro: Custom Clone on TokenStream
Change for a custom implementation of the Clone trait since the
TokenStream structure owns a foreign array which needs to be deeply
cloned to prevent use after free in case the original TokenStream is
dropped early.
ChangeLog:
* librust/proc_macro/rust/bridge/token_stream.rs: Implement
custom Clone.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r-- | librust/proc_macro/rust/bridge/token_stream.rs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/librust/proc_macro/rust/bridge/token_stream.rs b/librust/proc_macro/rust/bridge/token_stream.rs index afd986f..1c9825f 100644 --- a/librust/proc_macro/rust/bridge/token_stream.rs +++ b/librust/proc_macro/rust/bridge/token_stream.rs @@ -14,6 +14,7 @@ extern "C" { fn TokenStream__with_capacity(capacity: u64) -> TokenStream; fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree); fn TokenStream__from_string(str: *const c_uchar, len: u64, ts: *mut TokenStream) -> bool; + fn TokenStream__clone(ts: *const TokenStream) -> TokenStream; } // TODO: There surely is a better way to achieve this. I don't like this @@ -52,7 +53,7 @@ impl From<ExternalTokenTree> for TokenTree { } #[repr(C)] -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct TokenStream { pub(crate) data: *const TokenTree, pub(crate) size: u64, @@ -151,3 +152,9 @@ impl FromStr for TokenStream { } } } + +impl Clone for TokenStream { + fn clone(&self) -> Self { + unsafe { TokenStream__clone(self as *const TokenStream) } + } +} |