Repository navigation
Conversation
Walkthrough
Merge Risk:
|
|
AcknowlEDG review results updated for
CI other. |
daveedvdv-nvidia
left a comment
There was a problem hiding this comment.
See suggested changes.
| Reject C++ pointer casts to undersized integers | ||
|
|
||
| In GNU C++ mode, pointer-to-integer reinterpret_cast conversions now | ||
| require a destination type large enough to hold the pointer value. |
There was a problem hiding this comment.
I'd add a small example here.
There was a problem hiding this comment.
Done. The entry now shows reinterpret_cast<unsigned char>(p) on a target with 64-bit pointers, which is now an error with --g++.
| is_integral(dest_type) && | ||
| (C_mode() || ms_extensions || gpp_mode || | ||
| (C_mode() || ms_extensions || | ||
| dest_of_ptr_cast_big_enough(source_type, dest_type))) { |
There was a problem hiding this comment.
I tracked down why that gpp_mode is there. It's for this test case:
int main()
{
int *ptr;
int x;
x = (int)ptr;
x = (short)ptr; // warning in gcc 3.4.6
return 0;
}
Prior to GCC 4, this was accepted with a warning (https://godbolt.org/z/fG4YcKKr9).
Note that your test case was not accepted even then.
Rather than emulating this 100% accurately, I'd just replace the gpp_mode by gpp_version_is(<40000). That way any pre-GCC-4 code out there that relies on the leniency will continue to work (which should not be a whole lot), and modern code will track GCC 4+.
There was a problem hiding this comment.
Thanks for tracking that down. I replaced gpp_mode with gpp_version_is(<40000), so Clang mode and GNU C++ 4.0 and later now give the error. regressions/edg-28892-legacy is your example with --gnu_version=30406: (int)ptr and (short)ptr are accepted with a warning.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@coderabbitai full review |
Action performed
|
|
AcknowlEDG review results updated for
CI failure. |
…/28892] In GNU C++ mode (including Clang mode), a cast of a pointer to an integer type too small to hold it was accepted with a warning. GCC 4.0 and later, and Clang, reject it. Keep the leniency only when emulating GNU C++ versions before 4.0, which accepted such casts with a warning. Add regression tests for the error, the accepted wide cast, and the pre-4.0 GNU behavior, and update the recordings of imported tests that now get the error their sources expect. gnu/cpp/parse/cast1 is an ILP32 test upstream; it now fails on 64-bit configurations, as with GCC.
8118f59 to
d4836c2
Compare
|
AcknowlEDG review results updated for
CI success. |
In GNU C++ mode, including Clang mode, a cast of a pointer to an integer type too small to hold it was accepted with a warning. An example is
(int)ptron a 64-bit target. GCC 4.0 and later, and Clang, reject it.In
types.c, thegpp_modetest that allowed this is nowgpp_version_is(<40000). GNU C++ before 4.0 accepted such casts with a warning, so code written for those versions keeps working. C and Microsoft mode are unchanged.regressions/edg-28892covers the error for object and function pointers,regressions/edg-28892-positivea cast to a wide enough type, andregressions/edg-28892-legacythe warning with--gnu_version=30406.Nine imported Clang and GCC tests are re-recorded, and each now gets the error its source expects.
gnu/cpp/parse/cast1is an ILP32 test upstream, so it now fails on the 64-bit configurations, as it does with GCC, and passes on the 32-bit ones.gnu/cpp/template/sfinae13now passes.Tested with the full test suites for edg_x86_64 and edg_x86_64_cp, compared with a run of main, and with the new and changed tests for edg_x86_32 and edg_x86_32_cp.
The
Changesentry uses this PR's number until there is an issue for it.Reported in discussion #298.