Skip to content

Commit b035e9c

Browse files
author
Hwi-sung Im
committed
Update C26430 rule with heuristics that was unclear in original documentation, with supporting example code snippets.
1 parent c0faabe commit b035e9c

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

docs/code-quality/c26430.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ A variable is marked as checked for null when it's used in the following context
2727
- in non-bitwise logical operations;
2828
- in comparison operations where one operand is a constant expression that evaluates to zero.
2929

30-
The rule doesn't have full data flow tracking. It can produce incorrect results in cases where indirect checks are used (such as when an intermediate variable holds a null value and is later used in a comparison).
31-
3230
Implicit null checks are assumed when a pointer value is assigned from:
3331

3432
- an allocation performed with throwing `operator new`;
@@ -67,3 +65,32 @@ void merge_states(gsl::not_null<const state *> left, gsl::not_null<const state *
6765
}
6866
}
6967
```
68+
69+
## Heuristics
70+
71+
The rule does not enforce every dereference of a pointer to have a prior null check to ensure the pointer is not null. Instead, it requires a null check before first derefernce of the pointer. So, the following function does not trigger a C26430 warning:
72+
```cpp
73+
void f(int* p)
74+
{
75+
if (p)
76+
*p = 1;
77+
*p = 2;
78+
}
79+
```
80+
But the following function gets a C26430 warning because there is a path without null check for p to where *p is assigned with 2:
81+
```cpp
82+
void f(bool b, int* p)
83+
{
84+
if (b && p)
85+
*p = 1;
86+
*p = 2;
87+
}
88+
```
89+
Rules [C26822](c26822.md) and [C26823](c26823.md) apply to dereferencing a (possibly) null pointer.
90+
91+
The rule doesn't have full data flow tracking. It can produce incorrect results in cases where indirect checks are used (such as when an intermediate variable holds a null value and is later used in a comparison).
92+
93+
## See also
94+
95+
[C26822](c26822.md)\
96+
[C26823](c26823.md)

0 commit comments

Comments
 (0)