public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113047] New: dereferencing a null pointer in a constant expression
@ 2023-12-16 21:01 gcc_bz at brnz dot org
  2023-12-16 21:02 ` [Bug c++/113047] " gcc_bz at brnz dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gcc_bz at brnz dot org @ 2023-12-16 21:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113047
           Summary: dereferencing a null pointer in a constant expression
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc_bz at brnz dot org
  Target Milestone: ---

Should it be possible to dereference a null pointer in a C++20 constant
expression?

Consider this c++20 code:

struct T {
  constexpr bool isNull() {
    return !this;
  }
};
static_assert(((T*)nullptr)->isNull());

constexpr T* t = nullptr;
static_assert((*t).isNull());


I would expect this to not compile due to the dereferencing of a null pointer
in a constant expression.

With reference to https://godbolt.org/z/q53rz5d69

By default, gcc-13.2 compiles without warning. -Wall yields a -Wnonnull
warning.

clang-17.0.1 produces errors and warning, by default.

msvc 19.38 compiles without warning.

Potentially relevant context:
2748. Accessing static data members via null pointer
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2748

2823. Implicit undefined behavior when dereferencing pointers
https://cplusplus.github.io/CWG/issues/2823.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/113047] dereferencing a null pointer in a constant expression
  2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
@ 2023-12-16 21:02 ` gcc_bz at brnz dot org
  2023-12-16 21:16 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: gcc_bz at brnz dot org @ 2023-12-16 21:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from gcc_bz at brnz dot org ---
I have submitted this same question for clang: 
https://github.com/llvm/llvm-project/issues/75716

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/113047] dereferencing a null pointer in a constant expression
  2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
  2023-12-16 21:02 ` [Bug c++/113047] " gcc_bz at brnz dot org
@ 2023-12-16 21:16 ` pinskia at gcc dot gnu.org
  2023-12-19  4:22 ` de34 at live dot cn
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-16 21:16 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=102420

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I suspect this is a dup of bug 102420 .

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/113047] dereferencing a null pointer in a constant expression
  2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
  2023-12-16 21:02 ` [Bug c++/113047] " gcc_bz at brnz dot org
  2023-12-16 21:16 ` pinskia at gcc dot gnu.org
@ 2023-12-19  4:22 ` de34 at live dot cn
  2023-12-19 22:31 ` nathanieloshead at gmail dot com
  2024-01-18 19:17 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: de34 at live dot cn @ 2023-12-19  4:22 UTC (permalink / raw)
  To: gcc-bugs

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

Jiang An <de34 at live dot cn> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |de34 at live dot cn

--- Comment #3 from Jiang An <de34 at live dot cn> ---
CWG2823 has been voted into the standard (see https://wg21.link/p3046r0,
https://wg21.link/n4972).

> Should it be possible to dereference a null pointer in a C++20 constant expression?

Now such derefencing is clearly UB and needs to cause constant evaluation
failure (not only in C++20, IMO).

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/113047] dereferencing a null pointer in a constant expression
  2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
                   ` (2 preceding siblings ...)
  2023-12-19  4:22 ` de34 at live dot cn
@ 2023-12-19 22:31 ` nathanieloshead at gmail dot com
  2024-01-18 19:17 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: nathanieloshead at gmail dot com @ 2023-12-19 22:31 UTC (permalink / raw)
  To: gcc-bugs

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

Nathaniel Shead <nathanieloshead at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nathanieloshead at gmail dot com

--- Comment #4 from Nathaniel Shead <nathanieloshead at gmail dot com> ---
I've pushed a fix for PR102420 (and hence comment #1), but looking at the DR
this isn't sufficient for the result of CWG2823, for which presumably all of
the following should also start erroring as well (note none of these error in
Clang yet either):

  struct X {
    static constexpr int f() { return 0; }
  };

  constexpr int g(X* x) { return (*x).f(); }  // error
  constexpr int a = g(nullptr);

  constexpr int h(X* x) { return x->f(); }  // error
  constexpr int b = h(nullptr);

  // and similarly
  constexpr int test() {
    int* p = nullptr;
    *p;  // error
    return 0;
  }
  constexpr int t = test();

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/113047] dereferencing a null pointer in a constant expression
  2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
                   ` (3 preceding siblings ...)
  2023-12-19 22:31 ` nathanieloshead at gmail dot com
@ 2024-01-18 19:17 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-01-18 19:17 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-01-18
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-18 19:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-16 21:01 [Bug c++/113047] New: dereferencing a null pointer in a constant expression gcc_bz at brnz dot org
2023-12-16 21:02 ` [Bug c++/113047] " gcc_bz at brnz dot org
2023-12-16 21:16 ` pinskia at gcc dot gnu.org
2023-12-19  4:22 ` de34 at live dot cn
2023-12-19 22:31 ` nathanieloshead at gmail dot com
2024-01-18 19:17 ` mpolacek at gcc dot gnu.org

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).