Skip to content

Commit 86ee5fe

Browse files
committed
C++: More test cases and correct an existing one.
1 parent e5e8a1b commit 86ee5fe

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
| test2.cpp:35:2:35:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:35:36:35:43 | password | this source. |
2-
| test2.cpp:36:2:36:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:36:37:36:45 | thepasswd | this source. |
3-
| test2.cpp:41:2:41:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:41:41:41:53 | passwd_config | this source. |
4-
| test2.cpp:45:2:45:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:45:39:45:49 | call to getPassword | this source. |
5-
| test2.cpp:53:3:53:9 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:50:18:50:25 | password | this source. |
1+
| test2.cpp:43:2:43:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:43:36:43:43 | password | this source. |
2+
| test2.cpp:44:2:44:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:44:37:44:45 | thepasswd | this source. |
3+
| test2.cpp:49:2:49:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:49:41:49:53 | password_path | this source. |
4+
| test2.cpp:50:2:50:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:50:41:50:53 | passwd_config | this source. |
5+
| test2.cpp:54:2:54:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:54:41:54:52 | widepassword | this source. |
6+
| test2.cpp:55:2:55:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:55:40:55:51 | widepassword | this source. |
7+
| test2.cpp:57:2:57:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:57:39:57:49 | call to getPassword | this source. |
8+
| test2.cpp:65:3:65:9 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:62:18:62:25 | password | this source. |
9+
| test2.cpp:79:2:79:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:79:36:79:43 | password | this source. |
10+
| test2.cpp:84:4:84:10 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:84:50:84:63 | passwd_config2 | this source. |
611
| test.cpp:45:3:45:7 | call to fputs | This write into file 'file' may contain unencrypted data from $@ | test.cpp:45:9:45:19 | thePassword | this source. |
712
| test.cpp:70:35:70:35 | call to operator<< | This write into file 'mystream' may contain unencrypted data from $@ | test.cpp:70:38:70:48 | thePassword | this source. |
813
| test.cpp:73:37:73:41 | call to write | This write into file 'mystream' may contain unencrypted data from $@ | test.cpp:73:43:73:53 | thePassword | this source. |

cpp/ql/test/query-tests/Security/CWE/CWE-311/semmle/tests/test2.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
#define FILE int
33

4+
typedef unsigned long size_t;
5+
6+
FILE *fopen(const char *filename, const char *mode);
7+
int snprintf(char *s, size_t n, const char *format, ...);
48
int fprintf(FILE *stream, const char *format, ...);
59
char *strcpy(char *s1, const char *s2);
610

@@ -12,18 +16,22 @@ struct myStruct
1216
char *password;
1317
char *thepasswd;
1418
char *accountkey;
19+
wchar_t *widepassword;
1520

1621
// encrypted
1722
char password_hash[64];
1823
char *encrypted_passwd;
1924

2025
// not sensitive
2126
char *password_file;
27+
char *password_path;
2228
int num_passwords;
29+
int *password_tries;
2330
bool have_passwd;
2431

2532
// dubious
2633
char *passwd_config;
34+
char *passwd_config2;
2735
};
2836

2937
char *getPassword();
@@ -38,12 +46,16 @@ void tests(FILE *log, myStruct &s)
3846
fprintf(log, "password_hash = %s\n", s.password_hash); // GOOD
3947
fprintf(log, "encrypted_passwd = %s\n", s.encrypted_passwd); // GOOD
4048
fprintf(log, "password_file = %s\n", s.password_file); // GOOD
49+
fprintf(log, "password_path = %s\n", s.password_path); // GOOD [FALSE POSITIVE]
4150
fprintf(log, "passwd_config = %s\n", s.passwd_config); // DUBIOUS [REPORTED]
4251
fprintf(log, "num_passwords = %i\n", s.num_passwords); // GOOD
52+
fprintf(log, "password_tries = %i\n", *(s.password_tries)); // GOOD
4353
fprintf(log, "have_passwd = %i\n", s.have_passwd); // GOOD
54+
fprintf(log, "widepassword = %ls\n", s.widepassword); // BAD
55+
fprintf(log, "widepassword = %S\n", s.widepassword); // BAD
4456

45-
fprintf(log, "getPassword() = %i\n", getPassword()); // BAD
46-
fprintf(log, "getPasswordHash() = %i\n", getPasswordHash()); // GOOD
57+
fprintf(log, "getPassword() = %s\n", getPassword()); // BAD
58+
fprintf(log, "getPasswordHash() = %s\n", getPasswordHash()); // GOOD
4759
fprintf(log, "getPasswordMaxChars() = %i\n", getPasswordMaxChars()); // GOOD
4860

4961
{
@@ -63,4 +75,20 @@ void tests(FILE *log, myStruct &s)
6375
strcpy(buf, s.password_hash);
6476
fprintf(log, "buf = %s\n", buf); // GOOD
6577
}
78+
79+
fprintf(log, "password = %p\n", s.password); // GOOD [FALSE POSITIVE]
80+
81+
{
82+
if (fopen(s.passwd_config2, "rt") == 0)
83+
{
84+
fprintf(log, "could not open file '%s'.\n", s.passwd_config2); // GOOD [FALSE POSITIVE]
85+
}
86+
}
87+
88+
{
89+
char buffer[1024];
90+
91+
snprintf(buffer, 1024, "password = %s", s.password);
92+
fprintf(log, "log: %s", buffer); // BAD [NOT DETECTED]
93+
}
6694
}

0 commit comments

Comments
 (0)