scriptstdstring parseFloat bug

Recommended Posts

Hi,

I just found this piece of code in the scriptstdstring addon:

	// WinCE doesn't have setlocale. Some quick testing on my current platform
// still manages to parse the numbers such as "3.14" even if the decimal for the
// locale is ",".
#if !defined(_WIN32_WCE) && !defined(ANDROID) && !defined(__psp2__)
// Set the locale to C so that we are guaranteed to parse the float value correctly
char *orig = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, "C");
#endif

double res = strtod(val.c_str(), &end);

#if !defined(_WIN32_WCE) && !defined(ANDROID) && !defined(__psp2__)
// Restore the locale
setlocale(LC_NUMERIC, orig);
#endif

But, this seems to be invalid code, since the second setlocale call will invalidate the first "orig" pointer, as described on MSDN: https://msdn.microsoft.com/en-us/library/x99tb11d.aspx

Quote

You can copy the string returned by setlocale to restore that part of the program's locale information. Global or thread local storage is used for the string returned by setlocale. Later calls to setlocale overwrite the string, which invalidates string pointers returned by earlier calls.

Share on other sites

Thanks. You're right. This appears to be a bug. I'll have it fixed.

Regards,
Andreas

Share on other sites

I've fixed this in revision 2468.

Create an account

Register a new account