File tree Expand file tree Collapse file tree 3 files changed +74
-1
lines changed
test/query-tests/Critical/NewFree Expand file tree Collapse file tree 3 files changed +74
-1
lines changed Original file line number Diff line number Diff line change 1+ # Improvements to C/C++ analysis
2+
3+ ## General improvements
4+
5+ ## New queries
6+
7+ | ** Query** | ** Tags** | ** Purpose** |
8+ | -----------------------------| -----------| --------------------------------------------------------------------|
9+
10+ ## Changes to existing queries
11+
12+ | ** Query** | ** Expected impact** | ** Change** |
13+ | ----------------------------| ------------------------| ------------------------------------------------------------------|
14+ | Mismatching new/free or malloc/delete (` cpp/new-free-mismatch ` ) | Fewer false positive results | Fixed an issue where functions were being identified as allocation functions inappropriately. Also affects ` cpp/new-array-delete-mismatch ` and ` cpp/new-delete-array-mismatch ` . |
15+
16+ ## Changes to QL libraries
Original file line number Diff line number Diff line change 33 */
44import cpp
55import semmle.code.cpp.controlflow.SSA
6+ import semmle.code.cpp.dataflow.DataFlow
67
78/**
89 * Holds if `alloc` is a use of `malloc` or `new`. `kind` is
@@ -46,7 +47,10 @@ predicate allocExprOrIndirect(Expr alloc, string kind) {
4647 alloc .( FunctionCall ) .getTarget ( ) = rtn .getEnclosingFunction ( ) and
4748 (
4849 allocExprOrIndirect ( rtn .getExpr ( ) , kind ) or
49- allocReaches0 ( rtn .getExpr ( ) , _, kind )
50+ exists ( Expr e |
51+ allocExprOrIndirect ( e , kind ) and
52+ DataFlow:: localFlow ( DataFlow:: exprNode ( e ) , DataFlow:: exprNode ( rtn .getExpr ( ) ) )
53+ )
5054 )
5155 )
5256}
Original file line number Diff line number Diff 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 myPointer3.getPointer (); // GOOD
426+ }
You can’t perform that action at this time.
0 commit comments