emilk / loguru

A lightweight C++ logging library
The Unlicense
1.79k stars 260 forks source link

[GCC Warning] '% 4d' directive output may be truncated #92

Open bugwelle opened 5 years ago

bugwelle commented 5 years ago

I get the following warning when compiling loguru with GCC8 on Ubuntu 18.04.

[  1%] Building CXX object third_party/CMakeFiles/wren_third_party.dir/loguru/loguru.cpp.o
/<my path>/third_party/loguru/loguru.cpp: In function 'void loguru::print_preamble(char*, size_t, loguru::Verbosity, const char*, unsigned int)':
/<my path>/third_party/loguru/loguru.cpp:1165:49: warning: '% 4d' directive output may be truncated writing between 4 and 11 bytes into a region of size 5 [-Wformat-truncation=]
    snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
                                                 ^~~~~~
/<my path>/third_party/loguru/loguru.cpp:1165:49: note: directive argument in the range [1, 2147483647]
In file included from /usr/include/stdio.h:862,
                 from /usr/include/c++/8/cstdio:42,
                 from /usr/include/c++/8/ext/string_conversions.h:43,
                 from /usr/include/c++/8/bits/basic_string.h:6391,
                 from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/istream:38,
                 from /usr/include/c++/8/sstream:38,
                 from /<my path>/third_party/loguru/loguru.hpp:1068,
                 from /<my path>/third_party/loguru/loguru.cpp:19:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64:35: note: '__builtin___snprintf_chk' output between 5 and 12 bytes into a destination of size 5
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

See:

I haven't looked that much into loguru's source, so I can't say whether it's an actual problem/bug or not. :-)

I'll provide more information if requested.

Regards, Andre

BullyWiiPlaza commented 5 years ago

I'm also running into this problem so I had to disable "treat all warnings as errors" when compiling code using Loguru in release mode. This happens using GCC and MSVC. A fix would be appreciated.