public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Manual Clone for Literal
@ 2023-04-06 21:33 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:da81fa1f087b70ab291c87170863eab8a8e1e8d3

commit da81fa1f087b70ab291c87170863eab8a8e1e8d3
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Feb 21 12:24:53 2023 +0100

    libproc_macro: Manual Clone for Literal
    
    The Literal type may contain foreign byte array when representing a byte
    array or a string. This means auto deriving the Clone trait would only
    perform a shallow copy of the pointer rather than a deep copy. This
    behavior would lead to use-after-free when a literal is cloned and then
    dropped.
    
    ChangeLog:
    
            * librust/proc_macro/rust/bridge/literal.rs: Implement Clone
            manually.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 librust/proc_macro/rust/bridge/literal.rs | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/librust/proc_macro/rust/bridge/literal.rs b/librust/proc_macro/rust/bridge/literal.rs
index e883e6b14e0..32514035723 100644
--- a/librust/proc_macro/rust/bridge/literal.rs
+++ b/librust/proc_macro/rust/bridge/literal.rs
@@ -13,7 +13,7 @@ extern "C" {
 }
 
 #[repr(C)]
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Copy)]
 pub enum Unsigned {
     Unsigned8(u8),
     Unsigned16(u16),
@@ -25,7 +25,7 @@ pub enum Unsigned {
 }
 
 #[repr(C)]
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Copy)]
 pub enum Signed {
     Signed8(i8),
     Signed16(i16),
@@ -37,7 +37,7 @@ pub enum Signed {
 }
 
 #[repr(C)]
-#[derive(Debug, Clone)]
+#[derive(Debug)]
 pub enum Literal {
     /// String literal internal representation
     ///
@@ -383,3 +383,19 @@ impl FromStr for Literal {
         }
     }
 }
+
+impl Clone for Literal {
+    fn clone(&self) -> Self {
+        match self {
+            Literal::String { data, len } => unsafe { Literal__string(*data, *len) },
+            Literal::ByteString { data, size } => unsafe { Literal__byte_string(*data, *size) },
+            Literal::Char(val) => Literal::Char(*val),
+            Literal::Unsigned(val, suffixed) => Literal::Unsigned(*val, *suffixed),
+            Literal::Signed(val, suffixed) => Literal::Signed(*val, *suffixed),
+            Literal::Usize(val, suffixed) => Literal::Usize(*val, *suffixed),
+            Literal::ISize(val, suffixed) => Literal::ISize(*val, *suffixed),
+            Literal::Float32(val, suffixed) => Literal::Float32(*val, *suffixed),
+            Literal::Float64(val, suffixed) => Literal::Float64(*val, *suffixed),
+        }
+    }
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:33 [gcc/devel/rust/master] libproc_macro: Manual Clone for Literal Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).