Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
initial pass of printf related diagnostics
  • Loading branch information
Rastaban committed Mar 8, 2023
commit 85035f53d936bcd6536c91cc0f54f95c290a5907
1 change: 1 addition & 0 deletions docs/code-quality/c6063.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ void f( )

## See also

[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)
2 changes: 1 addition & 1 deletion docs/code-quality/c6065.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ int PrintDiagnostic(UNICODE_STRING u)

## See also

[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions)\
[UNICODE_STRING](/windows/win32/api/ntdef/ns-ntdef-_unicode_string.md)\
[ANSI_STRING/_STRING](/windows/win32/api/ntdef/ns-ntdef-string.md)\
[C4313](../error-messages/compiler-warnings/C4313.md)
23 changes: 9 additions & 14 deletions docs/code-quality/c6066.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
description: "Learn more about: Warning C6066"
title: Warning C6066
ms.date: 10/04/2022
ms.date: 3/02/2023
f1_keywords: ["C6066", "NON_POINTER_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_NON_POINTER_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6066"]
ms.assetid: f03c9cf1-d8eb-4731-a66a-da7c924616fb
---
# Warning C6066

Expand Down Expand Up @@ -32,8 +31,7 @@ void f( )

void g( int i )
{
int result;
result = scanf( "%d", i ); // warning C6066
int result = scanf( "%d", i ); // warning C6066
// code ...
}
```
Expand All @@ -47,15 +45,12 @@ To correct this warning, the following code passes correct parameters to the `sp
void f( )
{
char buff[MAX];

sprintf( buff, "%s %p %d", "Hello, World!", buff, MAX ); // pass buff
// code ...
}
void g( int i )
{
int result;
// code ...
result = scanf( "%d", &i ); // pass the address of i
int result = scanf( "%d", &i ); // pass the address of i
// code ...
}
```
Expand All @@ -66,15 +61,12 @@ The following code uses safe string manipulation functions `sprintf_s` and `scan
void f( )
{
char buff[MAX];

sprintf_s( buff, sizeof(buff), "%s %p %d", "Hello, World!", buff, MAX );
// code ...
}
void g( int i )
{
int result;
// code ...
result = scanf_s( "%d", &i );
int result = scanf_s( "%d", &i );
// code ...
}
```
Expand All @@ -83,5 +75,8 @@ This warning is typically reported because an integer has been used for a `%p` f

## See also

- [`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)
- [`scanf_s`, `_scanf_s_l`, `wscanf_s`, `_wscanf_s_l`](../c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l.md)
[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[`scanf_s`, `_scanf_s_l`, `wscanf_s`, `_wscanf_s_l`](../c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l.md)\
[C4313](../error-messages/compiler-warnings/C4313.md)\
[C4477](../error-messages/compiler-warnings/C4477.md)
16 changes: 10 additions & 6 deletions docs/code-quality/c6067.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
description: "Learn more about: Warning C6067"
title: Warning C6067
ms.date: 11/04/2016
ms.date: 3/02/2023
f1_keywords: ["C6067", "NON_STRING_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_NON_STRING_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6067"]
ms.assetid: 6fbaee53-daaa-4ba5-9b11-2a8066d86240
---
# Warning C6067

> Parameter '*number*' in call to '*function*' must be the address of the string

## Remarks

This warning indicates a mismatch between the format specifier and the function parameter. Even though the warning suggests using the address of the string, you must check the type of parameter a function expects before correcting the problem. For example, a `%s` specification for `printf` requires a string argument, but a `%s` specification in `scanf` requires an address of the string.

This defect is likely to cause a crash or corruption of some form.
Expand All @@ -26,7 +27,7 @@ The following code generates this warning because an integer is passed instead o
void f_defective()
{
char *str = "Hello, World!";
printf("String:\n %s", 1); // warning
printf("String:\n %s", 1);
// code ...
}
```
Expand Down Expand Up @@ -98,6 +99,9 @@ void f_safe()

## See also

- [sprintf\_s, \_sprintf\_s\_l, swprintf\_s, \_swprintf\_s\_l](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)
- [printf, \_printf\_l, wprintf, \_wprintf\_l](../c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l.md)
- [scanf\_s, \_scanf\_s\_l, wscanf\_s, \_wscanf\_s\_l](../c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l.md)
[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[sprintf\_s, \_sprintf\_s\_l, swprintf\_s, \_swprintf\_s\_l](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[printf, \_printf\_l, wprintf, \_wprintf\_l](../c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l.md)\
[scanf\_s, \_scanf\_s\_l, wscanf\_s, \_wscanf\_s\_l](../c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l.md)\
[C4313](../error-messages/compiler-warnings/C4313.md)\
[C4477](../error-messages/compiler-warnings/C4477.md)
11 changes: 5 additions & 6 deletions docs/code-quality/c6270.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
---
description: "Learn more about: Warning C6270"
title: Warning C6270
ms.date: 10/03/2022
ms.date: 3/03/2023
f1_keywords: ["C6270", "MISSING_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_MISSING_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6270"]
ms.assetid: 34467f6e-98cf-489c-ae5e-c08a744d86c3
---
# Warning C6270

> Missing float argument to '*function-name*': add a float argument corresponding to conversion specifier '*number*'

This warning indicates that not enough arguments are provided to match a format string; at least one of the missing arguments is a floating-point number.

## Remarks

This defect can lead to crashes, in addition to potentially incorrect output.
This warning indicates that not enough arguments are provided to match a format string; at least one of the missing arguments is a floating-point number. This defect can lead to crashes, in addition to potentially incorrect output.

Code analysis name: `MISSING_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION`

Expand Down Expand Up @@ -42,5 +39,7 @@ void f()

## See also

[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf`, `_sprintf_l`, `swprintf`, `_swprintf_l`, `__swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)\
[`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)
[`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[C4473](../error-messages/compiler-warnings/C4473.md)
21 changes: 10 additions & 11 deletions docs/code-quality/c6271.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
description: "Learn more about: Warning C6271"
title: Warning C6271
ms.date: 11/04/2016
ms.date: 3/06/2023
f1_keywords: ["C6271", "EXTRA_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_EXTRA_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6271"]
ms.assetid: 24703b17-5bdc-4f97-a56a-b2ea48bacc43
---
# Warning C6271

> Extra argument passed to '*function*': parameter '*number*' is not used by the format string
> Extra argument passed to '*function*'

## Remarks

Expand All @@ -22,44 +21,44 @@ The following sample code generates this warning:

```cpp
#include <stdio.h>
#include <string.h>

void f()
{
char buff[5];

sprintf(buff,"%d",1,2);
sprintf(buff,"%d",1, 2);
}
```

To correct this warning, use the following sample code:
To correct this warning, remove the unused parameter or modify the format string to take it into account:

```cpp
#include <stdio.h>
#include <string.h>

void f()
{
char buff[5];

sprintf(buff,"%d, %d",1,2);
sprintf(buff,"%d, %d",1, 2);
}
```

The following sample code calls the safe string manipulation function, `sprintf_s`, to correct this warning:

```cpp
#include <stdio.h>
#include <string.h>

void f()
{
char buff[5];

sprintf_s( buff, 5,"%s %d", 1,2 ); //safe version
sprintf_s( buff, 5,"%d %d", 1, 2 ); //safe version
}
```

## See also

[sprintf, _sprintf_l, swprintf, _swprintf_l, \__swprintf_l](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)
[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf`, `_sprintf_l`, `swprintf`, `_swprintf_l`, `__swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)\
[`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[C4474](../error-messages/compiler-warnings/C4474.md)
16 changes: 10 additions & 6 deletions docs/code-quality/c6272.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
---
description: "Learn more about: Warning C6272"
title: Warning C6272
ms.date: 09/15/2022
ms.date: 03/07/2023
f1_keywords: ["C6272", "NON_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_NON_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6272"]
ms.assetid: b63937ac-fbb2-45ec-936a-641c156e6355
---
# Warning C6272

> Non-float passed as argument '*number*' when float is required in call to '*function-name*'

This warning indicates that the format string specifies that a float is required, for example, a `%f` or `%g` specification for `printf,` but a non-float such as an integer or string is being passed.

## Remarks

This defect is likely to result in incorrect output and, in certain circumstances, could result in a crash.
This warning indicates that the format string specifies that a float is required, for example, a `%f` or `%g` specification for `printf,` but a non-float such as an integer or string is being passed. This defect can lead to crashes, in addition to potentially incorrect output.

Code analysis name: `NON_FLOAT_ARGUMENT_TO_FORMAT_FUNCTION`

Expand All @@ -31,7 +28,7 @@ void f()
}
```

To correct this warning, use `%i` instead of `%f` specification as shown in the following code:
This can be resolved by either changing the format specifier or modifying the parameters passed to the function. In this example we correct this warning by using `%i` instead of `%f`.

```cpp
void f()
Expand All @@ -41,3 +38,10 @@ void f()
sprintf_s(buff, sizeof(buff), "%s %i", "a", i);
}
```

## See also

[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf`, `_sprintf_l`, `swprintf`, `_swprintf_l`, `__swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)\
[`sprintf_s`, `_sprintf_s_l`, `swprintf_s`, `_swprintf_s_l`](../c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l.md)\
[C4477](../error-messages/compiler-warnings/C4477.md)
16 changes: 7 additions & 9 deletions docs/code-quality/c6273.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
---
description: "Learn more about: Warning C6273"
title: Warning C6273
ms.date: 11/04/2016
ms.date: 03/07/2023
f1_keywords: ["C6273", "NON_INTEGER_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_NON_INTEGER_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6273"]
ms.assetid: e780e504-8b8d-4d61-b15f-4859133134ad
---
# Warning C6273

> Non-integer passed as parameter '*number*' when integer is required in call to '*function*': if a pointer value is being passed, %p should be used
> Non-integer passed as parameter '*number*' when an integer is required in call to '*function*'

## Remarks

This warning indicates that the format string specifies an integer, for example, a `%d`, length or precedence specification for `printf` but a non-integer such as a **`float`**, string, or **`struct`** is being passed as a parameter. This defect is likely to result in incorrect output.
This warning indicates that the format string specifier in a `printf` like function is expecting an integer integer type, but a non-integer such as a **`float`**, string, or **`struct`** is being passed instead. This includes type specifiers like `%d`, as well as width and precision specifier like `%*.*f`. This defect is likely to result in incorrect output.

Code analysis name: `NON_INTEGER_ARGUMENT_TO_FORMAT_FUNCTION`

Expand All @@ -22,7 +21,6 @@ The following code generates this warning because an integer is required instead

```cpp
#include <stdio.h>
#include <string.h>

