public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "b.buschinski at googlemail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/98109] Seemingly wrong warnings from -Wnonnull when combined with -O2 -fsanitize=undefined
Date: Sun, 31 Jul 2022 09:02:39 +0000	[thread overview]
Message-ID: <bug-98109-4-NmDg5OxKPJ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-98109-4@http.gcc.gnu.org/bugzilla/>

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

Bernd Buschinski <b.buschinski at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b.buschinski at googlemail dot com

--- Comment #4 from Bernd Buschinski <b.buschinski at googlemail dot com> ---
I also hit this problem, but not 100% sure if this is a duplicate or not.
Because as far as I can remember it used to work with GCC-11 (can not test now
only have 12) and only started failing with GCC 12.

Can someone tell me if this should be a new bug or is just a duplicate?

Known to fail on:
- sys-devel/gcc-12.1.1_p20220625 (Gentoo)
- gcc-12.1.1-1.fc36.x86_64 12.1.1 20220507 (Red Hat 12.1.1-1) (Fedora)
- 12-20220319-1ubuntu1 (Ubuntu 22.04)

Compiler explorer: https://godbolt.org/z/rrWffMe9x


Code:


#include <stdlib.h>

extern void *my_memmem(const void *__haystack, size_t __haystacklen,
                       const void *__needle, size_t __needlelen)
     __THROW __attribute_pure__ __nonnull ((1, 3))
    __attribute__((access(__read_only__, 1, 2)))
    __attribute__((access(__read_only__, 3, 4)));

#define CONTAINSCONST(_cstring, string)                                        
  \
  ({                                                                           
  \
    size_t const tmp_CONST_LEN = sizeof(string) - 1;                           
  \
    ((_cstring).length >= tmp_CONST_LEN &&                                     
  \
     (my_memmem((_cstring).buffer, (_cstring).length, string, tmp_CONST_LEN) !=
  \
      NULL));                                                                  
  \
  })

struct my_string {
  unsigned char *buffer;
  unsigned long long length;
};

int foo(struct my_string const *const ss) {
  struct my_string const str_path = *ss;

  if (CONTAINSCONST(str_path, "/") ||
      CONTAINSCONST(str_path, ".abcd") ||
      CONTAINSCONST(str_path, "?")) {
    return 1;
  }
  return 0;
}

// $ gcc -std=gnu99 -Wall -Wextra -Walloc-zero -O2 -fsanitize=undefined -Werror
test2.c -S
// test2.c: In function 'foo':
// test2.c:13:7: error: argument 1 is null but the corresponding size argument
2 value is [5, 18446744073709551615] [-Werror=nonnull]
//    13 |      (my_memmem((_cstring).buffer, (_cstring).length, string,
tmp_CONST_LEN) !=   \
//       |      
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test2.c:26:7: note: in expansion of macro 'CONTAINSCONST'
//    26 |       CONTAINSCONST(str_path, ".abcd") ||
//       |       ^~~~~~~~~~~~~
// test2.c:3:14: note: in a call to function 'my_memmem' declared with
attribute 'access (read_only, 3, 4)'
//     3 | extern void *my_memmem(const void *__haystack, size_t __haystacklen,
//       |              ^~~~~~~~~
// cc1: all warnings being treated as errors

  parent reply	other threads:[~2022-07-31  9:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-02 22:54 [Bug middle-end/98109] New: " gabravier at gmail dot com
2020-12-03  1:08 ` [Bug middle-end/98109] " msebor at gcc dot gnu.org
2021-05-19 10:44 ` jlegg at feralinteractive dot com
2021-05-19 16:02 ` msebor at gcc dot gnu.org
2022-07-31  9:02 ` b.buschinski at googlemail dot com [this message]
2022-08-01 22:01 ` msebor 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-98109-4-NmDg5OxKPJ@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).