|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: Warning C6328"
|
3 | 3 | title: Warning C6328
|
4 |
| -ms.date: 09/15/2022 |
| 4 | +ms.date: 02/14/2024 |
5 | 5 | f1_keywords: ["C6328", "FORMAT_SIZE_MISMATCH", "__WARNING_FORMAT_SIZE_MISMATCH"]
|
6 | 6 | helpviewer_keywords: ["C6328"]
|
7 | 7 | ms.assetid: e25b00fa-d344-4dc9-b322-b4f1ae06f315
|
8 | 8 | ---
|
9 | 9 | # Warning C6328
|
10 | 10 |
|
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*' |
12 | 12 |
|
13 | 13 | ## Remarks
|
14 | 14 |
|
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. |
18 | 17 |
|
19 | 18 | Code analysis name: `FORMAT_SIZE_MISMATCH`
|
20 | 19 |
|
21 | 20 | ## Example
|
22 | 21 |
|
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: |
24 | 43 |
|
25 | 44 | ```cpp
|
26 |
| -void f() |
| 45 | +#include <cstdio> |
| 46 | + |
| 47 | +void f(int a) |
27 | 48 | {
|
28 |
| - char c = -37; |
29 |
| - int retVal = isspace(c); |
| 49 | + printf("%d\n", a); // No C6328 emitted. |
30 | 50 | }
|
31 | 51 | ```
|
32 | 52 |
|
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: |
34 | 54 |
|
35 | 55 | ```cpp
|
36 |
| -void f( ) |
| 56 | +#include <cstdio> |
| 57 | +
|
| 58 | +void f(unsigned char a) |
37 | 59 | {
|
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. |
40 | 61 | }
|
41 | 62 | ```
|
| 63 | + |
| 64 | +## See also |
| 65 | + |
| 66 | +[C6340](c6340.md) |
0 commit comments