[SUGGESTION] Cppfront could prevent implicit truncation and conversion when functions return numeric types #635
bluetarpmedia
started this conversation in
Suggestions
Replies: 1 comment
-
This can be done by treating |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Suggestion
Cppfront could report errors when detecting implicit numeric truncation and/or conversion in function return statements.
These would be compile-time only checks for:
Examples:
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
Yes, see:
(There are various CVEs listed for these weaknesses but I couldn't find any with source code.)
Here's a Cpp2 snippet based on examples from the above CWE links:
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes, it automates the guidance to compile at high warning levels. See:
Using the above Cpp2 example, there are no warnings from the 3 major compilers about
return kError_InvalidDimensions
with default options. To produce a warning:/W4
-Wconversion
I couldn't get a warning in GCC 12.2 even with
-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion
https://godbolt.org/z/M9bb46j4T
Describe alternatives you've considered.
One alternative is to provide runtime checks which would validate the actual value rather than just the type itself.
However, this could possibly introduce a significant runtime overhead for functions called in hot loops, and would probably require an opt-in/out preference, whereas the suggestion above is for compile-time only checks.
Beta Was this translation helpful? Give feedback.
All reactions