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

KeyFile::get_string() leaks memory on invalid UTF8 #543

Closed
sdroege opened this issue Nov 23, 2019 · 4 comments · Fixed by #544
Closed

KeyFile::get_string() leaks memory on invalid UTF8 #543

sdroege opened this issue Nov 23, 2019 · 4 comments · Fixed by #544

Comments

@sdroege
Copy link
Member

sdroege commented Nov 23, 2019

Code in question is

glib/src/auto/key_file.rs

Lines 248 to 263 in 547887f

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))
}
}
}

If there's invalid UTF8, error will be set but we will also get the invalid string as return value and are supposed to free it.

See for example the tests in GLib: https://gitlab.gnome.org/GNOME/glib/blob/7b774398414f2bf35115603da4e3badd45ffb025/glib/tests/keyfile.c#L558-564

@sdroege sdroege mentioned this issue Nov 23, 2019
43 tasks
@sdroege
Copy link
Member Author

sdroege commented Nov 23, 2019

The same probably also applies to get_locale_string(), get_string_list() and get_locale_string_list().

@sdroege
Copy link
Member Author

sdroege commented Nov 23, 2019

Also get_value() / set_value() must not work with strings at all. The whole point of these functions is to work with plain bytes, potentially invalid UTF8. Have to work on &[u8] / Vec<u8> or similar.

@EPashkin
Copy link
Member

good catch

@sdroege
Copy link
Member Author

sdroege commented Nov 24, 2019

Actually set_value() / get_value() are still required to have valid UTF-8, so that part at least is fine.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants