public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tobi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/115267] New: Undue warning about undefined behavior in
Date: Wed, 29 May 2024 03:51:26 +0000	[thread overview]
Message-ID: <bug-115267-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 115267
           Summary: Undue warning about undefined behavior in
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tobi at gcc dot gnu.org
  Target Milestone: ---

This seems to be a simpler case of PR103724. I originally observed it with 4.9
but was able to reproduce it with gcc 14.

Godbolt: https://godbolt.org/z/6h8bEYsvP


=== Testcase ===================
#include <iostream>
#include <sstream>

double arr[4];

void read(std::istream& is) {
    for (unsigned int i = 0; i < 2; ++i) {
        for (unsigned int j = i; j < 2; ++j) {
            is >> arr[2*i+j];
            if (i != j) {
                arr[2*j+i] = arr[2*i+j];
            }
        }
    }
}

int main()
{
    std::stringstream ss("1 2 3");
    read(ss);
    return int(arr[0] + arr[1]);
}
===

Gives with -O3 -Wall:
=========================
<source>:11:39: warning: iteration 4 invokes undefined behavior
[-Waggressive-loop-optimizations]
   11 |                 arr[2*j+i] = arr[2*i+j];
      |                              ~~~~~~~~~^
<source>:8:36: note: within this loop
    8 |         for (unsigned int j = i; j < 2; ++j) {
      |                           
==========================
Try as I might, I don't see the undefined behavior, the matrix is also
correctly filled in, no infinite loop obtains.  Signedness of the loop index
doesn't play a role.  The original code used a Eigen::Matrix2d instead of
manually indexing the 2x2 matirx, and then the warning is about iteration 2. 
The stringstream seems to be necessary to confuse the compiler enough, but
maybe there's a way of getting the same behavior with a less complex thing
inside the loop.

             reply	other threads:[~2024-05-29  3:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-29  3:51 tobi at gcc dot gnu.org [this message]
2024-05-29  7:45 ` [Bug tree-optimization/115267] [14./15 Regression] Undue warning about undefined behavior in loop with varying limits rguenth at gcc dot gnu.org

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-115267-4@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).