[librsvg: 1/2] (391): Avoid undefined behavior casting opaque pointers to empty enums



commit ff9d7f19c896a461a212f435ecd4bc7283a7a3a2
Author: Jordan Petridis <jpetridis gnome org>
Date:   Thu Dec 6 15:13:44 2018 +0200

    (391): Avoid undefined behavior casting opaque pointers to empty enums
    
    Looks like enums had been the recommended way to do this kind of
    thing, in the rustbooks itself, but it can lead to UB and it was
    changed recently to reflect this.
    
    The new recommendation is a repr(c) struct with a single private
    field.
    
    Close #391

 rsvg_internals/src/drawing_ctx.rs |  5 ++++-
 rsvg_internals/src/handle.rs      | 10 ++++++++--
 rsvg_internals/src/xml.rs         |  5 ++++-
 3 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 56af9951..6cf74a74 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -95,7 +95,10 @@ impl Drop for ViewParams {
     }
 }
 
-pub enum RsvgDrawingCtx {}
+#[repr(C)]
+pub struct RsvgDrawingCtx {
+    _private: [u8; 0],
+}
 
 pub struct DrawingCtx {
     handle: *const RsvgHandle,
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 864e7dc0..967b8bec 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -24,9 +24,15 @@ use svg::Svg;
 use util::rsvg_g_warning;
 use xml::{RsvgXmlState, XmlState};
 
-pub enum RsvgHandle {}
+#[repr(C)]
+pub struct RsvgHandle {
+    _private: [u8; 0],
+}
 
-pub enum RsvgHandleRust {}
+#[repr(C)]
+pub struct RsvgHandleRust {
+    _private: [u8; 0],
+}
 
 struct Handle {
     base_url: RefCell<Option<Url>>,
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 1b6e8b8e..30f2c6e1 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -73,7 +73,10 @@ impl Context {
 }
 
 // A *const RsvgXmlState is just the type that we export to C
-pub enum RsvgXmlState {}
+#[repr(C)]
+pub struct RsvgXmlState {
+    _private: [u8; 0],
+}
 
 // This is to hold an xmlEntityPtr from libxml2; we just hold an opaque pointer
 // that is freed in impl Drop for XmlState


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]