0

I am using GCC 7.4.0 and at some point it started giving me the following warning:

offset outside bounds of constant string

The reason is that the code is now templated and inlined and the compiler can now make some additional compile time checks. The code looks more or less like this:

template <typename StringType = std::string>
class StringWrapper {
public:
    explicit StringWrapper(const typename StringType::value_type* string_buffer);

private:
    static bool check(const typename StringType::value_type* string_buffer);

private:
    StringType m_text;
};

template <typename StringType>
StringWrapper<StringType>::StringWrapper(
    const typename StringType::value_type* string_buffer)
    : m_text(check(string_buffer) ? string_buffer + 3U
                                  : StringType{}) // the warning is at this line
{
}

template <typename StringType>
bool StringWrapper<StringType>::check(const typename StringType::value_type* string_buffer)
{
    if (string_buffer == nullptr) {
        return false;
    }

    return std::none_of(string_buffer, string_buffer + 3U,
        [](const typename StringType::value_type& string_element) {
            return string_element == '\0';
        });
}

int main()
{
    StringWrapper<> a("na");
    return 0;
}

The compiler seems not to be able to understand at compile time that the function check() as already validated the offset to be applicable without any risk. The reason why the compiler basically ignores the call to check() is that the function unfortunately cannot be made constexpr, and therefore it cannot be evaluated at compile time.

How can I disable only this specific warning and only for this specific portion of code?

I've found How to disable GCC warnings for a few lines of code but I do not know what -Wname_of_warning should be used.

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
  • If `char_buffer` and `offset` are constant expressions then I don't think you're check function is actually working since if it knows you are out of bounds at compile time. – NathanOliver Sep 25 '19 at 18:03
  • @NathanOliver my `check` function is working, but the compiler is ignoring its existence for the reason I already explained in the question (which anyway is not a duplicate of the other one since I am asking this specific error, which I do not know which `-Wwhatever-the-name is) – nyarlathotep108 Sep 25 '19 at 18:08
  • The error message should contain which warning triggered it. Can you share the full error message? – NathanOliver Sep 25 '19 at 18:09
  • @NathanOliver `path/MyClass.inl: In member function 'virtual void MyClassTest_invalid_Test::TestBody()': path/MyClass.inl:28:53: warning: offset outside bounds of constant string` – nyarlathotep108 Sep 25 '19 at 18:13
  • Looks like `-Warray-bounds` is the specific warning flag – NathanOliver Sep 25 '19 at 18:15
  • @NathanOliver I tried that one without any success. I am trying now `-Wstringop-overflow`. Where are you checking by the way? Unfortunately I found no table which matches together warning code with warning flags with warning messages anywhere so far – nyarlathotep108 Sep 25 '19 at 18:20
  • @NathanOliver apparently the check is there by default as long as `-O2` optimization level is being provided. I have no idea how to disable this check with a `#pragma` directive. – nyarlathotep108 Sep 25 '19 at 18:49
  • Try `-Warray-bounds=1` That is the setting that is applied for `-O2` – NathanOliver Sep 25 '19 at 18:57
  • @NathanOliver I am writing `#pragma GCC diagnostic push` then `#pragma GCC diagnostic ignored "-Warray-bounds=1"` and after my function definition `#pragma GCC diagnostic pop`, but it does not go away – nyarlathotep108 Sep 25 '19 at 19:03
  • 1
    I've reopened and added info to the question – NathanOliver Sep 25 '19 at 19:07
  • @nyarlathotep108 If you shared a fully compilable minimal example rather than one line with a bunch of undefined variables and functions, people might be able to help better. – Thomas Sep 25 '19 at 19:27
  • @Thomas you are right, question became more complex than expected in the beginning. I updated the question with more code. Please keep in mind only with `-O2` you will get the warning – nyarlathotep108 Sep 26 '19 at 08:43
  • Can't reproduce on g++ 9.1.0 using `g++ --std=c++17 -O2 -otest test.cc`. (I had to add `#include ` and `#include ` to make it compile.) I get no warnings with any of the `-O` options, even if I add `-Wall -Wextra -pedantic`. Nor with `--std=c++11` or without `--std` flag at all. – Thomas Sep 26 '19 at 10:04

0 Answers0