-
Notifications
You must be signed in to change notification settings - Fork 67
Implement banned2 package, rule 21-24 ban rand() and srand(). #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelRFairhurst
merged 4 commits into
main
from
michaelrfairhurst/implement-banned2-rule-package-rule-21-24
Oct 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3520d1b
Implement banned2 package, rule 21-24 ban rand() and srand().
MichaelRFairhurst b9474d5
Update rule_packages/c/Banned2.json
lcartey aa646b8
Merge branch 'main' into michaelrfairhurst/implement-banned2-rule-pac…
lcartey ee78b9b
Fix query metadata
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
c/misra/src/rules/RULE-21-24/CallToBannedRandomFunction.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @id c/misra/call-to-banned-random-function | ||
* @name RULE-21-24: The random number generator functions of <stdlib.h> shall not be used | ||
* @description The standard functions rand() and srand() will not give high quality random results | ||
* in all implementations and is thus banned. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity warning | ||
* @tags external/misra/id/rule-21-24 | ||
* security | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
|
||
from FunctionCall call, string name | ||
where | ||
not isExcluded(call, Banned2Package::callToBannedRandomFunctionQuery()) and | ||
name = ["rand", "srand"] and | ||
call.getTarget().hasGlobalOrStdName(name) | ||
select call, "Call to banned random number generation function '" + name + "'." |
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/RULE-21-24/CallToBannedRandomFunction.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:5:3:5:7 | call to srand | Call to banned random number generation function 'srand'. | | ||
| test.c:6:11:6:14 | call to rand | Call to banned random number generation function 'rand'. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-21-24/CallToBannedRandomFunction.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "stdlib.h" | ||
|
||
void f() { | ||
// rand() is banned -- and thus, so is srand(). | ||
srand(0); // NON-COMPLIANT | ||
int x = rand(); // NON-COMPLIANT | ||
|
||
// Other functions from stdlib are not banned by this rule. | ||
x = abs(-4); // COMPLIANT | ||
getenv("ENV_VAR"); // COMPLIANT | ||
} |
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/c/Banned2.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype Banned2Query = TCallToBannedRandomFunctionQuery() | ||
|
||
predicate isBanned2QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
query = | ||
// `Query` instance for the `callToBannedRandomFunction` query | ||
Banned2Package::callToBannedRandomFunctionQuery() and | ||
queryId = | ||
// `@id` for the `callToBannedRandomFunction` query | ||
"c/misra/call-to-banned-random-function" and | ||
ruleId = "RULE-21-24" and | ||
category = "required" | ||
} | ||
|
||
module Banned2Package { | ||
Query callToBannedRandomFunctionQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `callToBannedRandomFunction` query | ||
TQueryC(TBanned2PackageQuery(TCallToBannedRandomFunctionQuery())) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"MISRA-C-2012": { | ||
"RULE-21-24": { | ||
"properties": { | ||
"obligation": "required" | ||
}, | ||
"queries": [ | ||
{ | ||
"description": "The standard functions rand() and srand() will not give high quality random results in all implementations and is thus banned.", | ||
"kind": "problem", | ||
"name": "The random number generator functions of <stdlib.h> shall not be used", | ||
"precision": "very-high", | ||
"severity": "warning", | ||
"short_name": "CallToBannedRandomFunction", | ||
"tags": [ | ||
"security", | ||
"external/misra/c/2012/amendment3" | ||
] | ||
} | ||
], | ||
"title": "The random number generator functions of <stdlib.h> shall not be used" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.