Skip to content

Repo sync for protected branch #4976

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
Mar 7, 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
51 changes: 38 additions & 13 deletions docs/code-quality/c6328.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
---
description: "Learn more about: Warning C6328"
title: Warning C6328
ms.date: 09/15/2022
ms.date: 02/14/2024
f1_keywords: ["C6328", "FORMAT_SIZE_MISMATCH", "__WARNING_FORMAT_SIZE_MISMATCH"]
helpviewer_keywords: ["C6328"]
ms.assetid: e25b00fa-d344-4dc9-b322-b4f1ae06f315
---
# Warning C6328

> Size mismatch: '*type*' passed as parameter '*number*' when '*type*' is required in call to '*function-name*'
> Size mismatch: '*type*' passed as _Param_(*number*) when '*type*' is required in call to '*function-name*'

## Remarks

An argument of type `char` passed to C runtime, character-based routines named `is<xxx>` (for example, `isspace`) can have unpredictable results. For example, an SBCS or MBCS single-byte character of type `char` with a value greater than `0x7F` is a negative value. If a `char` is passed, the compiler might convert the value to a signed `int` or a signed `long`. This value could be sign-extended by the compiler, with unexpected results. This issue could result in a function like `isspace`, which only expects values from 0-255 or `EOF`, being sent a negative value.

Warning C6328 exists specifically to catch this bug. For characters in the 7-bit ASCII range, the cast is unnecessary. Characters outside that range can have unpredictable results such as program fault and termination.
This warning indicates that type required by the format specifier and the type of the expression passed in don't match.
Using the wrong format specifier is undefined behavior. To fix the warning, make sure that the format specifiers match the types of the expressions passed in.

Code analysis name: `FORMAT_SIZE_MISMATCH`

## Example

The following code generates this warning. By default, `char` is a signed type in the Microsoft C++ compiler, so the range of values of a `char` is -128 to 127. Therefore, in the following code, `c` would be sign-extended to a signed `int` with a value of -37, which is outside the valid range for [isspace](../standard-library/locale-functions.md#isspace):
```cpp
#include <cstdio>

void f(long long a)
{
printf("%d\n", a); // C6328 emitted.
}
```

There are multiple ways to fix the undefined behavior. We can change the format specifier:

```cpp
#include <cstdio>

void f(long long a)
{
printf("%lld\n", a); // No C6328 emitted.
}
```

We can change the type of the expression:

```cpp
void f()
#include <cstdio>

void f(int a)
{
char c = -37;
int retVal = isspace(c);
printf("%d\n", a); // No C6328 emitted.
}
```

The following code remediates this warning by using `static_cast`:
As a last resort when overflow can't happen, we can introduce a cast:

```cpp
void f( )
#include <cstdio>

void f(unsigned char a)
{
char c = -37;
int retVal = isspace(static_cast<unsigned char>(c));
printf("%hhd\n", static_cast<signed char>(a)); // No C6328 emitted.
}
```

## See also

[C6340](c6340.md)
62 changes: 59 additions & 3 deletions docs/code-quality/c6340.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
---
description: "Learn more about: Warning C6340"
title: Warning C6340
ms.date: 11/04/2016
f1_keywords: ["C6340"]
ms.date: 02/14/2024
f1_keywords: ["C6340", "FORMAT_SIGN_MISMATCH"]
helpviewer_keywords: ["C6340"]
ms.assetid: c4fe474f-5a27-4148-ba35-1ef021371e13
---
# Warning C6340

> Mismatch on sign: Incorrect type passed as parameter in call to function
> Mismatch on sign: '*type*' passed as _Param_(*number*) when some (signed|unsigned) type is required in call to '*function-name*'

## Remarks

This warning indicates that sign of the type required by the format specifier and sign of the type of the expression passed in don't match.
Using the wrong format specifier is undefined behavior. To fix the warning, make sure that the format specifiers match the types of the expressions passed in.

Code analysis name: `FORMAT_SIGN_MISMATCH`

## Example

```cpp
#include <cstdio>

void f(unsigned char a)
{
printf("%hhd\n", a); // C6340 emitted.
}
```
There are multiple ways to fix the undefined behavior. We can change the format specifier:

```cpp
#include <cstdio>

void f(unsigned char a)
{
printf("%hhu\n", a); // No C6340 emitted.
}
```

We can change the type of the expression:

```cpp
#include <cstdio>

void f(signed char a)
{
printf("%hhd\n", a); // No C6340 emitted.
}
```

As a last resort when overflow can't happen, we can introduce a cast:

```cpp
#include <cstdio>

void f(long long a)
{
printf("%d\n", static_cast<int>(a)); // No C6328 emitted.
}
```

## See also

[C6328](c6328.md)