cpp - Using the return value of a strcpy or related string copy function in an if statement

This commit is contained in:
Raul Garcia
2018-12-14 15:42:49 -08:00
parent 54493eb990
commit 16f2bacf4d
8 changed files with 332 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
| test.c:33:9:33:14 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.c:37:10:37:15 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.c:41:9:41:14 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.c:45:27:45:32 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.c:49:9:49:15 | call to strncpy | Incorrect use of function strncpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:75:9:75:14 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:79:10:79:15 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:83:9:83:14 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:87:27:87:32 | call to strcpy | Incorrect use of function strcpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:91:9:91:37 | call to wcscpy | Incorrect use of function wcscpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:95:9:95:14 | call to wcscpy | Incorrect use of function wcscpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:99:9:99:15 | call to _mbscpy | Incorrect use of function _mbscpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:103:9:103:15 | call to strncpy | Incorrect use of function strncpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:107:9:107:15 | call to wcsncpy | Incorrect use of function wcsncpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:111:9:111:16 | call to _mbsncpy | Incorrect use of function _mbsncpy. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:115:9:115:18 | call to _strncpy_l | Incorrect use of function _strncpy_l. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:119:9:119:18 | call to _wcsncpy_l | Incorrect use of function _wcsncpy_l. Verify the logic and replace with a secure string copy function, or a string comparison function. |
| test.cpp:123:9:123:18 | call to _mbsncpy_l | Incorrect use of function _mbsncpy_l. Verify the logic and replace with a secure string copy function, or a string comparison function. |

View File

@@ -0,0 +1 @@
Likely Bugs/Likely Typos/UsingStrcpyInConditional.ql

View File

@@ -0,0 +1,70 @@
typedef unsigned int size_t;
typedef int* locale_t;
char* strcpy(char* destination, const char* source)
{
return destination;
}
char* strncpy(char* destination, const char* source, size_t count)
{
return destination;
}
int SomeFunction()
{
return 1;
}
int SomeFunctionThatTakesString(char* destination)
{
return 1;
}
int strcmp(char* destination, const char* source)
{
return 1;
}
void PositiveCases()
{
char szbuf1[100];
char szbuf2[100];
if (strcpy(szbuf1, "test")) // Bug, direct usage
{
}
if (!strcpy(szbuf1, "test")) // Bug, unary binary operator
{
}
if (strcpy(szbuf1, "test") == 0) // Bug, equality operator
{
}
if (SomeFunction() && strcpy(szbuf1, "test")) // Bug, binary logical operator
{
}
if (strncpy(szbuf1, "test", 100)) // Bug
{
}
}
void NegativeCases()
{
char szbuf1[100];
if (SomeFunction())
{
}
if (SomeFunctionThatTakesString(strcpy(szbuf1, "test")))
{
}
if (strcmp(szbuf1, "test"))
{
}
}

View File

@@ -0,0 +1,149 @@
typedef unsigned long size_t;
typedef int* locale_t;
char* strcpy(char* destination, const char* source)
{
return destination;
}
wchar_t* wcscpy(wchar_t* destination, const wchar_t* source)
{
return destination;
}
unsigned char* _mbscpy(unsigned char* destination, const unsigned char* source)
{
return destination;
}
char* strncpy(char* destination, const char* source, size_t count)
{
return destination;
}
wchar_t* wcsncpy(wchar_t* destination, const wchar_t* source, size_t count)
{
return destination;
}
unsigned char* _mbsncpy(unsigned char* destination, const unsigned char* source, size_t count)
{
return destination;
}
char* _strncpy_l(char* destination, const char* source, size_t count, locale_t locale)
{
return destination;
}
wchar_t* _wcsncpy_l(wchar_t* destination, const wchar_t* source, size_t count, locale_t locale)
{
return destination;
}
unsigned char* _mbsncpy_l(unsigned char* destination, const unsigned char* source, size_t count, locale_t locale)
{
return destination;
}
int SomeFunction()
{
return 1;
}
int SomeFunctionThatTakesString(char* destination)
{
return 1;
}
int strcmp(char* destination, const char* source)
{
return 1;
}
int strcpy_s(char* destination, size_t dest_size, const char* source)
{
return 1;
}
#define WCSCPY_6324(x,y) wcscpy(x,y)
void PositiveCases()
{
char szbuf1[100];
char szbuf2[100];
wchar_t wscbuf1[100];
wchar_t wscbuf2[100];
unsigned char mbcbuf1[100];
unsigned char mbcbuf2[100];
locale_t x;
*x = 0;
if (strcpy(szbuf1, "test")) // Bug, direct usage
{
}
if (!strcpy(szbuf1, "test")) // Bug, unary binary operator
{
}
if (strcpy(szbuf1, "test") == 0) // Bug, equality operator
{
}
if (SomeFunction() && strcpy(szbuf1, "test")) // Bug, binary logical operator
{
}
if (WCSCPY_6324(wscbuf1, wscbuf2)) // Bug, using a macro
{
}
if (wcscpy(wscbuf1, wscbuf2)) // Bug
{
}
if (_mbscpy(mbcbuf1, mbcbuf2)) // Bug
{
}
if (strncpy(szbuf1, "test", 100)) // Bug
{
}
if (wcsncpy(wscbuf1, wscbuf2, 100)) // Bug
{
}
if (_mbsncpy(mbcbuf1, (const unsigned char*)"test", 100)) // Bug
{
}
if (_strncpy_l(szbuf1, "test", 100, x)) // Bug
{
}
if (_wcsncpy_l(wscbuf1, wscbuf2, 100, x)) // Bug
{
}
if (_mbsncpy_l(mbcbuf1, (const unsigned char*)"test", 100, x)) //Bug
{
}
}
void NegativeCases()
{
char szbuf1[100];
if (SomeFunction())
{
}
if (SomeFunctionThatTakesString(strcpy(szbuf1, "test")))
{
}
if (strcmp(szbuf1, "test"))
{
}
if (strcpy_s(szbuf1, 100, "test"))
{
}
}