public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/100903] Bogus "zero as null pointer constant" warning
Date: Thu, 16 Nov 2023 14:35:54 +0000	[thread overview]
Message-ID: <bug-100903-4-8MdrvHmaV0@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-100903-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to M Welinder from comment #0)
> The standard is crazy-strict here.  Anything other than a literal 0 is
> undefined behaviour.  Allowed: 0, 0LL, 0'0'0'0, 0x0.

I don't think 0LL is allowed. A literal 0 has type int, and 0LL doesn't. I'm
unsure about 0'0 and 0x0.

>  No good: +0, nullptr,
> (void*)0, 0+0, '\0', (0,nullptr).  gcc incorrectly allows nullptr and
> (0,nullptr), but rejects the others.

gcc is not incorrect, the code that uses nullptr or (0, nullptr) is incorrect.
No diagnostic is required for violations of that rule.

> The library tries to do that with a construct that mainly allows null
> pointer constants.  That leads to the warning when you actually supply the 0
> you are supposed to.
> 
> Suggestion: just use an int argument.  That's wrong in different ways (and
> might cause warnings with 0LL which is allowed), but it matches 0 better.

Using int would allow 1 and 100 though. That seems much worse than allowing
other spellings of zero.

However, since I added the <compare> header gcc gained support for consteval
functions, which would allow us to reject non-zero values.

Something like this should work:

    struct __unspec
    {
      template<same_as<int> _Tp>
        consteval __unspec(_Tp __z) noexcept
        {
          if (__z != 0) throw; // comparison category types only compare to 0
        }
    };

This results in errors like:

cmp.cc:6:13: error: no match for 'operator<' (operand types are
'std::partial_ordering' and 'long int')
    6 |   return po < 0L;
      |          ~~ ^ ~~
      |          |    |
      |          |    long int
      |          std::partial_ordering

and:

/home/jwakely/gcc/14/include/c++/14.0.0/compare:59:25: error: expression
'<throw-expression>' is not a constant expression
   59 |           if (__z != 0) throw; // comparison category types only
compare to 0
      |                         ^~~~~

  parent reply	other threads:[~2023-11-16 14:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-04 13:11 [Bug libstdc++/100903] New: " terra at gnome dot org
2021-06-04 14:53 ` [Bug libstdc++/100903] " jakub at gcc dot gnu.org
2021-06-04 16:14 ` terra at gnome dot org
2021-06-04 17:16 ` redi at gcc dot gnu.org
2021-06-04 17:17 ` redi at gcc dot gnu.org
2021-06-04 17:21 ` redi at gcc dot gnu.org
2021-06-04 17:21 ` redi at gcc dot gnu.org
2021-06-07 15:01 ` jakub at gcc dot gnu.org
2023-11-16 10:24 ` marc.mutz at hotmail dot com
2023-11-16 14:22 ` redi at gcc dot gnu.org
2023-11-16 14:35 ` redi at gcc dot gnu.org [this message]
2023-11-16 15:07 ` redi at gcc dot gnu.org
2023-11-16 16:35 ` terra at gnome dot org
2023-11-16 17:15 ` redi at gcc dot gnu.org
2023-11-21  0:30 ` headch at gmail dot com
2024-01-25 17:19 ` redi 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-100903-4-8MdrvHmaV0@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).