Skip to content
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

Create rule S7119: Global should not depend on possibly not yet initialized variables CPP-5655 #4387

Merged
merged 28 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
babe35d
Create rule S7119
tomasz-kaminski-sonarsource Oct 8, 2024
6af48ec
Added explanation why this is an issue
tomasz-kaminski-sonarsource Oct 8, 2024
0b6938f
Fix - same file and constant init
tomasz-kaminski-sonarsource Oct 8, 2024
a78f754
Bd
tomasz-kaminski-sonarsource Oct 9, 2024
441aff3
Handling static data members
tomasz-kaminski-sonarsource Oct 9, 2024
cb6b683
How to fix it - static funcs
tomasz-kaminski-sonarsource Oct 9, 2024
e03ca17
Merge branch 'master' into rule/add-RSPEC-S7119
tomasz-kaminski-sonarsource Oct 9, 2024
9a9839a
Added inline variable section
tomasz-kaminski-sonarsource Oct 9, 2024
a74d407
Remove trailing spaces
tomasz-kaminski-sonarsource Oct 9, 2024
c18bf60
Grammarly pass
tomasz-kaminski-sonarsource Oct 9, 2024
070df16
Apply suggestions from code review
tomasz-kaminski-sonarsource Oct 10, 2024
942526d
Removed diff and reworked introduction
tomasz-kaminski-sonarsource Oct 10, 2024
ed05a40
Reworked variable instantiated
tomasz-kaminski-sonarsource Oct 10, 2024
27e79c3
Reworked multiple const copies to make it more linked to issue
tomasz-kaminski-sonarsource Oct 10, 2024
8b180b5
Added sections for constinit
tomasz-kaminski-sonarsource Oct 10, 2024
144a040
Merge branch 'master' into rule/add-RSPEC-S7119
tomasz-kaminski-sonarsource Oct 10, 2024
c1f71aa
Grammarly pass
tomasz-kaminski-sonarsource Oct 10, 2024
4237f39
Add links
tomasz-kaminski-sonarsource Oct 10, 2024
f18a99a
Nifty counter inspired
tomasz-kaminski-sonarsource Oct 10, 2024
622caa8
Grammarly pass
tomasz-kaminski-sonarsource Oct 10, 2024
4ae700a
Apply suggestions from code review
tomasz-kaminski-sonarsource Oct 14, 2024
b8cc4a6
Removed accidental unintended change
tomasz-kaminski-sonarsource Oct 14, 2024
32c0178
Used quotes in variable names in code examples
tomasz-kaminski-sonarsource Oct 14, 2024
254e183
Merge branch 'master' into rule/add-RSPEC-S7119
tomasz-kaminski-sonarsource Oct 14, 2024
41789bf
Apply suggestions from code review
tomasz-kaminski-sonarsource Oct 15, 2024
deeea2c
Merge branch 'master' into rule/add-RSPEC-S7119
tomasz-kaminski-sonarsource Oct 15, 2024
7534eec
Fix typo
frederic-tingaud-sonarsource Nov 4, 2024
cb702d2
Merge branch 'master' into rule/add-RSPEC-S7119
frederic-tingaud-sonarsource Nov 7, 2024
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
7 changes: 6 additions & 1 deletion rules/S5421/cfamily/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ unsigned** noncompliantPtr;
unsigned const* const* const compliantPtr = ...;
----

Some global variables defined in external libraries (such as ``++std::cout++``, ``++std::cin++``, ``++std::cerr++``) are acceptable to use, but you should have a good reason to create your own. If you use a global variable, ensure they can be safely accessed concurrently.
Some global variables defined in external libraries (such as ``++std::cout++``, ``++std::cin++``, ``++std::cerr++``) are acceptable to use, but you should have a good reason to create your own.
If you use a global variable, ensure they can be safely accessed concurrently, and there are no issues related to order of their initialization (see S7119).

Remember that it is much easier to maintain software without globals. Instead of such variables, it is better to design functions to take as input all the required variables. In addition to serving documentation, this also helps future refactoring and the evolution of the code.

Expand Down Expand Up @@ -72,6 +73,10 @@ unsigned volatile const* const gpio3 = ...; // Compliant, used for input only
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#i2-avoid-non-const-global-variables[I.2: Avoid non-`const` global variables]
* STIG Viewer - https://stigviewer.com/stig/application_security_and_development/2023-06-08/finding/V-222567[Application Security and Development: V-222567] - The application must not be vulnerable to race conditions.

=== Related rules

* S7119 detects order of initialization issues between global variables.

=== Articles & blog posts

* Stack Overflow - Answer by Gabriel Staples for https://stackoverflow.com/a/73027793/24103368[What is the point of declaring "const volatile int *p"?]
Expand Down
25 changes: 25 additions & 0 deletions rules/S7119/cfamily/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "Globals should not depend on possibly not yet initialized variables",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "30min"
},
"tags": [
"unpredictable"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7119",
"sqKey": "S7119",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "LOW"
},
"attribute": "LOGICAL"
}
}
Loading
Loading