Skip to content

Reject C++ pointer casts to undersized integers - #454

Draft
71iq wants to merge 5 commits into
edgcpp:mainfrom
71iq:fix/edg-28892-pointer-integer-cast
Draft

71iq wants to merge 5 commits into
edgcpp:mainfrom
71iq:fix/edg-28892-pointer-integer-cast

Conversation

@71iq

@71iq 71iq commented Nov 1, 2026

Copy link
Copy Markdown

Require a sufficiently wide destination type for pointer-to-integer reinterpret_cast conversions in GNU C++ mode. Microsoft and C compatibility behavior is preserved.

Related discussion: #298
Previously accepted by Mike Herrick as issue EDGcpfe/28892.

Adds rejection tests for object and function pointers and a valid pointer-width conversion test. GCC 13 rejects the negative cases and accepts the positive case.

EDG build, regression testing, and test recordings are pending because the Docker development environment is unavailable. This PR is a draft until those checks are completed.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 389fa8f1f379:

CI other.

@daveedvdv-nvidia daveedvdv-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a small example here.

Comment thread src/types.c
is_integral(dest_type) &&
(C_mode() || ms_extensions || gpp_mode ||
(C_mode() || ms_extensions ||
dest_of_ptr_cast_big_enough(source_type, dest_type))) {

Copy link
Copy Markdown
Contributor

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:

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+.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants