File tree Expand file tree Collapse file tree 4 files changed +17
-13
lines changed
src/Likely Bugs/Memory Management
test/query-tests/Likely Bugs/Memory Management/MemsetMayBeDeleted Expand file tree Collapse file tree 4 files changed +17
-13
lines changed Original file line number Diff line number Diff line change @@ -22,12 +22,20 @@ class MemsetCallInstruction extends CallInstruction {
2222
2323Instruction getAUseInstruction ( Instruction insn ) { result = insn .getAUse ( ) .getUse ( ) }
2424
25+ predicate pointsIntoStack ( Instruction instr ) {
26+ instr .( VariableAddressInstruction ) .getIRVariable ( ) instanceof IRAutomaticVariable
27+ or
28+ pointsIntoStack ( instr .( CopyInstruction ) .getSourceValue ( ) )
29+ or
30+ pointsIntoStack ( instr .( ConvertInstruction ) .getUnary ( ) )
31+ }
32+
2533from MemsetCallInstruction memset , SizedBufferMustWriteSideEffectInstruction sei
2634where
2735 sei .getPrimaryInstruction ( ) = memset and
28- forall ( Instruction use | use = getAUseInstruction + ( sei ) | use instanceof ChiInstruction ) and
29- exists ( Instruction def | memset .getPositionalArgument ( 0 ) = getAUseInstruction + ( def ) |
30- def instanceof UninitializedInstruction
31- )
36+ // The first argument to memset must reside on the stack
37+ pointsIntoStack ( valueNumber ( memset .getPositionalArgument ( 0 ) ) . getAnInstruction ( ) ) and
38+ // The result of memset may not be subsequently used
39+ forall ( Instruction use | use = getAUseInstruction + ( sei ) | use instanceof ChiInstruction )
3240select memset ,
3341 "Call to " + memset .getStaticCallTarget ( ) .getName ( ) + " may be deleted by the compiler."
Original file line number Diff line number Diff line change @@ -85,7 +85,7 @@ int func3(void) {
8585int func4 (void ) {
8686char pw1a [PW_SIZE ];
8787use_pw (pw1a );
88- __builtin_memset (pw1a + 3 , 0 , PW_SIZE - 3 ); // BAD
88+ __builtin_memset (pw1a + 3 , 0 , PW_SIZE - 3 ); // BAD [NOT DETECTED]
8989return 0 ;
9090}
9191
@@ -115,7 +115,7 @@ int func5(void) {
115115int func7 (void ) {
116116char pw1a [PW_SIZE ];
117117use_pw (pw1a );
118- __builtin_memset (& pw1a [3 ], 0 , PW_SIZE - 5 ); // BAD
118+ __builtin_memset (& pw1a [3 ], 0 , PW_SIZE - 5 ); // BAD [NOT DETECTED]
119119return 0 ;
120120}
121121
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ void func3(unsigned long long sz) {
4848// x64 msvc v19.22: deleted
4949void func4 (unsigned long long sz) {
5050 char buff[128 ];
51- memset (buff, 0 , PW_SIZE); // BAD
51+ memset (buff, 0 , PW_SIZE); // BAD [NOT DETECTED]
5252 strcpy (buff, " Hello" );
5353}
5454
Original file line number Diff line number Diff line change 1- WARNING: Unused predicate insnDominates (/mnt/c/code/ql/cpp/ql/src/Likely Bugs/Memory Management/MemsetMayBeDeleted.ql:27,19-32)
21| MemsetMayBeDeleted.c:19:2:19:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
32| MemsetMayBeDeleted.c:29:2:29:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
43| MemsetMayBeDeleted.c:39:2:39:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
5- | MemsetMayBeDeleted.c:59:2:59:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
6- | MemsetMayBeDeleted.c:79:2:79:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
7- | MemsetMayBeDeleted.c:109:2:109:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
8- | MemsetMayBeDeleted.c:129:2:129:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
4+ | MemsetMayBeDeleted.c:68:2:68:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
5+ | MemsetMayBeDeleted.c:138:2:138:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
96| MemsetMayBeDeleted.cpp:43:5:43:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
10- | MemsetMayBeDeleted.cpp:51:5:51:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
117| MemsetMayBeDeleted.cpp:71:5:71:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
128| MemsetMayBeDeleted.cpp:79:5:79:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
You can’t perform that action at this time.
0 commit comments