https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89337

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |msebor at gcc dot gnu.org
             Blocks|                            |88443
         Resolution|---                         |INVALID

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
In the test case from attachment 45704:

  struct sstring {
    size_t _size;
    sstring(size_t size) : _size(size) { memset(begin(), '\n', size); }
    size_t size() const noexcept { return _size; }
    void resize(size_t n) {
      if (n > size()) {
        sstring x(n - size());
      }
    }
    char *begin();
  };
  void drop3(sstring &name) { name.resize(name.size() - 3); }

GCC can't see that drop3() cannot be called with name.size() < 3, and in
resize, the condition (n > size()) can only be true only when name.size() < 3
so n - size() is unavoidably too large.

To avoid the warning make the precondition explicit:

  void drop3(sstring &name)
  {
    if (name.size () < 3)
    __builtin_unreachable ();

    name.resize(name.size() - 3);
  }


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88443
[Bug 88443] [meta-bug] bogus/missing -Wstringop-overflow warnings

Reply via email to