Skip to content

Avoid const_cast in methods on PEDecoder#126672

Merged
adamperlin merged 2 commits intomainfrom
copilot/avoid-const-cast-in-pdecoder
Apr 9, 2026
Merged

Avoid const_cast in methods on PEDecoder#126672
adamperlin merged 2 commits intomainfrom
copilot/avoid-const-cast-in-pdecoder

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Description

PEDecoder uses const_cast<PEDecoder *>(this)-> in 8 places across const methods to lazily cache validation flags and header pointers. This is the textbook use case for mutable.

Mark the four cache fields as mutable and remove all const_cast usages:

  • src/coreclr/inc/pedecoder.h — Add mutable to m_flags, m_pNTHeaders, m_pCorHeader, m_pReadyToRunHeader
  • src/coreclr/utilcode/pedecoder.cpp — Remove 7 const_cast sites (HasNTHeaders, CheckNTHeaders, CheckCorHeader, CheckILOnly ×2, FindReadyToRunHeader ×2)
  • src/coreclr/inc/pedecoder.inl — Remove 1 const_cast site (GetCorHeader)

No layout or behavioral change — mutable only affects const-correctness at compile time.

Copilot AI review requested due to automatic review settings April 8, 2026 22:55
Copilot AI review requested due to automatic review settings April 8, 2026 22:55
Copilot AI linked an issue Apr 8, 2026 that may be closed by this pull request
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Copilot AI requested review from Copilot and removed request for Copilot April 8, 2026 23:19
Copilot AI changed the title [WIP] Refactor PEDecoder methods to eliminate const_cast usage Avoid const_cast in methods on PEDecoder Apr 8, 2026
Copilot AI requested a review from adamperlin April 8, 2026 23:21
@adamperlin adamperlin marked this pull request as ready for review April 8, 2026 23:49
Copy link
Copy Markdown
Contributor

@adamperlin adamperlin left a comment

Choose a reason for hiding this comment

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

Pending CI, this LGTM. As copilot mentions, this is a compile time const-checking change so it shouldn't be behavioral.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves const-correctness within PEDecoder by replacing const_cast<PEDecoder*>(this) used for lazy caching inside const methods with mutable cache fields, keeping runtime behavior unchanged while making the intent explicit at the type level.

Changes:

  • Mark PEDecoder cache members (m_flags, m_pNTHeaders, m_pCorHeader, m_pReadyToRunHeader) as mutable.
  • Remove const_cast<PEDecoder*>(this)->... writes in pedecoder.cpp const methods used for one-time validation/caching.
  • Remove the const_cast-based lazy initialization in PEDecoder::GetCorHeader().

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/coreclr/utilcode/pedecoder.cpp Removes const_cast from const methods while preserving lazy caching of header pointers and validation flags.
src/coreclr/inc/pedecoder.inl Updates GetCorHeader() to lazily cache via mutable member instead of const_cast.
src/coreclr/inc/pedecoder.h Marks cache fields as mutable to support lazy initialization in const methods without casting away constness.

@AaronRobinsonMSFT
Copy link
Copy Markdown
Member

I like this idea a lot, it is a good start. The real fix though is to remove the const from methods that are mutating. I realize that would be a much bigger change though.

@adamperlin
Copy link
Copy Markdown
Contributor

/ba-g infra failures

@adamperlin adamperlin merged commit 6b6a8b5 into main Apr 9, 2026
106 of 118 checks passed
@adamperlin adamperlin deleted the copilot/avoid-const-cast-in-pdecoder branch April 9, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid const_cast in methods on PEDecoder

4 participants