Skip to content

Commit 9dc3c2a

Browse files
Use range analysis to detect realloc() where size may be zero, vs, is exactly zero.
1 parent 46b272a commit 9dc3c2a

16 files changed

+113
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @id c/misra/call-to-realloc-with-size-zero
3-
* @name RULE-1-5: Disallowed size argument value equal to zero in call to realloc
2+
* @id c/misra/size-in-realloc-call-is-zero
3+
* @name RULE-1-5: Size argument value in realloc call is equal zero
44
* @description Invoking realloc with a size argument set to zero is implementation-defined behavior
55
* and declared as an obsolete feature in C18.
66
* @kind problem
@@ -15,11 +15,12 @@
1515
import cpp
1616
import codingstandards.c.misra
1717
import semmle.code.cpp.rangeanalysis.new.RangeAnalysis
18+
import codingstandards.cpp.Realloc
1819

19-
from FunctionCall call, Expr arg
20+
from ReallocCall call
2021
where
21-
not isExcluded(call, Language4Package::callToReallocWithSizeZeroQuery()) and
22-
call.getTarget().hasGlobalOrStdName("realloc") and
23-
arg = call.getArgument(1) and
24-
upperBound(arg) = 0
25-
select arg, "Calling realloc with size zero results in implementation-defined behavior."
22+
not isExcluded(call, Language4Package::sizeInReallocCallIsZeroQuery()) and
23+
call.sizeIsExactlyZero()
24+
select call,
25+
"Size argument '$@' may equal zero in realloc call, resulting in obsolescent and/or implementation-defined behavior.",
26+
call.getSizeArgument(), call.getSizeArgument().toString()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/size-in-realloc-call-may-be-zero
3+
* @name RULE-1-5: Size argument value in realloc call may equal zero
4+
* @description Invoking realloc with a size argument set to zero is implementation-defined behavior
5+
* and declared as an obsolete feature in C18.
6+
* @kind problem
7+
* @precision medium
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-1-5
10+
* correctness
11+
* external/misra/c/2012/amendment3
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.Realloc
18+
19+
from ReallocCall call
20+
where
21+
not isExcluded(call, Language4Package::sizeInReallocCallMayBeZeroQuery()) and
22+
call.sizeMayBeZero() and
23+
not call.sizeIsExactlyZero()
24+
select call,
25+
"Size argument '$@' equals zero in realloc call, resulting in obsolescent and/or implementation-defined behavior.",
26+
call.getSizeArgument(), call.getSizeArgument().toString()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.c:36:3:36:6 | call to gets | Call to obsolescent function 'gets'. |
1+
| test.c:37:3:37:6 | call to gets | Call to obsolescent function 'gets'. |

c/misra/test/rules/RULE-1-5/CallToReallocWithSizeZero.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-1-5/CallToReallocWithSizeZero.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| test.c:21:1:21:14 | #define true 3 | Invalid define of boolean standard macro 'true'. |
2-
| test.c:22:1:22:15 | #define false 3 | Invalid define of boolean standard macro 'false'. |
3-
| test.c:23:1:23:18 | #define bool int * | Invalid define of boolean standard macro 'bool'. |
4-
| test.c:24:1:24:11 | #undef true | Invalid undefine of boolean standard macro 'true'. |
5-
| test.c:25:1:25:12 | #undef false | Invalid undefine of boolean standard macro 'false'. |
6-
| test.c:26:1:26:11 | #undef bool | Invalid undefine of boolean standard macro 'bool'. |
1+
| test.c:22:1:22:14 | #define true 3 | Invalid define of boolean standard macro 'true'. |
2+
| test.c:23:1:23:15 | #define false 3 | Invalid define of boolean standard macro 'false'. |
3+
| test.c:24:1:24:18 | #define bool int * | Invalid define of boolean standard macro 'bool'. |
4+
| test.c:25:1:25:11 | #undef true | Invalid undefine of boolean standard macro 'true'. |
5+
| test.c:26:1:26:12 | #undef false | Invalid undefine of boolean standard macro 'false'. |
6+
| test.c:27:1:27:11 | #undef bool | Invalid undefine of boolean standard macro 'bool'. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:14:3:14:9 | call to realloc | Size argument '$@' may equal zero in realloc call, resulting in obsolescent and/or implementation-defined behavior. | test.c:14:14:14:14 | 0 | 0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-5/SizeInReallocCallIsZero.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:15:3:15:9 | call to realloc | Size argument '$@' equals zero in realloc call, resulting in obsolescent and/or implementation-defined behavior. | test.c:15:14:15:15 | p0 | p0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-5/SizeInReallocCallMayBeZero.ql

0 commit comments

Comments
 (0)