Skip to content

Reject C++ pointer casts to integer types that are too small [EDGcpfe/28892] - #454

Open
71iq wants to merge 1 commit into
edgcpp:mainfrom
71iq:fix/edg-28892-pointer-integer-cast
Open

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

Conversation

@71iq

@71iq 71iq commented Nov 1, 2026 •

Copy link
Copy Markdown

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)ptr on a 64-bit target. GCC 4.0 and later, and Clang, reject it.

In types.c, the gpp_mode test that allowed this is now gpp_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-28892 covers the error for object and function pointers, regressions/edg-28892-positive a cast to a wide enough type, and regressions/edg-28892-legacy the 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/cast1 is 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/sfinae13 now 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 Changes entry uses this PR's number until there is an issue for it.

Reported in discussion #298.

Summary by CodeRabbit

  • Bug Fixes
    • GNU C++ and Clang modes now report an error when a pointer is cast to an integer type too small to hold it, instead of accepting the cast with a warning.
    • GNU C++ versions before 4.0 retain the legacy behavior of accepting these casts with a warning.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The conversion check no longer accepts undersized pointer-to-integer casts solely because GNU mode is enabled. The exception remains for C mode, Microsoft extensions, and emulated GNU C++ versions before 4.0. Regression tests and expected diagnostic outputs cover these rules.

Changes: Pointer-to-integer cast validation

Layer / File(s) Summary
Conversion rule and change note
src/types.c, src/pending_changes/edg-28892-pointer-integer-cast.txt
The conversion check limits the undersized-destination exception to C mode, Microsoft extensions, and emulated GNU C++ versions before 4.0. The pending-change note describes the diagnostic change and exception.
Regression cases for cast widths and GNU versions
tests/tests/regressions/edg-28892*.sft.cpp, tests/tests/regressions/.edg-28892*.rto/*
Regression cases cover narrow object and function pointer casts, a sufficiently wide destination, and GNU C++ 3.4.6 casts. Expected outputs record errors for narrow casts and warnings for the legacy casts.
Existing diagnostic baselines
tests/tests/imported/clang/cpp/*, tests/tests/imported/gnu/cpp/*
Expected Clang and GNU outputs replace pointer-to-smaller-integer warnings with invalid-type-conversion errors. The GNU template output fixtures for the cast checks are removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested labels: bug

Suggested reviewers: nv-cmeerw

Merge Risk: Minimal · up to d4836

This change makes GNU C++ mode, including Clang mode, reject casts of pointers to integer types too small to hold them. GNU C++ before 4.0 keeps the old warning. Regression tests and updated diagnostic baselines cover the new behavior. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: C++ pointer casts to integer types too small to hold the pointer are now rejected. The raw summary confirms this change in src/types.c, the pending-change note, and t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: C++ pointer casts to integer types too small to hold the pointer are now rejected. The raw summary confirms this change in src/types.c, the pending-change note, and the regression tests. The PR objectives describe the same change for GNU C++ 4.0 and later, including Clang mode, with the pre-4.0 warning behavior retained. The bracketed issue tag is a minor addition and does not affect relevance.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8118f59 and d4836c2.

Files ignored due to path filters: 3

  • tests/expectations/edg_x86_32/imported.log is excluded by !**/*.log
  • tests/expectations/edg_x86_32_cp/imported.log is excluded by !**/*.log
  • tests/expectations/edg_x86_64/imported.log is excluded by !**/*.log
Files selected for processing: 29

  • src/pending_changes/edg-28892-pointer-integer-cast.txt
  • src/types.c
  • tests/tests/imported/clang/cpp/CXX/temp/temp.res/.p4.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/CXX/temp/temp.res/.p4.rto/edg_x86_64_cp.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.cstyle-cast.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.functional-cast.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.nullptr.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.reinterpret-cast.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.reinterpret-cast.rto/edg_x86_32_cp.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.reinterpret-cast.rto/edg_x86_64_cp.1.1.txt
  • tests/tests/imported/clang/cpp/SemaCXX/.unknown-anytype.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/cpp0x/.nullptr04.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/parse/.cast1.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/parse/.cast1.rto/edg_x86_32.1.1.txt
  • tests/tests/imported/gnu/cpp/parse/.cast1.rto/edg_x86_32_cp.1.1.txt
  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32.1.1.txt
  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32_cp.1.1.txt
  • tests/tests/regressions/.edg-28892-legacy.rto/default.1.1.txt
  • tests/tests/regressions/.edg-28892-legacy.rto/edg_x86_32.1.1.txt
  • tests/tests/regressions/.edg-28892-legacy.rto/edg_x86_32_cp.1.1.txt
  • tests/tests/regressions/.edg-28892-positive.rto/default.1.1.txt
  • tests/tests/regressions/.edg-28892-positive.rto/default.2.1.txt
  • tests/tests/regressions/.edg-28892.rto/default.1.1.txt
  • tests/tests/regressions/.edg-28892.rto/default.2.1.txt
  • tests/tests/regressions/.edg-28892.rto/default.3.1.txt
  • tests/tests/regressions/edg-28892-legacy.sft.cpp
  • tests/tests/regressions/edg-28892-positive.sft.cpp
  • tests/tests/regressions/edg-28892.sft.cpp
Files with no reviewable changes: 3

  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32.1.1.txt
  • tests/tests/imported/gnu/cpp/template/.sfinae13.rto/edg_x86_32_cp.1.1.txt
Files skipped from review as they are similar to previous changes: 1

  • src/pending_changes/edg-28892-pointer-integer-cast.txt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

71iq added a commit to 71iq/compiler that referenced this pull request Nov 1, 2026
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 389fa8f1f379:

CI other.

71iq added a commit to 71iq/compiler that referenced this pull request Nov 1, 2026

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. The entry now shows reinterpret_cast<unsigned char>(p) on a target with 64-bit pointers, which is now an error with --g++.

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

Copy link
Copy Markdown
Author

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_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

codecov Bot commented Nov 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@71iq

71iq commented Nov 2, 2026

Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Fix comments: Fix CodeRabbit comments on this PR

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 8118f597e7ba:

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.
@71iq
71iq force-pushed the fix/edg-28892-pointer-integer-cast branch from 8118f59 to d4836c2 Compare November 8, 2026 09:28
@71iq 71iq changed the title Reject C++ pointer casts to undersized integers Reject C++ pointer casts to integer types that are too small [EDGcpfe/28892] Nov 8, 2026
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for d4836c2b19ab:

CI success.

@71iq
71iq marked this pull request as ready for review November 10, 2026 17:12
@71iq
71iq requested a review from a team November 10, 2026 17:12
@71iq
71iq marked this pull request as draft November 11, 2026 19:12
@71iq
71iq marked this pull request as ready for review November 12, 2026 08:14

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