Skip to content

Commit f6dbb0b

Browse files
Learn Build Service GitHub AppLearn Build Service GitHub App
authored andcommitted
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents 5a91727 + ffbabdc commit f6dbb0b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

docs/code-quality/c6396.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
description: "Learn more about: Warning C6396: sizeof('integerConstant') always returns the size of the underlying integer type"
3+
title: Warning C6396
4+
ms.date: 02/05/2024
5+
f1_keywords: ["C6396", "SIZEOF_CONSTANT"]
6+
helpviewer_keywords: ["C6396"]
7+
---
8+
# Warning C6396
9+
10+
> sizeof('integerConstant') always returns the size of the underlying integer type
11+
12+
## Remarks
13+
14+
This warning indicates where an integral constant is used as a `sizeof` argument. Such expression always returns the size of the type of the constant. It's better to write `sizeof(type)` instead. This warning catches common typos in buffer offset calculations.
15+
16+
This check ignores character literals because `buffer_size += sizeof(UNICODE_NULL)` is a common idiom.
17+
18+
## Example
19+
20+
```cpp
21+
void f()
22+
{
23+
int a = sizeof(5); // C6396 reported here
24+
}
25+
```
26+
27+
To fix this issue, replace the integral constant with its type:
28+
29+
```cpp
30+
void f()
31+
{
32+
int a = sizeof(int); // no C6396 reported here
33+
}
34+
```

docs/code-quality/c6397.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
description: "Learn more about: Warning C6397: The address-of operator cannot return null pointer in well-defined code"
3+
title: Warning C6397
4+
ms.date: 02/05/2024
5+
f1_keywords: ["C6397", "DUBIOUS_NULL_CHECK"]
6+
helpviewer_keywords: ["C6397"]
7+
---
8+
# Warning C6397
9+
10+
> The address-of operator cannot return `null` pointer in well-defined code
11+
12+
## Remarks
13+
14+
The address-of operator returns the address of its operand. This value should never be compared to `nullptr`:
15+
* The address-of a field can only be `nullptr` if the base pointer was `nullptr` and the field is at the zero offset (`&p->field == nullptr` implies `p == nullptr`). In this case, the expression should be simplified.
16+
* In any other cases, the value of the unary `&` operator can't be `nullptr` unless there's undefined behavior in the code (`&v == nullptr` always evaluates to false).
17+
18+
## Example
19+
20+
```cpp
21+
bool isNull(int *a)
22+
{
23+
return &a == nullptr; // C6397 reported here.
24+
}
25+
```
26+
27+
To fix this issue, double check if the use of unary `&` was intentional:
28+
29+
```cpp
30+
bool isNull(int *a)
31+
{
32+
return a == nullptr; // no C6397 reported here.
33+
}
34+
```
35+
36+
## See also
37+
38+
[C6398](c6398.md)

docs/code-quality/c6398.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
description: "Learn more about: Warning C6398: The address-of a field cannot be null in well-defined code"
3+
title: Warning C6398
4+
ms.date: 02/05/2024
5+
f1_keywords: ["C6398", "DUBIOUS_NULL_CHECK_FIELD"]
6+
helpviewer_keywords: ["C6398"]
7+
---
8+
# Warning C6398
9+
10+
> The address-of a field cannot be `null` in well-defined code
11+
12+
## Remarks
13+
14+
The address-of operator returns the address of its operand. This value should never be compared to `nullptr`:
15+
* The address-of a field can only be `nullptr` if the base pointer was `nullptr` and the field is at the zero offset (`&p->field == nullptr` implies `p == nullptr`). In this case, the expression should be simplified.
16+
* In any other cases, the value of the unary `&` operator can't be `nullptr` unless there's undefined behavior in the code (`&v == nullptr` always evaluates to false).
17+
18+
## Example
19+
20+
```cpp
21+
struct A { int* x; };
22+
23+
bool hasNullField(A *a)
24+
{
25+
return &a->x == nullptr; // C6398 reported here.
26+
}
27+
```
28+
29+
To fix this issue, double check if the use of unary `&` was intentional:
30+
31+
```cpp
32+
struct A { int* x; };
33+
34+
bool hasNullField(A *a)
35+
{
36+
return a->x == nullptr; // no C6398 reported here.
37+
}
38+
```
39+
40+
## See also
41+
42+
[C6397](c6397.md)

docs/code-quality/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ items:
487487
href: ../code-quality/c6394.md
488488
- name: Warning C6395
489489
href: ../code-quality/c6395.md
490+
- name: Warning C6396
491+
href: ../code-quality/c6396.md
492+
- name: Warning C6397
493+
href: ../code-quality/c6397.md
494+
- name: Warning C6398
495+
href: ../code-quality/c6398.md
490496
- name: Warning C6400
491497
href: ../code-quality/c6400.md
492498
- name: Warning C6401

0 commit comments

Comments
 (0)