Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dereferencing a null pointer in a constant expression #75716

Closed
twoscomplement opened this issue Dec 16, 2023 · 2 comments
Closed

dereferencing a null pointer in a constant expression #75716

twoscomplement opened this issue Dec 16, 2023 · 2 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! undefined behaviour

Comments

@twoscomplement
Copy link

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

clang-17.0.1 produces errors and warning, by default.

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

msvc 19.38 compiles without warning.

Potentially relevant context:
Accessing static data members via null pointer
Implicit undefined behavior when dereferencing pointers

I have submitted the same question for gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113047

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Dec 17, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 17, 2023

@llvm/issue-subscribers-clang-frontend

Author: Jonathan Adamczewski (twoscomplement)

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

clang-17.0.1 produces errors and warning, by default.

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

msvc 19.38 compiles without warning.

Potentially relevant context:
[Accessing static data members via null pointer](https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2748)
Implicit undefined behavior when dereferencing pointers

I have submitted the same question for gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113047

@shafik
Copy link
Collaborator

shafik commented Dec 18, 2023

Given the resolution of the two defect reports you cited clang is correct here. This is undefined behavior and should be rejected as ill-formed in a constant expression context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! undefined behaviour
Projects
None yet
Development

No branches or pull requests

4 participants