Skip to content

Fix date parsing crash on newer libc#34

Open
nyanpasu64 wants to merge 2 commits into
fortheusers:mainfrom
nyanpasu64:fix-locale-crash
Open

Fix date parsing crash on newer libc#34
nyanpasu64 wants to merge 2 commits into
fortheusers:mainfrom
nyanpasu64:fix-locale-crash

Conversation

@nyanpasu64

Copy link
Copy Markdown

Since newlib commit e5a9f552ecf908f1639b08729edc191c66f4e7d0, setlocale(LC_ALL, null) returns "C/C.UTF-8/C/C/C/C" instead of "C". And libstdc++'s std::locale(char*) cannot parse that locale and throws a runtime_error, which corrupts the call stack resulting in a segfault.

To fix this, pass std::locale::classic() directly and skip the setlocale business. And if even this fails, catch the exception and fail the
function instead.

Is it likely to throw an exception? Probably not. Ideally I'd exit the entire app cleanly (preferably avoiding a game/applet crash) rather than silently swallowing the exception, but I don't know how...

  • Funnily enough the original code was wrong since it parsed dates in the user's locale, while we were receiving fixed-format strings from a server-side JSON file, which the user's locale may not be compatible with.
  • Are there really platforms with a working std::istringstream + std::get_time, but no working strptime?

Since newlib commit e5a9f552ecf908f1639b08729edc191c66f4e7d0,
setlocale(LC_ALL, null) returns "C/C.UTF-8/C/C/C/C" instead of "C". And
libstdc++'s std::locale(char*) cannot parse that locale and throws a
runtime_error, which corrupts the call stack resulting in a segfault.

To fix this, pass std::locale::classic() directly and skip the setlocale
business. And if even this fails, catch the exception and fail the
function instead.

Is it likely to throw an exception? Probably not. Ideally I'd exit the
entire app cleanly (preferably avoiding a game/applet crash) rather than
silently swallowing the exception, but I don't know how...
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.

1 participant