Fix memory corruption in locale string #181
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The wrapper for the ncurses
setlocale()
function converts the provided locale&str
to a C string by creating astd::ffi::CString
, then passing its inner pointer on to the ncurses function. This inner pointer outlived theCString
's temporary lifetime, so the data it pointed to was undefined when callingsetlocale()
. The fix is to bind theCString
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.