Avoid const_cast in methods on PEDecoder#126672
Merged
adamperlin merged 2 commits intomainfrom Apr 9, 2026
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @agocke |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/c9812f45-72e6-4f80-bcbc-1c4d48a5f951 Co-authored-by: adamperlin <10533886+adamperlin@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor PEDecoder methods to eliminate const_cast usage
Avoid Apr 8, 2026
const_cast in methods on PEDecoder
adamperlin
approved these changes
Apr 8, 2026
Contributor
There was a problem hiding this comment.
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
PEDecodercache members (m_flags,m_pNTHeaders,m_pCorHeader,m_pReadyToRunHeader) asmutable. - Remove
const_cast<PEDecoder*>(this)->...writes inpedecoder.cppconst methods used for one-time validation/caching. - Remove the
const_cast-based lazy initialization inPEDecoder::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. |
Member
|
I like this idea a lot, it is a good start. The real fix though is to remove the |
AaronRobinsonMSFT
approved these changes
Apr 8, 2026
This was referenced Apr 9, 2026
Open
Contributor
|
/ba-g infra failures |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
PEDecoderusesconst_cast<PEDecoder *>(this)->in 8 places acrossconstmethods to lazily cache validation flags and header pointers. This is the textbook use case formutable.Mark the four cache fields as
mutableand remove allconst_castusages:src/coreclr/inc/pedecoder.h— Addmutabletom_flags,m_pNTHeaders,m_pCorHeader,m_pReadyToRunHeadersrc/coreclr/utilcode/pedecoder.cpp— Remove 7const_castsites (HasNTHeaders,CheckNTHeaders,CheckCorHeader,CheckILOnly×2,FindReadyToRunHeader×2)src/coreclr/inc/pedecoder.inl— Remove 1const_castsite (GetCorHeader)No layout or behavioral change —
mutableonly affects const-correctness at compile time.