Skip to content

Commit 2ee166d

Browse files
author
Jill Grant
authored
Merge pull request #5609 from hwisungi/main
Update C26430 rule with heuristics that were unclear in the original documentation
2 parents f68f2cf + c05ba65 commit 2ee166d

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

docs/code-quality/c26430.md

Lines changed: 33 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,36 @@ void merge_states(gsl::not_null<const state *> left, gsl::not_null<const state *
6765
}
6866
}
6967
```
68+
69+
## Heuristics
70+
71+
When ensuring that a dereference of a pointer isn't null, this rule doesn't require *every* dereference to have a prior null check. Instead, it requires a null check before *first* dereference of the pointer. The following function doesn't trigger C26430:
72+
73+
```cpp
74+
void f(int* p)
75+
{
76+
if (p)
77+
*p = 1;
78+
*p = 2;
79+
}
80+
```
81+
82+
The following function generates C26430 because there's a path to assign `*p` without a null check:
83+
84+
```cpp
85+
void f(bool b, int* p)
86+
{
87+
if (b && p)
88+
*p = 1;
89+
*p = 2;
90+
}
91+
```
92+
93+
Rules [C26822](c26822.md) and [C26823](c26823.md) apply to dereferencing a (possibly) null pointer.
94+
95+
This rule doesn't do 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.
96+
97+
## See also
98+
99+
[C26822](c26822.md)\
100+
[C26823](c26823.md)

0 commit comments

Comments
 (0)