Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a compilation warning - directive output may be truncated #187

Merged
merged 2 commits into from
Feb 21, 2022

Conversation

zivke
Copy link
Contributor

@zivke zivke commented Dec 3, 2021

Add a simple solution to a compilation warning that doesn't require any special changes to be made to already existing enums, types or logic.

loguru/loguru.cpp: In function ‘void loguru::print_preamble(char*, size_t, loguru::Verbosity, const char*, unsigned int)’:
loguru/loguru.cpp:1208:50: warning: ‘% 4d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 5 [-Wformat-truncation=]
 1208 |    snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                  ^~~~
loguru/loguru.cpp:1208:49: note: directive argument in the range [1, 2147483647]
 1208 |    snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                 ^~~~~~
In file included from /usr/include/stdio.h:867,
                 from /usr/include/c++/9/cstdio:42,
                 from /usr/include/c++/9/ext/string_conversions.h:43,
                 from /usr/include/c++/9/bits/basic_string.h:6493,
                 from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/stdexcept:39,
                 from /usr/include/c++/9/array:39,
                 from /usr/include/c++/9/tuple:39,
                 from /usr/include/c++/9/functional:54,
                 from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/9/algorithm:71,
                 from loguru/loguru.cpp:36:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:67:35: note: ‘__builtin___snprintf_chk’ output between 5 and 12 bytes into a destination of size 5
   67 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   68 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Add a simple solution to a compilation warning that doesn't require any special changes to be made to already existing enums, types or logic.

loguru/loguru.cpp: In function ‘void loguru::print_preamble(char*, size_t, loguru::Verbosity, const char*, unsigned int)’:
loguru/loguru.cpp:1208:50: warning: ‘% 4d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 5 [-Wformat-truncation=]
 1208 |    snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                  ^~~~
loguru/loguru.cpp:1208:49: note: directive argument in the range [1, 2147483647]
 1208 |    snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                 ^~~~~~
In file included from /usr/include/stdio.h:867,
                 from /usr/include/c++/9/cstdio:42,
                 from /usr/include/c++/9/ext/string_conversions.h:43,
                 from /usr/include/c++/9/bits/basic_string.h:6493,
                 from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/stdexcept:39,
                 from /usr/include/c++/9/array:39,
                 from /usr/include/c++/9/tuple:39,
                 from /usr/include/c++/9/functional:54,
                 from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/9/algorithm:71,
                 from loguru/loguru.cpp:36:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:67:35: note: ‘__builtin___snprintf_chk’ output between 5 and 12 bytes into a destination of size 5
   67 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   68 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@bylowerik
Copy link
Collaborator

Can you please format the description by using <>-button for example?

loguru.cpp Outdated
@@ -1311,7 +1311,7 @@ namespace loguru
if (custom_level_name) {
snprintf(level_buff, sizeof(level_buff) - 1, "%s", custom_level_name);
} else {
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", (int8_t)verbosity);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could be more explicit and do static_cast<int8_t>(verbosity)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure, why not. Just keep in mind that all of this was a slightly hackish solution to begin with.

Copy link
Collaborator

@bylowerik bylowerik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants