- template<typename _Tv>
- int
- __convert_from_v(char* __out, const int __size, const char* __fmt,
- _Tv __v, const __c_locale&, int __prec = -1)
- {
- char* __old = setlocale(LC_ALL, NULL);
- char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
- if (__sav)
- strcpy(__sav, __old);
- setlocale(LC_ALL, "C");
-
- int __ret;
-#ifdef _GLIBCPP_USE_C99
- if (__prec >= 0)
- __ret = snprintf(__out, __size, __fmt, __prec, __v);
- else
- __ret = snprintf(__out, __size, __fmt, __v);
+ // Convert numeric value of type double and long double to string and
+ // return length of string. If vsnprintf is available use it, otherwise
+ // fall back to the unsafe vsprintf which, in general, can be dangerous
+ // and should be avoided.
+ inline int
+ __convert_from_v(const __c_locale&, char* __out,
+ const int __size __attribute__((__unused__)),
+ const char* __fmt, ...)
+ {
+ char* __old = std::setlocale(LC_NUMERIC, NULL);
+ char* __sav = NULL;
+ if (__builtin_strcmp(__old, "C"))
+ {
+ const size_t __len = __builtin_strlen(__old) + 1;
+ __sav = new char[__len];
+ __builtin_memcpy(__sav, __old, __len);
+ std::setlocale(LC_NUMERIC, "C");
+ }
+
+ __builtin_va_list __args;
+ __builtin_va_start(__args, __fmt);
+
+#ifdef _GLIBCXX_USE_C99
+ const int __ret = __builtin_vsnprintf(__out, __size, __fmt, __args);