CPP: Add a test similar to the false positive in arvidn/libtorrent.

This commit is contained in:
Geoffrey White
2019-03-20 15:35:58 +00:00
parent a31794f20c
commit ea7e8927fe
2 changed files with 54 additions and 0 deletions

View File

@@ -12,3 +12,4 @@
| 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 |
| 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[] |
| 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 |
| 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 |

View File

@@ -371,3 +371,56 @@ void test12(bool cond)
free(z); // GOOD
}
}
// ---
class MyBuffer13
{
public:
MyBuffer13(int size)
{
buffer = (char *)malloc(size * sizeof(char));
}
~MyBuffer13()
{
free(buffer); // GOOD
}
char *getBuffer() // note: this should not be considered an allocation function
{
return buffer;
}
private:
char *buffer;
};
class MyPointer13
{
public:
MyPointer13(char *_pointer) : pointer(_pointer)
{
}
MyPointer13(MyBuffer13 &buffer) : pointer(buffer.getBuffer())
{
}
char *getPointer() // note: this should not be considered an allocation function
{
return pointer;
}
private:
char *pointer;
};
void test13()
{
MyBuffer13 myBuffer(100);
MyPointer13 myPointer2(myBuffer);
MyPointer13 myPointer3(new char[100]);
delete myPointer2.getPointer(); // GOOD [FALSE POSITIVE]
}