You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
16
16
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
+
17
19
Code analysis name: `BAD_CONCATENATION`
18
20
19
21
## Example
@@ -27,8 +29,8 @@ The following code generates warning C6059:
27
29
voidf( )
28
30
{
29
31
char szTarget[MAX];
30
-
char *szState ="Washington";
31
-
char *szCity="Redmond, ";
32
+
const char *szState ="Washington";
33
+
const char *szCity="Redmond, ";
32
34
33
35
strncpy(szTarget, szCity, MAX);
34
36
szTarget[MAX -1] = '\0';
@@ -46,8 +48,8 @@ To correct this warning, use the correct number of characters to concatenate as
46
48
void f( )
47
49
{
48
50
char szTarget[MAX];
49
-
char *szState ="Washington";
50
-
char *szCity="Redmond, ";
51
+
const char *szState ="Washington";
52
+
const char *szCity="Redmond, ";
51
53
52
54
strncpy(szTarget, szCity, MAX);
53
55
szTarget[MAX -1] = '\0';
@@ -63,8 +65,8 @@ To correct this warning using the safe string manipulation functions `strncpy_s`
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
+
#defineMAX 25
110
+
111
+
voidf( )
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
0 commit comments