From ecfe5feeaa0983b4044d12d37fde88ded4895b4c Mon Sep 17 00:00:00 2001 From: fbrouille Date: Thu, 26 Oct 2023 20:23:48 +0200 Subject: [PATCH] fix: pass str parameters as cstring to glib:::ffi Signed-off-by: fbrouille --- glib-macros/tests/dynamic_objects.rs | 8 ++++---- glib/src/subclass/mod.rs | 6 ++++-- glib/src/subclass/type_plugin.rs | 8 +++++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/glib-macros/tests/dynamic_objects.rs b/glib-macros/tests/dynamic_objects.rs index 86924b7bc2d5..9f2667737b14 100644 --- a/glib-macros/tests/dynamic_objects.rs +++ b/glib-macros/tests/dynamic_objects.rs @@ -290,6 +290,7 @@ pub mod plugin { use glib::translate::{FromGlib, IntoGlib}; use glib::ObjectType; use std::cell::Cell; + use std::ffi::CString; // impl for a dynamic interface that extends `MyStaticInterface` #[derive(Clone, Copy)] @@ -499,13 +500,12 @@ pub mod plugin { flags: glib::TypeFlags, ) -> glib::Type { let type_ = unsafe { - let mut g_type = glib::gobject_ffi::g_type_from_name( - type_name.as_ptr() as *const libc::c_char - ); + let type_name = CString::new(type_name).unwrap(); + let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr()); if g_type == glib::gobject_ffi::G_TYPE_INVALID { g_type = glib::gobject_ffi::g_type_register_dynamic( parent_type.into_glib(), - type_name.as_ptr() as *const libc::c_char, + type_name.as_ptr(), self.obj().upcast_ref::().as_ptr(), flags.into_glib(), ); diff --git a/glib/src/subclass/mod.rs b/glib/src/subclass/mod.rs index 81776a631a56..d796e45e4334 100644 --- a/glib/src/subclass/mod.rs +++ b/glib/src/subclass/mod.rs @@ -294,6 +294,7 @@ //! use glib::subclass::prelude::*; //! //! pub mod imp { +//! use std::ffi::CString; //! use super::*; //! use glib::translate::IntoGlib; //! use glib::translate::FromGlib; @@ -358,10 +359,11 @@ //! //! fn register_dynamic_type(&self, parent_type: glib::Type, type_name: &str, type_info: &glib::TypeInfo, flags: glib::TypeFlags) -> glib::Type { //! let type_ = unsafe { -//! let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr() as *const libc::c_char); +//! let type_name = CString::new(type_name).unwrap(); +//! let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr()); //! if g_type == glib::gobject_ffi::G_TYPE_INVALID { //! // register type -//! g_type = glib::gobject_ffi::g_type_register_dynamic(parent_type.into_glib(), type_name.as_ptr() as *const libc::c_char, self.obj().upcast_ref::().as_ptr(), flags.into_glib()); +//! g_type = glib::gobject_ffi::g_type_register_dynamic(parent_type.into_glib(), type_name.as_ptr(), self.obj().upcast_ref::().as_ptr(), flags.into_glib()); //! } //! glib::Type::from_glib(g_type) //! }; diff --git a/glib/src/subclass/type_plugin.rs b/glib/src/subclass/type_plugin.rs index f69f37ab8197..de4fad6de31b 100644 --- a/glib/src/subclass/type_plugin.rs +++ b/glib/src/subclass/type_plugin.rs @@ -202,6 +202,8 @@ mod tests { use super::*; mod imp { + use std::ffi::CString; + use super::*; use crate::object::ObjectType; @@ -267,13 +269,13 @@ mod tests { flags: TypeFlags, ) -> Type { let type_ = unsafe { - let mut g_type = - gobject_ffi::g_type_from_name(type_name.as_ptr() as *const libc::c_char); + let type_name = CString::new(type_name).unwrap(); + let mut g_type = gobject_ffi::g_type_from_name(type_name.as_ptr()); if g_type == gobject_ffi::G_TYPE_INVALID { // register type g_type = gobject_ffi::g_type_register_dynamic( parent_type.into_glib(), - type_name.as_ptr() as *const libc::c_char, + type_name.as_ptr(), self.obj().upcast_ref::().as_ptr(), flags.into_glib(), );