[libcxx-commits] [libcxx] b0cc7b5 - [libcxx] Don't use an undefined '+' in unsigned/octal/hexal print formats

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 4 02:09:23 PDT 2021


Author: Martin Storsjö
Date: 2021-06-04T12:07:30+03:00
New Revision: b0cc7b53a53f180c0b70b8b2f02999be78a32395

URL: https://github.com/llvm/llvm-project/commit/b0cc7b53a53f180c0b70b8b2f02999be78a32395
DIFF: https://github.com/llvm/llvm-project/commit/b0cc7b53a53f180c0b70b8b2f02999be78a32395.diff

LOG: [libcxx] Don't use an undefined '+' in unsigned/octal/hexal print formats

If building code like this:

    unsigned long val = 1000;
    snprintf(buf, sizeof(buf), "%+lu", val);

with clang, clang warns

    warning: flag '+' results in undefined behavior with 'u' conversion specifier [-Wformat]

Therefore, don't construct such undefined format strings. (There's
no compiler warnings here, as the compiler can't inspect dynamically
assembled format strings.)

This fixes number formatting in mingw-w64 if built with
`__USE_MINGW_ANSI_STDIO` defined (there, the '+' flag causes a
leading plus to be printed when formatting unsigned numbers too,
while the '+' flag doesn't cause any extra leading plus in other
stdio implementations).

Differential Revision: https://reviews.llvm.org/D103444

Added: 
    

Modified: 
    libcxx/src/locale.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index 903c78a1bde98..d5ab8fb3b8367 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -4597,7 +4597,10 @@ void
 __num_put_base::__format_int(char* __fmtp, const char* __len, bool __signd,
                              ios_base::fmtflags __flags)
 {
-    if (__flags & ios_base::showpos)
+    if ((__flags & ios_base::showpos) &&
+        (__flags & ios_base::basefield) != ios_base::oct &&
+        (__flags & ios_base::basefield) != ios_base::hex &&
+	__signd)
         *__fmtp++ = '+';
     if (__flags & ios_base::showbase)
         *__fmtp++ = '#';


        


More information about the libcxx-commits mailing list