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

missing diagnostic for member access on null pointer in C++11 constant expression evaluation #37549

Open
llvmbot opened this issue Jul 18, 2018 · 4 comments
Labels
accepts-invalid bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party undefined behaviour

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 18, 2018

Bugzilla Link 38201
Version unspecified
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

The code is as follow:

struct S { int a, b; } s;

constexpr S *p = (S*)0;
constexpr const int *q = &p->b;

The code is invalid not because of a cast (there is none) but because it attempts to use a null pointer to form the address of a member of an object. clang++ accepts it. I checked g++. It rejects the code:

error: dereferencing a null pointer in '*0'
 constexpr const int *q = &p->b;
                              ^
@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Jul 18, 2018

Confirmed. Slightly reduced:

struct S { int a, b; };
constexpr int *q = &((S*)nullptr)->b;

Interesting things: clang rejects accesses to 'a', but not to 'b'. Changing from forming a pointer to forming a reference results in a diagnostic but with no note explaining why the RHS is non-constant.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@wheatman
Copy link
Contributor

wheatman commented Jan 21, 2024

This behavior is still the same in post 17 trunk(975deb3)
https://godbolt.org/z/5qsMxd8vz

code

struct S { int a, b; };
constexpr int *q = &((S*)nullptr)->b;

gcc 13.2 error

<source>:2:36: error: dereferencing a null pointer in '*0'
    2 | constexpr int *q = &((S*)nullptr)->b;
      |                                    ^
Compiler returned: 1

@wheatman wheatman added clang:frontend Language frontend issues, e.g. anything involving "Sema" accepts-invalid labels Jan 21, 2024
@llvmbot
Copy link
Collaborator Author

llvmbot commented Jan 21, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [38201](https://llvm.org/bz38201) | | Version | unspecified | | OS | Linux | | Reporter | LLVM Bugzilla Contributor | | CC | @DougGregor,@zygoloid |

Extended Description

The code is as follow:

struct S { int a, b; } s;

constexpr S p = (S)0;
constexpr const int *q = &p->b;

The code is invalid not because of a cast (there is none) but because it attempts to use a null pointer to form the address of a member of an object. clang++ accepts it. I checked g++. It rejects the code:

error: dereferencing a null pointer in '*0'
constexpr const int *q = &p->b;
^

@shafik
Copy link
Collaborator

shafik commented Jan 21, 2024

This is related to: #75716

The address of unary-operator disables the diagnostic, w/o it we obtain the expected diagnostic: https://godbolt.org/z/Kqbn3dvh5

@Endilll Endilll added the confirmed Verified by a second party label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts-invalid bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party undefined behaviour
Projects
None yet
Development

No branches or pull requests

4 participants