typedefs that indicate size and signedness should be used in place of the basic types
When you use code that has types that cpp:S813 does not like, you will get this issue. And most code does not respect that rule.
int fd = open("name", O_OPEN);
What shoud I do with this int?
Misra rules tell no preferred mitigation.
My solutions are all bad:
- use int32_t instead (int may be 16 bit, it is not portable)
- use specializes typedef:
typedef int stdFd; (this requires tons of typedef, might be useful in some cases though)
- use generic typedef:
typedef int sqPleaseShutUpInt; (this just makes the code harder to read and does not improve anything)
- Add // NOSONAR (this are also tons of NOSONARs that might also mask other issues)
On the other hand, we want developers to use these types whenever possible. Disabling this rule will lower the quality of out interfaces.
- SonarQube Server 7.9.6 LTS
- “cpp”: “22.214.171.12475 [SonarCFamily]”,
I think that among the solutions you mentioned, the
stdFd is probably the best, both in satisfying the check and in keeping-up with the spirit of this MISRA rule.
If you end-up needing many such typedefs, this is probably a sign that the design of your code is not great, and that using opaque types would make it safer (and yes, I believe that
open is a badly designed function, that it should have returned a
FileDescriptor structure). If I was to use it in many places, I would probably wrap it into a safer interface. I don’t know if you are working with C or C++, but in the C++ community there is a strong push toward strong typing, and providing different types for different integers values, to increase code safety.
Another option that you did not mention would be to write:
auto fd = open("name", O_OPEN);
For information, this rule has been debated a lot in the MISRA committee for the next version, especially because of examples such as the one you provide. It’s very hard to come up with something automated that can differentiate when an int is just a value for which you don’t care about any platform limit, and when it is something numeric for which you must care about overflows and where having a typedef might help you remembering those limits.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.