Skip to content

Commit 9058dd3

Browse files
authored
Merge pull request #2841 from corob-msft/cr-vs4296
Fix from VS4296 PR in C6054 sample
2 parents 70d9738 + c249d60 commit 9058dd3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

docs/code-quality/c6054.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: C6054
3-
ms.date: 11/04/2016
3+
description: "Reference guide to Microsoft C++ code analysis warning C6054."
4+
ms.date: 04/22/2020
45
ms.topic: reference
56
f1_keywords: ["C6054"]
67
helpviewer_keywords: ["C6054"]
@@ -10,38 +11,41 @@ ms.assetid: d573a5c1-7e74-402b-92e6-8085f967aa50
1011

1112
> warning C6054: string \<variable> may not be zero-terminated
1213
13-
This warning indicates that a function that requires zero-terminated string was passed a non-zero terminated string. A function that expects a zero-terminated string will go beyond the end of the buffer to look for the zero. This defect might cause an exploitable buffer overrun error or crash. The program should make sure that the string ends with a zero.
14+
## Remarks
15+
16+
This warning indicates that a function that requires a zero-terminated string was passed a non-zero terminated string. A function that expects a zero-terminated string could look for the zero beyond the end of the buffer. This defect might cause an exploitable buffer overrun error or crash. The program should make sure the string passed in ends with a zero.
1417

1518
## Example
1619

1720
The following code generates this warning:
1821

1922
```cpp
20-
23+
// C6054_bad.cpp
24+
// Compile using: cl /W4 /EHsc /c /analyze C6054_bad.cpp
2125
#include <sal.h>
2226

2327
void func( _In_z_ wchar_t* wszStr );
2428

2529
void g ( )
2630
{
27-
wchar_t wcArray[200];
31+
wchar_t wcArray[200] = { 'h', 'e', 'l', 'l', 'o' };
2832
func(wcArray); // Warning C6054
2933
}
3034
```
3135
3236
To correct this warning, null-terminate `wcArray` before calling function `func()` as shown in the following sample code:
3337
3438
```cpp
35-
39+
// C6054_good.cpp
40+
// Compile using: cl /W4 /EHsc /c /analyze C6054_good.cpp
3641
#include <sal.h>
3742
3843
void func( _In_z_ wchar_t* wszStr );
3944
40-
void g( )
45+
void g ( )
4146
{
42-
wchar_t wcArray[200];
43-
wcArray[0]= '\0';
44-
func(wcArray);
47+
wchar_t wcArray[200] = { 'h', 'e', 'l', 'l', 'o', '\0' };
48+
func(wcArray); // OK
4549
}
4650
```
4751

0 commit comments

Comments
 (0)