Skip to content

Commit ea7e892

Browse files
committed
CPP: Add a test similar to the false positive in arvidn/libtorrent.
1 parent a31794f commit ea7e892

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cpp/ql/test/query-tests/Critical/NewFree/NewFreeMismatch.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
| test.cpp:235:2:235:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:227:7:227:13 | new | new |
1313
| test.cpp:239:2:239:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:228:7:228:17 | new[] | new[] |
1414
| test.cpp:272:3:272:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:265:7:265:13 | new | new |
15+
| test.cpp:425:2:425:31 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:425:20:425:29 | call to getPointer | malloc |

cpp/ql/test/query-tests/Critical/NewFree/test.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,56 @@ void test12(bool cond)
371371
free(z); // GOOD
372372
}
373373
}
374+
375+
// ---
376+
377+
class MyBuffer13
378+
{
379+
public:
380+
MyBuffer13(int size)
381+
{
382+
buffer = (char *)malloc(size * sizeof(char));
383+
}
384+
385+
~MyBuffer13()
386+
{
387+
free(buffer); // GOOD
388+
}
389+
390+
char *getBuffer() // note: this should not be considered an allocation function
391+
{
392+
return buffer;
393+
}
394+
395+
private:
396+
char *buffer;
397+
};
398+
399+
class MyPointer13
400+
{
401+
public:
402+
MyPointer13(char *_pointer) : pointer(_pointer)
403+
{
404+
}
405+
406+
MyPointer13(MyBuffer13 &buffer) : pointer(buffer.getBuffer())
407+
{
408+
}
409+
410+
char *getPointer() // note: this should not be considered an allocation function
411+
{
412+
return pointer;
413+
}
414+
415+
private:
416+
char *pointer;
417+
};
418+
419+
void test13()
420+
{
421+
MyBuffer13 myBuffer(100);
422+
MyPointer13 myPointer2(myBuffer);
423+
MyPointer13 myPointer3(new char[100]);
424+
425+
delete myPointer2.getPointer(); // GOOD [FALSE POSITIVE]
426+
}

0 commit comments

Comments
 (0)