Skip to content

Commit ec2e4f4

Browse files
committed
C++: Add more test cases, inspired by FPs on LGTM with the query.
1 parent 74957dc commit ec2e4f4

File tree

2 files changed

+62
-45
lines changed

2 files changed

+62
-45
lines changed

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/ImproperNullTermination.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
| test.cpp:285:10:285:15 | buffer | Variable $@ may not be null terminated. | test.cpp:282:8:282:13 | buffer | buffer |
1818
| test.cpp:302:10:302:16 | buffer2 | Variable $@ may not be null terminated. | test.cpp:297:8:297:14 | buffer2 | buffer2 |
1919
| test.cpp:314:10:314:15 | buffer | Variable $@ may not be null terminated. | test.cpp:310:8:310:13 | buffer | buffer |
20+
| test.cpp:328:10:328:15 | buffer | Variable $@ may not be null terminated. | test.cpp:325:8:325:13 | buffer | buffer |
2021
| test.cpp:336:18:336:23 | buffer | Variable $@ may not be null terminated. | test.cpp:335:8:335:13 | buffer | buffer |
2122
| test.cpp:355:11:355:16 | buffer | Variable $@ may not be null terminated. | test.cpp:350:8:350:13 | buffer | buffer |
2223
| test.cpp:364:11:364:16 | buffer | Variable $@ may not be null terminated. | test.cpp:359:8:359:13 | buffer | buffer |
24+
| test.cpp:392:11:392:16 | buffer | Variable $@ may not be null terminated. | test.cpp:381:8:381:13 | buffer | buffer |
25+
| test.cpp:410:11:410:16 | buffer | Variable $@ may not be null terminated. | test.cpp:397:8:397:13 | buffer | buffer |
2326
| test.cpp:421:19:421:25 | buffer2 | Variable $@ may not be null terminated. | test.cpp:419:8:419:14 | buffer2 | buffer2 |
2427
| test.cpp:448:17:448:22 | buffer | Variable $@ may not be null terminated. | test.cpp:446:8:446:13 | buffer | buffer |
2528
| test.cpp:454:18:454:23 | buffer | Variable $@ may not be null terminated. | test.cpp:452:8:452:13 | buffer | buffer |
29+
| test.cpp:502:10:502:18 | after_ptr | Variable $@ may not be null terminated. | test.cpp:497:9:497:17 | after_ptr | after_ptr |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/test.cpp

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ size_t strlen(const char *s);
66
char *strcpy(char *s1, const char *s2);
77
char *strcat(char *s1, const char *s2);
88
char *strdup(const char *s1);
9-
9+
long int strtol(const char* nptr, char** endptr, int base);
1010
void *malloc(size_t size);
1111
void *memset(void *s, int c, size_t n);
1212
void *memcpy(void *s1, const void *s2, size_t n);
@@ -226,7 +226,7 @@ void test_readlink(int fd, const char *path, size_t sz)
226226
void doNothing(char *data) { };
227227
void doNothing2(const char *data);
228228
void clearBuffer(char *data, size_t size);
229-
229+
char *id(char *data) { return data; }
230230

231231
void test_strcat()
232232
{
@@ -321,12 +321,12 @@ void test_strcat()
321321
strcat(buffer, "content"); // GOOD
322322
}
323323

324+
{
325+
char buffer[1024];
324326

325-
326-
327-
328-
329-
327+
clearBuffer(id(buffer), 1024);
328+
strcat(buffer, "content"); // GOOD [FALSE POSITIVE]
329+
}
330330
}
331331

332332
void test_strlen(bool cond1, bool cond2)
@@ -364,52 +364,52 @@ void test_strlen(bool cond1, bool cond2)
364364
strlen(buffer); // BAD
365365
}
366366

367+
{
368+
char buffer[1024];
367369

370+
if (cond1)
371+
{
372+
buffer[0] = 0;
373+
} else {
374+
buffer[0] = 0;
375+
}
368376

377+
strlen(buffer); // GOOD
378+
}
369379

380+
{
381+
char buffer[1024];
382+
int init = 0;
370383

384+
if (cond1)
385+
{
386+
buffer[0] = 0;
387+
init = 1;
388+
}
371389

390+
if (init != 0)
391+
{
392+
strlen(buffer); // GOOD [FALSE POSITIVE]
393+
}
394+
}
372395

396+
{
397+
char buffer[1024];
398+
int init = 0;
373399

400+
if (cond1)
401+
{
402+
buffer[0] = 0;
403+
init = 1;
404+
}
374405

375-
376-
377-
378-
379-
380-
381-
382-
383-
384-
385-
386-
387-
388-
389-
390-
391-
392-
393-
394-
395-
396-
397-
398-
399-
400-
401-
402-
403-
404-
405-
406-
407-
408-
409-
410-
411-
412-
406+
if (init == 0)
407+
{
408+
// ...
409+
} else {
410+
strlen(buffer); // GOOD [FALSE POSITIVE]
411+
}
412+
}
413413
}
414414

415415
void test_strcpy()
@@ -489,3 +489,16 @@ void test_read_fread(int read_src, FILE *s)
489489
strlen(buffer); // GOOD
490490
}
491491
}
492+
493+
void test_strtol()
494+
{
495+
{
496+
char buffer[100];
497+
char *after_ptr;
498+
long int num;
499+
500+
strcpy(buffer, "123abc");
501+
num = strtol("123abc", &after_ptr, 10);
502+
strlen(after_ptr); // GOOD [FALSE POSITIVE]
503+
}
504+
}

0 commit comments

Comments
 (0)