void f_defective()
{
Expand All @@ -33,11 +31,10 @@ void f_defective()
}
```

The following code uses an integer cast to correct this warning:
The following code uses an integer cast to correct this warning. Alternatively it could have corrected the warning by modifying the format specifier to match the type.

```cpp
#include <stdio.h>
#include <string.h>

void f_corrected()
{
Expand All @@ -52,7 +49,6 @@ The following code uses safe string manipulation function, `sprintf_s`, to corre

```cpp
#include <stdio.h>
#include <string.h>

void f_safe()
{
Expand All @@ -67,4 +63,6 @@ This warning isn't applicable on Windows 9x and Windows NT version 4 because %p

## See also

[`sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)
[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)\
[C4477](../error-messages/compiler-warnings/C4477.md)
14 changes: 6 additions & 8 deletions docs/code-quality/c6274.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
description: "Learn more about: Warning C6274"
title: Warning C6274
ms.date: 11/04/2016
ms.date: 03/07/2023
f1_keywords: ["C6274", "NON_CHAR_ARGUMENT_TO_FORMAT_FUNCTION", "__WARNING_NON_CHAR_ARGUMENT_TO_FORMAT_FUNCTION"]
helpviewer_keywords: ["C6274"]
ms.assetid: d32f1c67-edf1-4d03-9103-133272631948
---
# Warning C6274

Expand All @@ -22,12 +21,10 @@ The following code generates this warning:

```cpp
#include <stdio.h>
#include <string.h>

void f(char str[])
{
char buff[5];

sprintf(buff,"%c",str);
}
```
Expand All @@ -36,12 +33,10 @@ To correct this warning, use the following code:

```cpp
#include <stdio.h>
#include <string.h>

void f(char str[])
{
char buff[5];

sprintf(buff,"%c",str[0]);
}
```
Expand All @@ -50,12 +45,15 @@ The following code uses safe string manipulation function, `sprintf_s`, to corre

```cpp
#include <stdio.h>
#include <string.h>

void f(char str[])
{
char buff[5];

sprintf_s(buff,5,"%c", str[0]);
}
```

[Format specification syntax: printf and wprintf functions](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md)\
[`sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l`](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)\
[C4477](../error-messages/compiler-warnings/C4477.md)\
[C4313](../error-messages/compiler-warnings/C4474.md)
Loading