public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "stefan at bytereef dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/113664] New: False positive warnings with -fno-strict-overflow (-Warray-bounds, -Wstringop-overflow)
Date: Mon, 29 Jan 2024 21:50:06 +0000	[thread overview]
Message-ID: <bug-113664-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113664
           Summary: False positive warnings with -fno-strict-overflow
                    (-Warray-bounds, -Wstringop-overflow)
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stefan at bytereef dot org
  Target Milestone: ---

These false positives only occur in combination with fno-strict-overflow:

================
 -Warray-bounds
================

foo.c
=========================================================
#include <stdio.h>

static char *
f(char *s, int n, char *dot)
{
  switch(n) {
  case 1:
    if (s == dot) {
      *s++ = '.';
    }
    *s++ = '0'; /* fall-through (yes, really!) */
  default:
    if (s == dot) {
      *s++ = '.';
    }
  }

  *s = '\0';
  return s;
}

char *
g(char *s)
{
  return f(s, 1, NULL);
}
=========================================================


$ /home/skrah/gcc/bin/gcc -Wall -O3 -c foo.c
$ /home/skrah/gcc/bin/gcc -Wall -O3 -fno-strict-overflow -c foo.c
In function ‘f’,
    inlined from ‘g’ at foo.c:25:10:
foo.c:11:10: warning: array subscript 0 is outside array bounds of ‘char[0]’
[-Warray-bounds=]
   11 |     *s++ = '0'; /* fall-through (yes, really!) */
      |     ~~~~~^~~~~
In function ‘g’:
cc1: note: source object is likely at address zero



=====================
 -Wstringop-overflow 
=====================

bar.c
=========================================================
#include <stdio.h>

static char *
f(char *s, int n, char *dot)
{
  switch(n) {
  case 1:
    if (s == dot) {
      *s++ = '.';
    }
    *s++ = '0'; /* fall-through (yes, really!) */
  default:
    if (s == dot) {
      *s++ = '.';
    }
  }

  *s = '\0';
  return s;
}

char *
g(char *s)
{
    char sign = '+';
    *s++ = sign;

    return f(s, 1, NULL);
}
=========================================================


$ /home/skrah/gcc/bin/gcc -Wall -O3 -c bar.c
$ /home/skrah/gcc/bin/gcc -Wall -O3 -fno-strict-overflow -c bar.c
In function ‘f’,
    inlined from ‘g’ at bar.c:28:12:
bar.c:11:10: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
   11 |     *s++ = '0'; /* fall-through (yes, really!) */
      |     ~~~~~^~~~~
In function ‘g’:
cc1: note: destination object is likely at address zero




Note that a very small change gives a very different warning.

             reply	other threads:[~2024-01-29 21:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29 21:50 stefan at bytereef dot org [this message]
2024-01-29 22:01 ` [Bug tree-optimization/113664] " pinskia at gcc dot gnu.org
2024-01-29 22:16 ` stefan at bytereef dot org
2024-01-29 22:19 ` pinskia at gcc dot gnu.org
2024-01-30  8:00 ` rguenth at gcc dot gnu.org
2024-01-30 11:11 ` stefan at bytereef dot org
2024-01-30 11:25 ` stefan at bytereef dot 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-113664-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).