Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
KeyFile::get_string() can return a string in error case that still ha…
Browse files Browse the repository at this point in the history
…s to be freed

If it's an invalid UTF-8 string for example we still get it returned but
should simply free it. We can't return it as a String in any way.

Fixes #543
  • Loading branch information
sdroege committed Nov 24, 2019
1 parent 547887f commit 22c0eae
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 90 deletions.
16 changes: 16 additions & 0 deletions Gir.toml
Expand Up @@ -447,6 +447,22 @@ status = "generate"
name = "get_boolean_list"
#boolean array needs to be converted to Vec<bool>
ignore = true
[[object.function]]
name = "get_string"
# can return an error but still a value to be freed
ignore = true
[[object.function]]
name = "get_string_list"
# can return an error but still a value to be freed
ignore = true
[[object.function]]
name = "get_locale_string"
# can return an error but still a value to be freed
ignore = true
[[object.function]]
name = "get_locale_string_list"
# can return an error but still a value to be freed
ignore = true

[[object]]
name = "GLib.DateTime"
Expand Down
90 changes: 0 additions & 90 deletions src/auto/key_file.rs
Expand Up @@ -190,100 +190,10 @@ impl KeyFile {
}
}

pub fn get_locale_string(
&self,
group_name: &str,
key: &str,
locale: Option<&str>,
) -> Result<GString, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_locale_string(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
locale.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}

pub fn get_locale_string_list(
&self,
group_name: &str,
key: &str,
locale: Option<&str>,
) -> Result<Vec<GString>, Error> {
unsafe {
let mut length = mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_locale_string_list(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
locale.to_glib_none().0,
length.as_mut_ptr(),
&mut error,
);
if error.is_null() {
Ok(FromGlibContainer::from_glib_full_num(
ret,
length.assume_init() as usize,
))
} else {
Err(from_glib_full(error))
}
}
}

pub fn get_start_group(&self) -> Option<GString> {
unsafe { from_glib_full(glib_sys::g_key_file_get_start_group(self.to_glib_none().0)) }
}

pub fn get_string(&self, group_name: &str, key: &str) -> Result<GString, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_string(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}

pub fn get_string_list(&self, group_name: &str, key: &str) -> Result<Vec<GString>, Error> {
unsafe {
let mut length = mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_string_list(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
length.as_mut_ptr(),
&mut error,
);
if error.is_null() {
Ok(FromGlibContainer::from_glib_full_num(
ret,
length.assume_init() as usize,
))
} else {
Err(from_glib_full(error))
}
}
}

pub fn get_uint64(&self, group_name: &str, key: &str) -> Result<u64, Error> {
unsafe {
let mut error = ptr::null_mut();
Expand Down
102 changes: 102 additions & 0 deletions src/key_file.rs
Expand Up @@ -149,4 +149,106 @@ impl KeyFile {
))
}
}

pub fn get_string(&self, group_name: &str, key: &str) -> Result<GString, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_string(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
if !ret.is_null() {
glib_sys::g_free(ret as *mut _);
}
Err(from_glib_full(error))
}
}
}

pub fn get_string_list(&self, group_name: &str, key: &str) -> Result<Vec<GString>, Error> {
unsafe {
let mut length = mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_string_list(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
length.as_mut_ptr(),
&mut error,
);
if error.is_null() {
Ok(FromGlibContainer::from_glib_full_num(
ret,
length.assume_init() as usize,
))
} else {
if !ret.is_null() {
glib_sys::g_strfreev(ret);
}
Err(from_glib_full(error))
}
}
}

pub fn get_locale_string(
&self,
group_name: &str,
key: &str,
locale: Option<&str>,
) -> Result<GString, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_locale_string(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
locale.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
if !ret.is_null() {
glib_sys::g_free(ret as *mut _);
}
Err(from_glib_full(error))
}
}
}

pub fn get_locale_string_list(
&self,
group_name: &str,
key: &str,
locale: Option<&str>,
) -> Result<Vec<GString>, Error> {
unsafe {
let mut length = mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_locale_string_list(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
locale.to_glib_none().0,
length.as_mut_ptr(),
&mut error,
);
if error.is_null() {
Ok(FromGlibContainer::from_glib_full_num(
ret,
length.assume_init() as usize,
))
} else {
if !ret.is_null() {
glib_sys::g_strfreev(ret);
}
Err(from_glib_full(error))
}
}
}
}

0 comments on commit 22c0eae

Please sign in to comment.