Skip to content

Fix Duplicate and Incorrect Rule IDs #124

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
merged 21 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/validate-rules-csv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: ⚙️ Validate Rules CSV

on:
push:
branches:
- main
- "rc/**"
- next
pull_request:
branches:
- main
- "rc/**"
- next


jobs:
validate-rules-csv:
name: Validate Rules CSV
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Check Rules
shell: pwsh
run: scripts/util/Get-DuplicateRules.ps1 -Language 'all' -CIMode


1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"type": "pickString",
"options": [
"Allocations",
"Banned",
"BannedFunctions",
"BannedLibraries",
"BannedSyntax",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @id c/misra/precaution-include-guards-not-provided
* @name RULE-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once
* @name DIR-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once
* @description Using anything other than a standard include guard form can make code confusing and
* can lead to multiple or conflicting definitions.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-4-10
* @tags external/misra/id/dir-4-10
* correctness
* maintainability
* readability
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @id c/misra/std-lib-dynamic-memory-allocation-used
* @name RULE-4-12: Dynamic memory allocation shall not be used
* @name DIR-4-12: Dynamic memory allocation shall not be used
* @description Using dynamic memory allocation and deallocation can result to undefined behavior.
* This query is for the Standard Library Implementation. Any implementation outside it
* will require a separate query under the same directive.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-4-12
* @tags external/misra/id/dir-4-12
* security
* correctness
* maintainability
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @id c/misra/sections-of-code-shall-not-be-commented-out
* @name RULE-4-4: Sections of code should not be commented out
* @name DIR-4-4: Sections of code should not be commented out
* @description Commented out code may become out of date leading to developer confusion.
* @kind problem
* @precision high
* @problem.severity warning
* @tags external/misra/id/rule-4-4
* @tags external/misra/id/dir-4-4
* maintainability
* readability
* correctness
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @id c/misra/object-with-no-pointer-dereference-should-be-opaque
* @name RULE-4-8: The implementation of an object shall be hidden if a pointer to its structure or union is never dereferenced within a translation unit
* @name DIR-4-8: The implementation of an object shall be hidden if a pointer to its structure or union is never dereferenced within a translation unit
* @description If a pointer to a structure or union is never dereferenced within a translation
* unit, then the implementation of the object should be hidden to prevent
* unintentional changes.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-4-8
* @tags external/misra/id/dir-4-8
* readability
* maintainability
* external/misra/obligation/advisory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql
File renamed without changes.
File renamed without changes.

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 9 additions & 0 deletions change_notes/2022-11-04-refactor-dir-rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- The following rules have been renamed:
- RULE-4-4 has been renamed to DIR-4-4 to reflect correct naming as per
MISRA C:2012 standard.
- RULE-4-8 has been renamed to DIR-4-8 to reflect correct naming as per
MISRA C:2012 standard.
- RULE-4-10 has been renamed to DIR-4-10 to reflect correct naming as per
MISRA C:2012 standard.
- RULE-4-12 has been renamed to DIR-4-12 to reflect correct naming as per
MISRA C:2012 standard.
2 changes: 1 addition & 1 deletion cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ predicate isBannedQueryMetadata(Query query, string queryId, string ruleId) {
queryId =
// `@id` for the `stdLibDynamicMemoryAllocationUsed` query
"c/misra/std-lib-dynamic-memory-allocation-used" and
ruleId = "RULE-4-12"
ruleId = "DIR-4-12"
or
query =
// `Query` instance for the `octalConstantsUsed` query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ predicate isPointers1QueryMetadata(Query query, string queryId, string ruleId) {
queryId =
// `@id` for the `objectWithNoPointerDereferenceShouldBeOpaque` query
"c/misra/object-with-no-pointer-dereference-should-be-opaque" and
ruleId = "RULE-4-8"
ruleId = "DIR-4-8"
or
query =
// `Query` instance for the `pointerShouldPointToConstTypeWhenPossible` query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ predicate isPreprocessor2QueryMetadata(Query query, string queryId, string ruleI
queryId =
// `@id` for the `precautionIncludeGuardsNotProvided` query
"c/misra/precaution-include-guards-not-provided" and
ruleId = "RULE-4-10"
ruleId = "DIR-4-10"
}

module Preprocessor2Package {
Expand Down
2 changes: 1 addition & 1 deletion cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ predicate isSyntaxQueryMetadata(Query query, string queryId, string ruleId) {
queryId =
// `@id` for the `sectionsOfCodeShallNotBeCommentedOut` query
"c/misra/sections-of-code-shall-not-be-commented-out" and
ruleId = "RULE-4-4"
ruleId = "DIR-4-4"
or
query =
// `Query` instance for the `identifiersInTheSameNameSpaceUnambiguous` query
Expand Down
2 changes: 1 addition & 1 deletion rule_packages/c/Banned.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@
],
"title": "The Standard Library functions 'bsearch' and 'qsort' of 'stdlib.h' shall not be used"
},
"RULE-4-12": {
"DIR-4-12": {
"properties": {
"obligation": "required"
},
Expand Down
2 changes: 1 addition & 1 deletion rule_packages/c/Pointers1.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
],
"title": "The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist"
},
"RULE-4-8": {
"DIR-4-8": {
"properties": {
"obligation": "advisory"
},
Expand Down
2 changes: 1 addition & 1 deletion rule_packages/c/Preprocessor2.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
],
"title": "#undef should not be used"
},
"RULE-4-10": {
"DIR-4-10": {
"properties": {
"obligation": "required"
},
Expand Down
2 changes: 1 addition & 1 deletion rule_packages/c/Syntax.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
],
"title": "Octal and hexadecimal escape sequences shall be terminated"
},
"RULE-4-4": {
"DIR-4-4": {
"properties": {
"obligation": "advisory"
},
Expand Down
30 changes: 15 additions & 15 deletions rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -599,23 +599,23 @@ c,CERT-C,STR34-C,Yes,Rule,,,Cast characters to unsigned char before converting t
c,CERT-C,STR37-C,Yes,Rule,,,Arguments to character-handling functions must be representable as an unsigned char,,Strings2,Medium,
c,CERT-C,STR38-C,Yes,Rule,,,Do not confuse narrow and wide character strings and functions,,Strings3,Medium,
c,CERT-C,WIN30-C,OutOfScope,Rule,,,Properly pair allocation and deallocation functions,DCL54-CPP,,Easy,
c,MISRA-C-2012,RULE-1-1,No,Required,,,Any implementation-defined behaviour on which the output of the program depends shall be documented and understood,,,,
c,MISRA-C-2012,RULE-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium,
c,MISRA-C-2012,RULE-3-1,No,Required,,,All code shall be traceable to documented requirements,,,,
c,MISRA-C-2012,RULE-4-1,No,Required,,,Run-time failures shall be minimized,,,,
c,MISRA-C-2012,RULE-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import,
c,MISRA-C-2012,DIR-1-1,No,Required,,,Any implementation-defined behaviour on which the output of the program depends shall be documented and understood,,,,
c,MISRA-C-2012,DIR-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium,
c,MISRA-C-2012,DIR-3-1,No,Required,,,All code shall be traceable to documented requirements,,,,
c,MISRA-C-2012,DIR-4-1,No,Required,,,Run-time failures shall be minimized,,,,
c,MISRA-C-2012,DIR-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import,
c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium,
c,MISRA-C-2012,RULE-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import,
c,MISRA-C-2012,DIR-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import,
c,MISRA-C-2012,DIR-4-5,Yes,Advisory,,,Identifiers in the same name space with overlapping visibility should be typographically unambiguous,M2-10-1,Syntax,Easy,
c,MISRA-C-2012,RULE-4-6,Yes,Advisory,,,typedefs that indicate size and signedness should be used in place of the basic numerical types,,Types,Hard,
c,MISRA-C-2012,RULE-4-7,Yes,Required,,,"If a function returns error information, then that error information shall be tested",M0-3-2,Contracts,Import,
c,MISRA-C-2012,RULE-4-8,Yes,Advisory,,,"If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden",,Pointers1,Medium,
c,MISRA-C-2012,RULE-4-9,Yes,Advisory,,,A function should be used in preference to a function-like macro where they are interchangeable,,Preprocessor,Medium,
c,MISRA-C-2012,RULE-4-10,Yes,Required,,,Precautions shall be taken in order to prevent the contents of a header file being included more than once,M16-2-3,Preprocessor2,Medium,
c,MISRA-C-2012,RULE-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard,
c,MISRA-C-2012,RULE-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium,
c,MISRA-C-2012,RULE-4-13,Yes,Advisory,,,Functions which are designed to provide operations on a resource should be called in an appropriate sequence,,Contracts,Hard,
c,MISRA-C-2012,RULE-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard,
c,MISRA-C-2012,DIR-4-6,Yes,Advisory,,,typedefs that indicate size and signedness should be used in place of the basic numerical types,,Types,Hard,
c,MISRA-C-2012,DIR-4-7,Yes,Required,,,"If a function returns error information, then that error information shall be tested",M0-3-2,Contracts,Import,
c,MISRA-C-2012,DIR-4-8,Yes,Advisory,,,"If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden",,Pointers1,Medium,
c,MISRA-C-2012,DIR-4-9,Yes,Advisory,,,A function should be used in preference to a function-like macro where they are interchangeable,,Preprocessor,Medium,
c,MISRA-C-2012,DIR-4-10,Yes,Required,,,Precautions shall be taken in order to prevent the contents of a header file being included more than once,M16-2-3,Preprocessor2,Medium,
c,MISRA-C-2012,DIR-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard,
c,MISRA-C-2012,DIR-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium,
c,MISRA-C-2012,DIR-4-13,Yes,Advisory,,,Functions which are designed to provide operations on a resource should be called in an appropriate sequence,,Contracts,Hard,
c,MISRA-C-2012,DIR-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard,
c,MISRA-C-2012,RULE-1-1,Yes,Required,,,"The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation�s translation limits",,Language,Easy,
c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language,Easy,
c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language,Hard,
Expand Down
29 changes: 29 additions & 0 deletions scripts/PSCodingStandards/Get-RulesFromCSV.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-RulesFromCSV {
param(
[ValidateSet('c', 'cpp', 'all')]
[string]
$Language = 'all')


$csvFile = (Join-Path (Get-RepositoryRoot) "rules.csv")

Write-Host "Loading rules for language=$Language from file $csvFile..."

$csv = Import-Csv $csvFile
$filteredCSV = @()
# don't filter if not neeeded
if ($Language -eq 'all'){
$filteredCSV = $csv
}else{
foreach($rule in $csv){
if($rule.Language -eq $Language){
$filteredCSV += $rule
}
}
}

Write-Host "Loaded $($filteredCSV.Length) rules."

return $csv

}
47 changes: 47 additions & 0 deletions scripts/util/Get-DuplicateRules.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env pwsh
param(
[ValidateSet('c', 'cpp', 'all')]
[string]
$Language = 'all',
[switch]
$CIMode

)

Import-Module -Name "$PSScriptRoot/../PSCodingStandards/CodingStandards"

# load the rules.
$rules = Get-RulesFromCSV -Language $Language

# find out duplicates
$counter = @{}

foreach($rule in $rules){
$key = "$($rule.Language):$($rule.Standard):$($rule.ID)"
if($counter.Contains($key)){
$counter[$key] += $rule
}else{
$counter[$key] = @()
$counter[$key] += $rule
}
}

$duplicates = @()
$numDuplicates = 0

foreach($k in $counter.Keys){
if($counter[$k].Count -gt 1){
$numDuplicates = $numDuplicates + 1
foreach($v in $counter[$k]){
$duplicates += $v
}
}
}

$duplicates | Format-Table

if(($CIMode) -and ($numDuplicates -gt 0)){
throw "Found $numDuplicates duplicate Rule IDs"
}else{
Write-Host "Found $numDuplicates duplicate Rule IDs"
}