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 {
22
22
23
23
Instruction getAUseInstruction ( Instruction insn ) { result = insn .getAUse ( ) .getUse ( ) }
24
24
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
+
25
33
from MemsetCallInstruction memset , SizedBufferMustWriteSideEffectInstruction sei
26
34
where
27
35
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 )
32
40
select memset ,
33
41
"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) {
85
85
int func4 (void ) {
86
86
char pw1a [PW_SIZE ];
87
87
use_pw (pw1a );
88
- __builtin_memset (pw1a + 3 , 0 , PW_SIZE - 3 ); // BAD
88
+ __builtin_memset (pw1a + 3 , 0 , PW_SIZE - 3 ); // BAD [NOT DETECTED]
89
89
return 0 ;
90
90
}
91
91
@@ -115,7 +115,7 @@ int func5(void) {
115
115
int func7 (void ) {
116
116
char pw1a [PW_SIZE ];
117
117
use_pw (pw1a );
118
- __builtin_memset (& pw1a [3 ], 0 , PW_SIZE - 5 ); // BAD
118
+ __builtin_memset (& pw1a [3 ], 0 , PW_SIZE - 5 ); // BAD [NOT DETECTED]
119
119
return 0 ;
120
120
}
121
121
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ void func3(unsigned long long sz) {
48
48
// x64 msvc v19.22: deleted
49
49
void func4 (unsigned long long sz) {
50
50
char buff[128 ];
51
- memset (buff, 0 , PW_SIZE); // BAD
51
+ memset (buff, 0 , PW_SIZE); // BAD [NOT DETECTED]
52
52
strcpy (buff, " Hello" );
53
53
}
54
54
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)
2
1
| MemsetMayBeDeleted.c:19:2:19:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
3
2
| MemsetMayBeDeleted.c:29:2:29:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
4
3
| 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. |
9
6
| 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. |
11
7
| MemsetMayBeDeleted.cpp:71:5:71:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
12
8
| 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