Skip to content

Add lint to detect dereferencing of NULL pointers #83856

Closed
@RalfJung

Description

@RalfJung

I propose we add a lint that detects code like

*(0 as *const i32)
*ptr::null()
*ptr::null_mut()

Some people seem to think that this is okay in contexts like &*(0 as *const i32) or addr_of!(*(0 as *const i32)), but that is not the case -- * on a NULL pointer is UB even as a place expression.

To implement this, you can use the invalid_value lint as a template:

declare_lint_pass!(InvalidValue => [INVALID_VALUE]);

check_expr should check for Unary expressions with a Deref operator, and then check the operand to be either a cast of 0 to a pointer type, or a call to one of the null methods. To detect the methods, make them "diagnostic items"; the invalid_value lint does that e.g. for mem::zeroed so it can again serve as a template here.

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-langRelevant to the language team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions