Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d0c84dc

Browse filesBrowse files
Merge pull request #738 from github/michaelrfairhurst/implement-banned2-rule-package-rule-21-24
Implement banned2 package, rule 21-24 ban rand() and srand().
2 parents b476450 + ee78b9b commit d0c84dc
Copy full SHA for d0c84dc

File tree

Expand file treeCollapse file tree

7 files changed

+90
-0
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+90
-0
lines changed
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/call-to-banned-random-function
3+
* @name RULE-21-24: The random number generator functions of <stdlib.h> shall not be used
4+
* @description The standard functions rand() and srand() will not give high quality random results
5+
* in all implementations and are therefore banned.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-21-24
10+
* security
11+
* external/misra/c/2012/amendment3
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from FunctionCall call, string name
19+
where
20+
not isExcluded(call, Banned2Package::callToBannedRandomFunctionQuery()) and
21+
name = ["rand", "srand"] and
22+
call.getTarget().hasGlobalOrStdName(name)
23+
select call, "Call to banned random number generation function '" + name + "'."
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:5:3:5:7 | call to srand | Call to banned random number generation function 'srand'. |
2+
| test.c:6:11:6:14 | call to rand | Call to banned random number generation function 'rand'. |
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-21-24/CallToBannedRandomFunction.ql

‎c/misra/test/rules/RULE-21-24/test.c

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "stdlib.h"
2+
3+
void f() {
4+
// rand() is banned -- and thus, so is srand().
5+
srand(0); // NON-COMPLIANT
6+
int x = rand(); // NON-COMPLIANT
7+
8+
// Other functions from stdlib are not banned by this rule.
9+
x = abs(-4); // COMPLIANT
10+
getenv("ENV_VAR"); // COMPLIANT
11+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Banned2Query = TCallToBannedRandomFunctionQuery()
7+
8+
predicate isBanned2QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `callToBannedRandomFunction` query
11+
Banned2Package::callToBannedRandomFunctionQuery() and
12+
queryId =
13+
// `@id` for the `callToBannedRandomFunction` query
14+
"c/misra/call-to-banned-random-function" and
15+
ruleId = "RULE-21-24" and
16+
category = "required"
17+
}
18+
19+
module Banned2Package {
20+
Query callToBannedRandomFunctionQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `callToBannedRandomFunction` query
24+
TQueryC(TBanned2PackageQuery(TCallToBannedRandomFunctionQuery()))
25+
}
26+
}

‎cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

Copy file name to clipboardExpand all lines: cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import cpp
33
import codingstandards.cpp.exclusions.RuleMetadata
44
//** Import packages for this language **/
55
import Banned
6+
import Banned2
67
import BitfieldTypes
78
import BitfieldTypes2
89
import Concurrency1
@@ -78,6 +79,7 @@ import Types2
7879
/** The TQuery type representing this language * */
7980
newtype TCQuery =
8081
TBannedPackageQuery(BannedQuery q) or
82+
TBanned2PackageQuery(Banned2Query q) or
8183
TBitfieldTypesPackageQuery(BitfieldTypesQuery q) or
8284
TBitfieldTypes2PackageQuery(BitfieldTypes2Query q) or
8385
TConcurrency1PackageQuery(Concurrency1Query q) or
@@ -153,6 +155,7 @@ newtype TCQuery =
153155
/** The metadata predicate * */
154156
predicate isQueryMetadata(Query query, string queryId, string ruleId, string category) {
155157
isBannedQueryMetadata(query, queryId, ruleId, category) or
158+
isBanned2QueryMetadata(query, queryId, ruleId, category) or
156159
isBitfieldTypesQueryMetadata(query, queryId, ruleId, category) or
157160
isBitfieldTypes2QueryMetadata(query, queryId, ruleId, category) or
158161
isConcurrency1QueryMetadata(query, queryId, ruleId, category) or

‎rule_packages/c/Banned2.json

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-21-24": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "The standard functions rand() and srand() will not give high quality random results in all implementations and are therefore banned.",
10+
"kind": "problem",
11+
"name": "The random number generator functions of <stdlib.h> shall not be used",
12+
"precision": "very-high",
13+
"severity": "warning",
14+
"short_name": "CallToBannedRandomFunction",
15+
"tags": [
16+
"security",
17+
"external/misra/c/2012/amendment3"
18+
]
19+
}
20+
],
21+
"title": "The random number generator functions of <stdlib.h> shall not be used"
22+
}
23+
}
24+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.