From 3395203a535798c9e8cc8fc6fa56e36619950400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 4 Aug 2017 18:51:27 +0300 Subject: [PATCH] Make miniobject ToOwned impls actually safe Previously it was possible to create a new reference from a mutable reference, which is not good. Now a copy is always created to prevent that. --- gstreamer/src/buffer.rs | 11 +++++++++++ gstreamer/src/bufferlist.rs | 7 +++++-- gstreamer/src/caps.rs | 7 +++++-- gstreamer/src/context.rs | 7 +++++-- gstreamer/src/sample.rs | 5 ++++- gstreamer/src/tags.rs | 7 +++++-- 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 79b389f2e..825eabdeb 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -330,6 +330,17 @@ impl glib::types::StaticType for Buffer { } } +impl ToOwned for BufferRef { + type Owned = GstRc; + + fn to_owned(&self) -> GstRc { + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } + } +} + impl fmt::Debug for BufferRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", unsafe { self.as_ptr() }) diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs index 4726e897f..e30a91688 100644 --- a/gstreamer/src/bufferlist.rs +++ b/gstreamer/src/bufferlist.rs @@ -9,7 +9,7 @@ use ffi; use glib; use glib::StaticType; -use glib::translate::{from_glib, from_glib_full, from_glib_none}; +use glib::translate::{from_glib, from_glib_full}; use miniobject::*; use Buffer; @@ -77,7 +77,10 @@ impl ToOwned for BufferListRef { type Owned = GstRc; fn to_owned(&self) -> GstRc { - unsafe { from_glib_none(self.as_ptr()) } + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } } } diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index 157b9a3b6..4dfa8a839 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -15,7 +15,7 @@ use CapsIntersectMode; use glib; use ffi; -use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlib, ToGlibPtr}; +use glib::translate::{from_glib, from_glib_full, ToGlib, ToGlibPtr}; use glib::value::ToValue; #[repr(C)] @@ -348,7 +348,10 @@ impl ToOwned for CapsRef { type Owned = GstRc; fn to_owned(&self) -> GstRc { - unsafe { from_glib_none(self.as_ptr()) } + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } } } diff --git a/gstreamer/src/context.rs b/gstreamer/src/context.rs index 69bfddff2..939bbe3cc 100644 --- a/gstreamer/src/context.rs +++ b/gstreamer/src/context.rs @@ -12,7 +12,7 @@ use ffi; use glib; use glib::StaticType; -use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlib, ToGlibPtr}; +use glib::translate::{from_glib, from_glib_full, ToGlib, ToGlibPtr}; use miniobject::*; use StructureRef; @@ -80,7 +80,10 @@ impl ToOwned for ContextRef { type Owned = GstRc; fn to_owned(&self) -> GstRc { - unsafe { from_glib_none(self.as_ptr()) } + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } } } diff --git a/gstreamer/src/sample.rs b/gstreamer/src/sample.rs index cfe27fc32..7b8be58c5 100644 --- a/gstreamer/src/sample.rs +++ b/gstreamer/src/sample.rs @@ -88,7 +88,10 @@ impl ToOwned for SampleRef { type Owned = GstRc; fn to_owned(&self) -> GstRc { - unsafe { from_glib_none(self.as_ptr()) } + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } } } diff --git a/gstreamer/src/tags.rs b/gstreamer/src/tags.rs index 8683b52cc..51e0fb6e6 100644 --- a/gstreamer/src/tags.rs +++ b/gstreamer/src/tags.rs @@ -15,7 +15,7 @@ use ffi; use glib; use glib::StaticType; use glib::value::{FromValueOptional, SetValue, ToValue, TypedValue, Value}; -use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlib, ToGlibPtr, ToGlibPtrMut}; +use glib::translate::{from_glib, from_glib_full, ToGlib, ToGlibPtr, ToGlibPtrMut}; use miniobject::*; @@ -326,7 +326,10 @@ impl ToOwned for TagListRef { type Owned = GstRc; fn to_owned(&self) -> GstRc { - unsafe { from_glib_none(self.as_ptr()) } + unsafe { + from_glib_full(ffi::gst_mini_object_copy(self.as_ptr() as *const _) as + *mut _) + } } }