Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory corruption in locale string #181

Merged
merged 1 commit into from
Feb 25, 2019
Merged

Conversation

simmons
Copy link
Contributor

@simmons simmons commented Feb 19, 2019

The wrapper for the ncurses setlocale() function converts the provided locale &str to a C string by creating a std::ffi::CString, then passing its inner pointer on to the ncurses function. This inner pointer outlived the CString's temporary lifetime, so the data it pointed to was undefined when calling setlocale(). The fix is to bind the CString to a variable, thus extending its lifetime.

Most of the time this does not result in bad behavior, but if the memory being referred to by the pointer is overwritten before the call to setlocale(), then a garbage locale string may be used. Unable to find the locale, libc will fall back to a "C" locale, and attempts to print most Unicode characters will result in garbage characters being displayed on the terminal.

I haven't been able to reproduce this with a minimal example program, but in my Cursive-based program I can consistently reproduce this and have confirmed in gdb that the dropped memory becomes overwritten in the above fashion somewhere in core::ptr::real_drop_in_place(). This patch fixes the problem.

The wrapper for the ncurses setlocale() function converts the provided
locale &str to a C string by creating a std::ffi::CString, then passing
its inner pointer on to the ncurses function. This inner pointer
outlived the CString's temporary lifetime, so the data it pointed to was
undefined when calling setlocale(). The fix is to bind the CString to a
variable, thus extending its lifetime.

Most of the time this does not result in bad behavior, but if the memory
being referred to by the pointer is overwritten before the call to
setlocale(), then a garbage locale string may be used. Unable to find
the locale, libc will fall back to a "C" locale, and attempts to print
most Unicode characters will result in garbage characters being
displayed on the terminal.

I haven't been able to reproduce this with a minimal example program,
but in my Cursive-based program I can consistently reproduce this and
have confirmed in gdb that the dropped memory becomes overwritten in the
above fashion somewhere in core::ptr::real_drop_in_place(). This patch
fixes the problem.
@jeaye
Copy link
Owner

jeaye commented Feb 25, 2019

Thank you for the PR and for the explanation! I'll make a new release with this now.

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

Successfully merging this pull request may close these issues.

2 participants