Skip to content

Commit e5174a6

Browse files
Merge pull request #4976 from MicrosoftDocs/main638454333234551668sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 20b3126 + 3d2d2e2 commit e5174a6

File tree

2 files changed

+97
-16
lines changed

2 files changed

+97
-16
lines changed

docs/code-quality/c6328.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,66 @@
11
---
22
description: "Learn more about: Warning C6328"
33
title: Warning C6328
4-
ms.date: 09/15/2022
4+
ms.date: 02/14/2024
55
f1_keywords: ["C6328", "FORMAT_SIZE_MISMATCH", "__WARNING_FORMAT_SIZE_MISMATCH"]
66
helpviewer_keywords: ["C6328"]
77
ms.assetid: e25b00fa-d344-4dc9-b322-b4f1ae06f315
88
---
99
# Warning C6328
1010

11-
> Size mismatch: '*type*' passed as parameter '*number*' when '*type*' is required in call to '*function-name*'
11+
> Size mismatch: '*type*' passed as _Param_(*number*) when '*type*' is required in call to '*function-name*'
1212
1313
## Remarks
1414

15-
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.
16-
17-
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.
15+
This warning indicates that type required by the format specifier and the type of the expression passed in don't match.
16+
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.
1817

1918
Code analysis name: `FORMAT_SIZE_MISMATCH`
2019

2120
## Example
2221

23-
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):
22+
```cpp
23+
#include <cstdio>
24+
25+
void f(long long a)
26+
{
27+
printf("%d\n", a); // C6328 emitted.
28+
}
29+
```
30+
31+
There are multiple ways to fix the undefined behavior. We can change the format specifier:
32+
33+
```cpp
34+
#include <cstdio>
35+
36+
void f(long long a)
37+
{
38+
printf("%lld\n", a); // No C6328 emitted.
39+
}
40+
```
41+
42+
We can change the type of the expression:
2443

2544
```cpp
26-
void f()
45+
#include <cstdio>
46+
47+
void f(int a)
2748
{
28-
char c = -37;
29-
int retVal = isspace(c);
49+
printf("%d\n", a); // No C6328 emitted.
3050
}
3151
```
3252
33-
The following code remediates this warning by using `static_cast`:
53+
As a last resort when overflow can't happen, we can introduce a cast:
3454
3555
```cpp
36-
void f( )
56+
#include <cstdio>
57+
58+
void f(unsigned char a)
3759
{
38-
char c = -37;
39-
int retVal = isspace(static_cast<unsigned char>(c));
60+
printf("%hhd\n", static_cast<signed char>(a)); // No C6328 emitted.
4061
}
4162
```
63+
64+
## See also
65+
66+
[C6340](c6340.md)

docs/code-quality/c6340.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
---
22
description: "Learn more about: Warning C6340"
33
title: Warning C6340
4-
ms.date: 11/04/2016
5-
f1_keywords: ["C6340"]
4+
ms.date: 02/14/2024
5+
f1_keywords: ["C6340", "FORMAT_SIGN_MISMATCH"]
66
helpviewer_keywords: ["C6340"]
77
ms.assetid: c4fe474f-5a27-4148-ba35-1ef021371e13
88
---
99
# Warning C6340
1010

11-
> Mismatch on sign: Incorrect type passed as parameter in call to function
11+
> Mismatch on sign: '*type*' passed as _Param_(*number*) when some (signed|unsigned) type is required in call to '*function-name*'
12+
13+
## Remarks
14+
15+
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.
16+
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.
17+
18+
Code analysis name: `FORMAT_SIGN_MISMATCH`
19+
20+
## Example
21+
22+
```cpp
23+
#include <cstdio>
24+
25+
void f(unsigned char a)
26+
{
27+
printf("%hhd\n", a); // C6340 emitted.
28+
}
29+
```
30+
There are multiple ways to fix the undefined behavior. We can change the format specifier:
31+
32+
```cpp
33+
#include <cstdio>
34+
35+
void f(unsigned char a)
36+
{
37+
printf("%hhu\n", a); // No C6340 emitted.
38+
}
39+
```
40+
41+
We can change the type of the expression:
42+
43+
```cpp
44+
#include <cstdio>
45+
46+
void f(signed char a)
47+
{
48+
printf("%hhd\n", a); // No C6340 emitted.
49+
}
50+
```
51+
52+
As a last resort when overflow can't happen, we can introduce a cast:
53+
54+
```cpp
55+
#include <cstdio>
56+
57+
void f(long long a)
58+
{
59+
printf("%d\n", static_cast<int>(a)); // No C6328 emitted.
60+
}
61+
```
62+
63+
## See also
64+
65+
[C6328](c6328.md)
66+
67+

0 commit comments

Comments
 (0)