Repository navigation
Reject C++ pointer casts to integer types that are too small [EDGcpfe/28892] #454
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
Draft
71iq
wants to merge
1
commit into
edgcpp:main
Choose a base branch
from
71iq:fix/edg-28892-pointer-integer-cast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−99
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [GH #454] | ||
| GNU C++ mode: casts of pointers to integer types that are too small | ||
|
|
||
| In GNU C++ mode (including Clang mode), a cast of a pointer to an integer type | ||
| too small to hold all of its bits was accepted with a warning. GCC 4.0 and | ||
| later, and Clang, reject such casts, and the standard requires a diagnostic. | ||
| They are now errors, except when emulating GCC versions before 4.0, which | ||
| accepted them with a warning. For example, with --g++ on a target with 64-bit | ||
| pointers: | ||
|
|
||
| unsigned char narrow(void *p) { | ||
| return reinterpret_cast<unsigned char>(p); // Now an error. | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/tests/imported/gnu/cpp/parse/.cast1.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics -w Test_name.c | ||
| "./parse/cast1.C", line 8: error: invalid type conversion | ||
| (int((char*)0)); | ||
| ^ | ||
|
|
||
| "./parse/cast1.C", line 9: error: invalid type conversion | ||
| sizeof ((int((char*)0))); | ||
| ^ | ||
|
|
||
| "./parse/cast1.C", line 10: error: invalid type conversion | ||
| typeid ((int((char*)0))); | ||
| ^ | ||
|
|
||
| 3 errors detected in the compilation of "Test_name.c". |
1 change: 1 addition & 0 deletions
1
tests/tests/imported/gnu/cpp/parse/.cast1.rto/edg_x86_32.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics -w Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/imported/gnu/cpp/parse/.cast1.rto/edg_x86_32_cp.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics -w Test_name.c |
23 changes: 0 additions & 23 deletions
23
tests/tests/imported/gnu/cpp/template/.sfinae13.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics Test_name.c | ||
| "Test_name.c", line 59: warning: conversion from pointer to smaller integer | ||
| CHECK_CAST(reinterpret); | ||
| ^ | ||
| detected during: | ||
| instantiation of "check_reinterpret_cast" based on template arguments <void *, char> at line 59 | ||
| instantiation of class "has_reinterpret_cast<T, U> [with T=void *, U=char]" at line 83 | ||
|
|
||
| "Test_name.c", line 83: error: static assertion failed with "!(has_reinterpret_cast<void*, char>::value)" | ||
| STATIC_ASSERT(!(has_reinterpret_cast<void*, char>::value)); | ||
| ^ | ||
|
|
||
| "Test_name.c", line 38: warning: conversion from pointer to smaller integer | ||
| typename enable_if<(sizeof(((U)create_a<T>()), 0) > 0), yes_type>::type | ||
| ^ | ||
| detected during: | ||
| instantiation of "check_c_cast" based on template arguments <void *, char> at line 47 | ||
| instantiation of class "has_c_cast<T, U> [with T=void *, U=char]" at line 88 | ||
|
|
||
| "Test_name.c", line 88: error: static assertion failed with "!(has_c_cast<void*, char>::value)" | ||
| STATIC_ASSERT(!(has_c_cast<void*, char>::value)); | ||
| ^ | ||
|
|
||
| 2 errors detected in the compilation of "Test_name.c". |
24 changes: 0 additions & 24 deletions
24
tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32.1.1.txt
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32_cp.1.1.txt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
tests/tests/regressions/.edg-28892-legacy.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| fe_only -DTEST_NUMBER=1 --g++ --gnu_version=30406 Test_name.c | ||
| "Test_name.c", line 11: warning: conversion from pointer to smaller integer | ||
| x = (int)ptr; | ||
| ^ | ||
|
|
||
| "Test_name.c", line 12: warning: conversion from pointer to smaller integer | ||
| x = (short)ptr; | ||
| ^ | ||
|
|
5 changes: 5 additions & 0 deletions
5
tests/tests/regressions/.edg-28892-legacy.rto/edg_x86_32.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| fe_only -DTEST_NUMBER=1 --g++ --gnu_version=30406 Test_name.c | ||
| "Test_name.c", line 12: warning: conversion from pointer to smaller integer | ||
| x = (short)ptr; | ||
| ^ | ||
|
|
5 changes: 5 additions & 0 deletions
5
tests/tests/regressions/.edg-28892-legacy.rto/edg_x86_32_cp.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| fe_only -DTEST_NUMBER=1 --g++ --gnu_version=30406 Test_name.c | ||
| "Test_name.c", line 12: warning: conversion from pointer to smaller integer | ||
| x = (short)ptr; | ||
| ^ | ||
|
|
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.edg-28892-positive.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=130300 Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.edg-28892-positive.rto/default.2.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --strict Test_name.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=130300 Test_name.c | ||
| "Test_name.c", line 7: error: invalid type conversion | ||
| return reinterpret_cast<unsigned char>(p); // Must be an error in GNU mode. | ||
| ^ | ||
|
|
||
| "Test_name.c", line 10: error: invalid type conversion | ||
| return reinterpret_cast<unsigned char>(p); // Must also be an error. | ||
| ^ | ||
|
|
||
| 2 errors detected in the compilation of "Test_name.c". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --g++ --gnu_version=150200 Test_name.c | ||
| "Test_name.c", line 7: error: invalid type conversion | ||
| return reinterpret_cast<unsigned char>(p); // Must be an error in GNU mode. | ||
| ^ | ||
|
|
||
| "Test_name.c", line 10: error: invalid type conversion | ||
| return reinterpret_cast<unsigned char>(p); // Must also be an error. | ||
| ^ | ||
|
|
||
| 2 errors detected in the compilation of "Test_name.c". |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tracked down why that gpp_mode is there. It's for this test case:
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tracking that down. I replaced
gpp_modewithgpp_version_is(<40000), so Clang mode and GNU C++ 4.0 and later now give the error.regressions/edg-28892-legacyis your example with--gnu_version=30406:(int)ptrand(short)ptrare accepted with a warning.