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

suppress -Wformat-overflow when optimization is enabled on GCC 8.2.0 #160

ToruNiina opened this Issue Sep 12, 2018 · 3 comments


None yet
2 participants
Copy link

ToruNiina commented Sep 12, 2018


When I compile the following code with g++ v8.2.0 on linux (Ubuntu 16.04),

#define BOOST_TEST_MODULE test_min
#include <boost/test/included/unit_test.hpp>

    const double a = 1.5, b = 2.5, c = 4.0;
    BOOST_TEST(c == a + b, boost::test_tools::tolerance(1e-8));

I get this warning.

$ g++-8 test.cpp -Wformat-overflow=1 -O2 -I./boost_1_68_0
In file included from ./boost_1_68_0/boost/test/impl/compiler_log_formatter.ipp:29,
                 from ./boost_1_68_0/boost/test/included/unit_test.hpp:18,
                 from test.cpp:2:
./boost_1_68_0/boost/test/utils/setcolor.hpp: In constructor ‘boost::unit_test::utils::scope_setcolor::scope_setcolor(std::ostream&, boost::unit_test::utils::term_attr::_, boost::unit_test::utils::term_color::_, boost::unit_test::utils::term_color::_)’:
./boost_1_68_0/boost/test/utils/setcolor.hpp:84:38: warning: ‘%d’ directive writing between 1 and 11 bytes into a region of size between 0 and 9 [-Wformat-overflow=]
         m_command_size = std::sprintf( m_control_command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40 );
./boost_1_68_0/boost/test/utils/setcolor.hpp:84:38: note: directive argument in the range [-2147483618, 2147483647]
./boost_1_68_0/boost/test/utils/setcolor.hpp:84:38: note: directive argument in the range [-2147483608, 2147483647]
./boost_1_68_0/boost/test/utils/setcolor.hpp:84:38: note: ‘sprintf’ output between 9 and 39 bytes into a destination of size 13

And the version of g++ is this.

$ g++-8 --version
g++ (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

Also, I found that it happens only when I turn optimization on. With -O0 (or without -O flags), I found no warning with -Wformat-overflow=1. You can check it here.

I know in reality it does not matter because the types of arguments are actually enum types and the range of the defined value is in [0, 9]. Even though, I would like to suppress it.

As the message said, the reason for this warning is that the buffer is too short. To supress the warning, basically, we need to increase the buffer size, or use std::snprintf instead. However std::snprintf doesn't exist in C++98, so we cannot use it. I think expanding the buffer is the best way if the size of the class setcolor does not matter.

I have one more workaround that does not need to expand the buffer. Assuming ASCII, we can convert an integral value to ASCII code and put it exactly as a byte. To make the argument as just one character, we can write 3 and 4 directly in the format string instead of adding 30 and 40 to the argument.

This workaround would look like this. I confirmed that it suppresses the warning.

m_command_size = std::sprintf( m_control_command, "%c[%c;3%c;4%cm", 0x1B,
    static_cast<char>(attr + 48), static_cast<char>(fg + 48), static_cast<char>(bg + 48));

However, since POSIX does not define the character encoding, it is not a portable code.

What do you think about this problem?



This comment has been minimized.

Copy link

raffienficiaud commented Nov 6, 2018

Thanks for the report, and sorry about the delay. I'll try to integrate the fix for the upcoming release.

raffienficiaud added a commit that referenced this issue Feb 3, 2019

Merge branch 'topic/GH-160-format-overflow' into next-internal
* topic/GH-160-format-overflow:
  Change log
  Suppress color setup warnings

@raffienficiaud raffienficiaud added next develop and removed next labels Feb 3, 2019


This comment has been minimized.

Copy link

raffienficiaud commented Feb 5, 2019

Should be in 1.70, thanks!

@raffienficiaud raffienficiaud self-assigned this Feb 8, 2019

@raffienficiaud raffienficiaud added 1.70 and removed develop labels Feb 8, 2019


This comment has been minimized.

Copy link

raffienficiaud commented Feb 8, 2019

In master, closing. Thanks!

@raffienficiaud raffienficiaud added this to the 1.70 milestone Feb 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.