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

Fix MISRA C++ 2023 rule 6.7.1 "Local variables shall not have static storage duration" #631

Closed
mohammedRafeeque opened this issue Jun 14, 2024 · 3 comments
Assignees
Labels
bug Something isn't working c++ C++ language generator
Milestone

Comments

@mohammedRafeeque
Copy link

Zserio version and language
Zserio: latest from master
Commit date: 6 days ago (6/4/2024 7:12:28 PM)
Commit hash: b8cc6c0

Describe the bug
2 Misra warnings are getting reported for rule MISRACPP2023-6_7_1-a

1 static MemoryResource* defaultResource = getNewDeleteResource();
2 static detail::NewDeleteResource newDeleteResource;

<style> </style>
1336 MISRACPP2023-6_7_1-a 2 Do not declare the 'newDeleteResource' local variable with static storage duration zserio_runtime/zserio/pmr/NewDeleteResource.cpp 10
1335 MISRACPP2023-6_7_1-a 2 Do not declare the 'defaultResource' local variable with static storage duration zserio_runtime/zserio/pmr/MemoryResource.cpp 17

Please check possibility of fixing the same.

Steps to reproduce the behavior:
SCA run using MISRA 2023 configuration.

Expected behavior
No SCA warnings for MISRA 2023 ruleset

Additional context
N.A

@Roland-Homeier @mikir FYI.

@mohammedRafeeque mohammedRafeeque added the bug Something isn't working label Jun 14, 2024
@mikir mikir added the c++ C++ language generator label Jun 14, 2024
@mikir mikir added this to the 2.15 milestone Jun 14, 2024
@mikir mikir changed the title Fix for MISRA C++ 2023 rule MISRACPP2023-6_7_1-a Do not declare the local variable with static storage duration Fix MISRA C++ 2023 rule 6.7.1 "Local variables shall not have static storage duration" Jun 14, 2024
@mohammedRafeeque
Copy link
Author

Observation : We cannot remove static as the function returning a reference . Can we declare static variable in .h file within class scope to have single instance ?
MemoryResource* getNewDeleteResource() noexcept
{
static detail::NewDeleteResource newDeleteResource;
return &newDeleteResource;
}

@Mi-La
Copy link
Contributor

Mi-La commented Aug 8, 2024

I'm afraid that we could get into problems with initialization of such static class variable. This way we ensure that get_default_resource returns pointer to already initialized resource even when it's used before main.

@Mi-La
Copy link
Contributor

Mi-La commented Aug 8, 2024

Note that Sonar rule M23_233 is enabled and we have 3 accepted issues as an exceptions:

Moudle Line Command Link
AnyHolder.h 29 static int currentTypeId; SonarCloud
MemoryResource.cpp 17 static MemoryResource* defaultResource = getNewDeleteResource(); SonarCloud
NewDeleteResource.cpp 10 static detail::NewDeleteResource newDeleteResource; SonarCloud

@Mi-La Mi-La closed this as completed Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working c++ C++ language generator
Projects
None yet
Development

No branches or pull requests

3 participants