public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/104017] unexpeted -Warray-bounds popping a fixed number of std::deque elements
Date: Thu, 13 Jan 2022 23:16:29 +0000	[thread overview]
Message-ID: <bug-104017-4-PLGGSufUEV@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104017-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning triggers for the clobber statement in bb 43 below.  _236 is assumed
to point to the beginning of the block of 512 bytes allocated by new, so
subtracting a positive integer from it or adding one in excess of 512 is
invalid, as is dereferencing the result:

  <bb 2> [local count: 118111600]:
  ...
  _229 = operator new (512);                               >>> _229
  ...
  <bb 42> [local count: 50546886]:
  _176 = p.D.20902._M_impl.D.20257._M_finish._M_first;
  if (_176 != _229)
    goto <bb 43>; [82.57%]
  else
    goto <bb 44>; [17.43%]

  <bb 43> [local count: 41736564]:
  _236 = ASSERT_EXPR <_229, _229 != _176>;                 <<< _229
  _177 = _236 + 18446744073709551608;
  p.D.20951._M_impl.D.20306._M_finish._M_cur = _177;
  MEM[(const struct Node * *)_236 + -8B] ={v} {CLOBBER};   <<< -Warray-bounds
  goto <bb 45>; [100.00%]

I view the warning as helpful here (and so not a false positive even) because
the test function assumes the loop inserts at least three elements into the
container.  If it doesn't, one of the pop() calls will crash.

A more compelling test case would guard each if the pop() calls to prevent the
crash, like below:

#include <deque>

struct Node { Node const * parent = nullptr; };

void func(Node const * n)
{
    std::deque<Node const *> p;

    Node const * e = n;

    while (e != nullptr) {
        p.push_front(e);
        e = e->parent;
    }

    if (p.size ())
      p.pop_front();
    if (p.size ())
      p.pop_front();
    if (p.size ())
      p.pop_back();
}


This test case also triggers a warning, for the same reason: GCC can't
determine the relationship between a deque's internal node pointers and the
result of std::deque::size() (which is a function of the node pointers).

  reply	other threads:[~2022-01-13 23:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-13 22:52 [Bug middle-end/104017] New: " msebor at gcc dot gnu.org
2022-01-13 23:16 ` msebor at gcc dot gnu.org [this message]
2022-01-18 11:12 ` [Bug tree-optimization/104017] " larsbj at gullik dot net
2022-01-27 22:43 ` redi at gcc dot gnu.org
2022-01-27 22:44 ` redi at gcc dot gnu.org
2022-01-27 22:55 ` redi at gcc dot gnu.org
2022-05-12 14:37 ` [Bug tree-optimization/104017] unexpected " larsbj at gullik dot net

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104017-4-PLGGSufUEV@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).