Skip to content

Commit 3917622

Browse files
Merge pull request #4881 from MicrosoftDocs/main638399896479454875sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents cd63e91 + e8cbb47 commit 3917622

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

docs/code-quality/c6059.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: Warning C6059"
33
title: Warning C6059
4-
ms.date: 10/04/2022
4+
ms.date: 12/14/2023
55
f1_keywords: ["C6059", "BAD_CONCATENATION", "__WARNING_BAD_CONCATENATION"]
66
helpviewer_keywords: ["C6059"]
77
ms.assetid: 343a4cd1-048a-4edf-bb4b-187097bb6093
@@ -14,6 +14,8 @@ ms.assetid: 343a4cd1-048a-4edf-bb4b-187097bb6093
1414

1515
This warning indicates that a call to a string concatenation function is probably passing an incorrect value for the number of characters to concatenate. This defect might cause an exploitable buffer overrun or crash. A common cause of this defect is passing the buffer size (instead of the remaining number of characters in the buffer) to the string manipulation function.
1616

17+
This warning helps identify the common error of sending the size of the target buffer instead of the size of the data. It does so by detecting when the size used to allocate the buffer is passed, unchanged, to the function putting data in the buffer.
18+
1719
Code analysis name: `BAD_CONCATENATION`
1820

1921
## Example
@@ -27,8 +29,8 @@ The following code generates warning C6059:
2729
void f( )
2830
{
2931
char szTarget[MAX];
30-
char *szState ="Washington";
31-
char *szCity="Redmond, ";
32+
const char *szState ="Washington";
33+
const char *szCity="Redmond, ";
3234

3335
strncpy(szTarget, szCity, MAX);
3436
szTarget[MAX -1] = '\0';
@@ -46,8 +48,8 @@ To correct this warning, use the correct number of characters to concatenate as
4648
void f( )
4749
{
4850
char szTarget[MAX];
49-
char *szState ="Washington";
50-
char *szCity="Redmond, ";
51+
const char *szState ="Washington";
52+
const char *szCity="Redmond, ";
5153
5254
strncpy(szTarget, szCity, MAX);
5355
szTarget[MAX -1] = '\0';
@@ -63,8 +65,8 @@ To correct this warning using the safe string manipulation functions `strncpy_s`
6365

6466
void f( )
6567
{
66-
char *szState ="Washington";
67-
char *szCity="Redmond, ";
68+
const char *szState ="Washington";
69+
const char *szCity="Redmond, ";
6870

6971
size_t nTargetSize = strlen(szState) + strlen(szCity) + 1;
7072
char *szTarget= new char[nTargetSize];
@@ -77,6 +79,48 @@ void f( )
7779
}
7880
```
7981
82+
## Heuristics
83+
84+
This analysis detects when the target buffer size is passed unmodified into the length parameter of the string manipulation function. This warning isn't given if some other value is passed as the length parameter, even if that value is incorrect.
85+
86+
Consider the following code that generates warning C6059:
87+
88+
```cpp
89+
#include <string.h>
90+
#define MAX 25
91+
92+
void f( )
93+
{
94+
char szTarget[MAX];
95+
const char *szState ="Washington";
96+
const char *szCity="Redmond, ";
97+
98+
strncpy(szTarget, szCity, MAX);
99+
szTarget[MAX -1] = '\0';
100+
strncat(szTarget, szState, MAX); // wrong size
101+
// code ...
102+
}
103+
```
104+
105+
The warning goes away by changing the `MAX` argument to `strncat` to `MAX - 1`, even though the length calculation is still incorrect.
106+
107+
```cpp
108+
#include <string.h>
109+
#define MAX 25
110+
111+
void f( )
112+
{
113+
char szTarget[MAX];
114+
const char *szState ="Washington";
115+
const char *szCity="Redmond, ";
116+
117+
strncpy(szTarget, szCity, MAX);
118+
szTarget[MAX -1] = '\0';
119+
strncat(szTarget, szState, MAX - 1); // wrong size, but no warning
120+
// code ...
121+
}
122+
```
123+
80124
## See also
81125
82126
- [`strncpy_s`, `_strncpy_s_l`, `wcsncpy_s`, `_wcsncpy_s_l`, `_mbsncpy_s`, `_mbsncpy_s_l`](../c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l.md)

0 commit comments

Comments
 (0)