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

Fix memory unsafety in FontDescription::set_family_static #186

Merged
merged 2 commits into from
May 15, 2020

Conversation

yvt
Copy link
Contributor

@yvt yvt commented May 11, 2020

It's illegal to pass the result of <str as ToGlibPtr>::to_glib_none() to a *_static function because it expires at the end of the statement.

This commit fixes a memory safety issue in this method.

`<str as ToGlibPtr>::to_glib_none` returns a null-terminated string
which only lives as long as the `Stash` it returns. This leads to
use-after-free because `pango_font_description_set_family_static`
expects the given string to outlive this `PangoFontDescription`.

Obviously, we can't simply pass the value of `str::as_ptr` because it
might not be null-terminated. What we need is `&'static CStr` - `CStr`
guarantees the memory representation of the string is null-terminated,
and `&'static` guarantees it will remain valid throughout the program's
lifetime.
use FontDescription;

impl FontDescription {
pub fn set_family_static(&mut self, family: &'static str) {
pub fn set_family_static(&mut self, family: &'static CStr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather annoying to use like that. I'd suggest removing the function completely as it doesn't bring much advantage.

@GuillaumeGomez ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so...

Copy link
Contributor Author

@yvt yvt May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's impossible to make it as ergonomic as other methods. I would have no problem with it being removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get rid of it then, if someone wants to call it they can still use the FFI functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvt do you want to update the PR?

@sdroege sdroege mentioned this pull request May 11, 2020
40 tasks
We decided to get rid of this method because it was deemed too
unergonomic to use.
@yvt yvt requested a review from sdroege May 15, 2020 07:13
Copy link
Member

@sdroege sdroege left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sdroege
Copy link
Member

sdroege commented May 15, 2020

@GuillaumeGomez All green

@GuillaumeGomez
Copy link
Member

Thanks!

@GuillaumeGomez GuillaumeGomez merged commit 8021e0f into gtk-rs:master May 15, 2020
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 this pull request may close these issues.

None yet

3 participants