Skip to content

Repo sync for protected branch #4942

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

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/code-quality/c6396.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
description: "Learn more about: Warning C6396: sizeof('integerConstant') always returns the size of the underlying integer type"
title: Warning C6396
ms.date: 02/05/2024
f1_keywords: ["C6396", "SIZEOF_CONSTANT"]
helpviewer_keywords: ["C6396"]
---
# Warning C6396

> sizeof('integerConstant') always returns the size of the underlying integer type

## Remarks

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.

This check ignores character literals because `buffer_size += sizeof(UNICODE_NULL)` is a common idiom.

## Example

```cpp
void f()
{
int a = sizeof(5); // C6396 reported here
}
```

To fix this issue, replace the integral constant with its type:

```cpp
void f()
{
int a = sizeof(int); // no C6396 reported here
}
```
38 changes: 38 additions & 0 deletions docs/code-quality/c6397.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
description: "Learn more about: Warning C6397: The address-of operator cannot return null pointer in well-defined code"
title: Warning C6397
ms.date: 02/05/2024
f1_keywords: ["C6397", "DUBIOUS_NULL_CHECK"]
helpviewer_keywords: ["C6397"]
---
# Warning C6397

> The address-of operator cannot return `null` pointer in well-defined code

## Remarks

The address-of operator returns the address of its operand. This value should never be compared to `nullptr`:
* 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.
* 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).

## Example

```cpp
bool isNull(int *a)
{
return &a == nullptr; // C6397 reported here.
}
```

To fix this issue, double check if the use of unary `&` was intentional:

```cpp
bool isNull(int *a)
{
return a == nullptr; // no C6397 reported here.
}
```

## See also

[C6398](c6398.md)
42 changes: 42 additions & 0 deletions docs/code-quality/c6398.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
description: "Learn more about: Warning C6398: The address-of a field cannot be null in well-defined code"
title: Warning C6398
ms.date: 02/05/2024
f1_keywords: ["C6398", "DUBIOUS_NULL_CHECK_FIELD"]
helpviewer_keywords: ["C6398"]
---
# Warning C6398

> The address-of a field cannot be `null` in well-defined code

## Remarks

The address-of operator returns the address of its operand. This value should never be compared to `nullptr`:
* 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.
* 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).

## Example

```cpp
struct A { int* x; };

bool hasNullField(A *a)
{
return &a->x == nullptr; // C6398 reported here.
}
```

To fix this issue, double check if the use of unary `&` was intentional:

```cpp
struct A { int* x; };

bool hasNullField(A *a)
{
return a->x == nullptr; // no C6398 reported here.
}
```

## See also

[C6397](c6397.md)
6 changes: 6 additions & 0 deletions docs/code-quality/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ items:
href: ../code-quality/c6394.md
- name: Warning C6395
href: ../code-quality/c6395.md
- name: Warning C6396
href: ../code-quality/c6396.md
- name: Warning C6397
href: ../code-quality/c6397.md
- name: Warning C6398
href: ../code-quality/c6398.md
- name: Warning C6400
href: ../code-quality/c6400.md
- name: Warning C6401
Expand Down