Skip to content

Commit 7dd7bf3

Browse files
committed
CPP: Add a test of placement new in CWE-772 (this case came up recently but has already been fixed).
1 parent c974693 commit 7dd7bf3

File tree

1 file changed

+15
-0
lines changed
  • cpp/ql/test/query-tests/Security/CWE/CWE-772/semmle/tests-memory

1 file changed

+15
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-772/semmle/tests-memory/test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,20 @@ void test28()
555555
dostuff();
556556
}
557557

558+
// placement new
559+
void* operator new(size_t, void* p);
560+
561+
class MyClass29
562+
{
563+
};
564+
565+
void test29()
566+
{
567+
void *buf = malloc(sizeof(MyClass29)); // GOOD (freed)
568+
MyClass29 *p1 = new (buf) MyClass29(); // GOOD (not really an allocation)
569+
free(buf);
570+
}
571+
558572
// run tests
559573
int main(int argc, char *argv[])
560574
{
@@ -585,4 +599,5 @@ int main(int argc, char *argv[])
585599
test26();
586600
test27();
587601
test28();
602+
test29();
588603
}

0 commit comments

Comments
 (0)