[PATCH] [libc++] Use snprintf() instead of sprintf()

Howard Hinnant hhinnant at apple.com
Sat Jun 29 17:47:18 PDT 2013


I'm having trouble applying this patch:

$ patch -p0 < temp.patch 
patching file include/locale
patch: **** malformed patch at line 6: }

And this is just long enough that I'm hesitant to do it manually for fear of making a mistake.  I'm not sure what the correct fix is.  When I create a patch file I do:

$ svn diff | my_editor

Howard

On Jun 7, 2013, at 10:57 PM, Matthew Dempsky <matthew at dempsky.org> wrote:

> In libc++'s <locale>, there's already dependence on an snprintf_l
> implementation and all of the char buffers readily have their
> allocated size available, so we can easily use snprintf_l instead of
> sprintf_l.
> 
> This avoids OpenBSD's linker warnings against using sprintf and
> vsprintf.
> 
> Index: include/locale
> ===================================================================
> --- include/locale	(revision 183583)
> +++ include/locale	(working copy)
> @@ -357,20 +357,6 @@ size_t __mbsrtowcs_l(wchar_t *__dest, const char *
> }
> 
> inline
> -int __sprintf_l(char *__s, locale_t __l, const char *__format, ...) {
> -  va_list __va;
> -  va_start(__va, __format);
> -#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -  int __res = vsprintf_l(__s, __l, __format, __va);
> -#else
> -  __locale_raii __current(uselocale(__l), uselocale);
> -  int __res = vsprintf(__s, __format, __va);
> -#endif
> -  va_end(__va);
> -  return __res;
> -}
> -
> -inline
> int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
>   va_list __va;
>   va_start(__va, __format);
> @@ -1803,9 +1789,9 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type
>                           + 1;
>     char __nar[__nbuf];
> #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> +    int __nc = snprintf_l(__nar, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> #else
> -    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
> +    int __nc = __snprintf_l(__nar, __nbuf, __cloc(), __fmt, __v);
> #endif
>     char* __ne = __nar + __nc;
>     char* __np = this->__identify_padding(__nar, __ne, __iob);
> @@ -1833,9 +1819,9 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type
>                           + 1;
>     char __nar[__nbuf];
> #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> +    int __nc = snprintf_l(__nar, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> #else
> -    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
> +    int __nc = __snprintf_l(__nar, __nbuf, __cloc(), __fmt, __v);
> #endif
>     char* __ne = __nar + __nc;
>     char* __np = this->__identify_padding(__nar, __ne, __iob);
> @@ -1863,9 +1849,9 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type
>                           + 1;
>     char __nar[__nbuf];
> #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> +    int __nc = snprintf_l(__nar, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> #else
> -    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
> +    int __nc = __snprintf_l(__nar, __nbuf, __cloc(), __fmt, __v);
> #endif
>     char* __ne = __nar + __nc;
>     char* __np = this->__identify_padding(__nar, __ne, __iob);
> @@ -1893,9 +1879,9 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type
>                           + 1;
>     char __nar[__nbuf];
> #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> +    int __nc = snprintf_l(__nar, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> #else
> -    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
> +    int __nc = __snprintf_l(__nar, __nbuf, __cloc(), __fmt, __v);
> #endif
>     char* __ne = __nar + __nc;
>     char* __np = this->__identify_padding(__nar, __ne, __iob);
> @@ -2057,9 +2043,9 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type
>     const unsigned __nbuf = 20;
>     char __nar[__nbuf];
> #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
> -    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> +    int __nc = snprintf_l(__nar, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
> #else
> -    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
> +    int __nc = __snprintf_l(__nar, __nbuf, __cloc(), __fmt, __v);
> #endif
>     char* __ne = __nar + __nc;
>     char* __np = this->__identify_padding(__nar, __ne, __iob);
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list