Skip to content

Fix XmlNodeReader invalid stage: log and restore XmlNodeType - #124559

Open
lilinus wants to merge 8 commits into
dotnet:mainfrom
lilinus:fix-xml-node-reader
Open

lilinus wants to merge 8 commits into
dotnet:mainfrom
lilinus:fix-xml-node-reader

Conversation

@lilinus

@lilinus lilinus commented Feb 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #34443

Copilot AI review requested due to automatic review settings February 18, 2026 15:29
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Feb 18, 2026

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@lilinus
lilinus marked this pull request as ready for review February 19, 2026 08:24

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings March 3, 2026 13:41

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@stephentoub

Copy link
Copy Markdown
Member

🤖 Copilot Code Review — PR #124559

Holistic Assessment

Motivation: The bug is real and well-documented. When XmlNodeReader is positioned on an attribute and MoveToAttribute returns false for a nonexistent attribute, the reader's _nodeType field gets corrupted to XmlNodeType.Element while _curNode still points to the original attribute — leaving the reader in an inconsistent state. This causes downstream operations like ReadStartElement to fail. Other XmlReader implementations (XmlTextReader, XmlReader.Create) handle this correctly.

Approach: The fix correctly identifies the root cause: ResetMove calls LogMove to save state, then navigates from attribute back to element (modifying _nodeType via ref), but the companion RollBackMove never restored _nodeType. The fix adds a _logNodeType field and threads it through LogMove/RollBackMove — the minimal, correct fix.

Summary: ✅ LGTM. The fix is correct, minimal, and follows the existing log/rollback pattern. All call sites for LogMove (3 callers) and RollBackMove (7 callers) are updated. The regression test directly reproduces the reported bug. Two minor suggestions below.


Detailed Findings

✅ Correctness — Fix properly restores _nodeType on rollback

Traced the bug scenario step by step:

  1. Reader on attr1 (_nodeType = Attribute)
  2. MoveToAttribute("attr2") → ResetMove saves state via LogMove, then navigates to parent element, setting _nodeType = Element
  3. Attribute lookup fails → RollBackMove restores _curNode back to attr1

Before fix: _nodeType stays Element → inconsistent with _curNode (attr1). After fix: _logNodeType = Attribute is saved in LogMove and restored in RollBackMove → consistent state.

All 3 LogMove call sites and all 7 RollBackMove call sites are updated. The constructor initializes _logNodeType correctly.

✅ No new public API surface

XmlNodeReaderNavigator is internal sealed. LogMove and RollBackMove are internal-only methods. No ref assembly changes needed.

✅ Comment cleanup

The stale //, ref curDepth ) ) { comments on lines 1371 and 1392 are cleaned up — good housekeeping.

💡 Test — Consider adding explicit state assertions (suggestion, not blocking)

The test implicitly validates the fix via ReadStartElement("child") succeeding. Adding explicit assertions after the failed MoveToAttribute would make the test more precise and easier to diagnose if it ever regresses:

csharp Assert.False(nodeReader.MoveToAttribute("attr2")); Assert.Equal(XmlNodeType.Attribute, nodeReader.NodeType); Assert.Equal("attr1", nodeReader.Name);

This was flagged by all three model reviews (Claude, Gemini, GPT).

💡 Test — Consider testing MoveToAttribute(name, namespaceURI) overload (suggestion, not blocking)

The namespace-qualified MoveToAttribute(string, string?) overload has the same rollback pattern and fix. A [Theory] with both overloads or an additional test case would increase coverage with minimal effort.

@danmoseley

Copy link
Copy Markdown
Contributor

@copilot look at feedback and address it, pushing to this PR

@lilinus

lilinus commented Mar 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot look at feedback and address it, pushing to this PR

Not sure copilot can push to PR :( I'll get around to address the feedback asap (probably in a week or two)

@danmoseley

Copy link
Copy Markdown
Contributor

It can't because it came from a fork. My bad. Sounds good!

Copilot AI review requested due to automatic review settings April 2, 2026 14:22

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings April 3, 2026 06:16

Copilot AI 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.

Pull request overview

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

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "df98818e64ee43e6ca875a5f1308d544051b3806",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "98bfa17c9f3910eec30099b7ac140d8a09df6227",
  "last_reviewed_commit": "df98818e64ee43e6ca875a5f1308d544051b3806",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "98bfa17c9f3910eec30099b7ac140d8a09df6227",
  "last_recorded_worker_run_id": "29741198531",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "beaf28e2933105e17a8ed7a2c4b66b4a1c7d310d",
      "review_id": 4730521926
    },
    {
      "commit": "df98818e64ee43e6ca875a5f1308d544051b3806",
      "review_id": 4734870612
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: XmlNodeReader.MoveToAttribute (and related move APIs) left the reader in an inconsistent state after a failed move: when an attribute was not found, the rollback restored the navigator cursor and depth but not the reader's cached _nodeType. As reported in #34443, this produced states like Element attr1 where a subsequent ReadStartElement throws Element 'child' was not found., diverging from XmlTextReader/XmlReader.Create behavior.

Approach: The fix extends the roll-back bookkeeping in XmlNodeReaderNavigator to also snapshot and restore the reader's XmlNodeType. A new _logNodeType field is added and initialized in the constructor; LogMove gains an XmlNodeType nt parameter that records it, and RollBackMove gains a ref XmlNodeType nt out-parameter that restores it. All callers in XmlNodeReader (the MoveToAttribute overloads, MoveToFirstAttribute, MoveToNextAttribute, MoveToElement) are updated to pass/restore _nodeType, so a failed move now leaves _nodeType unchanged. Two targeted tests reproduce the exact issue scenario (with and without namespaces) and assert the reader stays on Attribute attr1 after the failed move and that ReadStartElement then succeeds. Some stale commented-out code (//, ref curDepth ) ) {) is also cleaned up.

Summary: This is a well-scoped, correct fix that keeps the log/restore invariant symmetric — every existing RollBackMove call site is updated consistently, and ResetMove/MoveToNextAttribute/MoveToElement correctly seed the log with the current _nodeType before the attempted move, so the restored value matches the pre-move node type. Behavior now aligns with the other XmlReader implementations shown in the issue. The regression tests directly encode the reported repro. I found no correctness concerns; only one trivial style nit (a double blank line between the two new test methods), noted inline. LGTM.

Detailed Findings

No actionable correctness or design issues found. One minor style nit is noted as an inline comment on the test file (double blank line between methods).

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 57 AIC · ⌖ 10.5 AIC · ⊞ 10K

@github-actions github-actions Bot 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.

Holistic Review

Motivation: XmlNodeReader.MoveToAttribute (and related move APIs) left the reader in an inconsistent state after a failed move: when an attribute was not found, the rollback restored the navigator cursor and depth but not the reader's cached _nodeType. As reported in #34443, this produced states like Element attr1 where a subsequent ReadStartElement throws Element 'child' was not found., diverging from XmlTextReader/XmlReader.Create behavior.

Approach: The fix extends the roll-back bookkeeping in XmlNodeReaderNavigator to also snapshot and restore the reader's XmlNodeType. A new _logNodeType field is added and initialized in the constructor; LogMove gains an XmlNodeType nt parameter that records it, and RollBackMove gains a ref XmlNodeType nt parameter that restores it. All callers in XmlNodeReader (the MoveToAttribute overloads, MoveToFirstAttribute, MoveToNextAttribute, MoveToElement) are updated to pass/restore _nodeType, so a failed move now leaves _nodeType unchanged. Stale commented-out code (//, ref curDepth ) ) {) is cleaned up. Targeted tests reproduce the reported scenario (with and without namespaces) and assert the reader stays on Attribute attr1 after a failed move and that ReadStartElement then succeeds.

Summary: The latest change since the prior review is a single cosmetic edit that removes a double blank line between the two new test methods in XmlNodeReaderMoveToAttributeTests.cs — precisely the style nit the previous review noted. It introduces no behavioral change. The cumulative fix remains well-scoped and correct: the log/restore invariant stays symmetric across every RollBackMove call site, and ResetMove/MoveToNextAttribute/MoveToElement seed the log with the current _nodeType before the attempted move so the restored value matches the pre-move node type. Behavior now aligns with the other XmlReader implementations. No new actionable findings in the incremental change. LGTM.

Assessment History

  • review 4730521926 reviewed commit beaf28e2933105e17a8ed7a2c4b66b4a1c7d310d with an LGTM verdict. The current verdict is also LGTM. The assessment is unchanged: the only new commit (df98818) removes the double blank line that review flagged as its lone inline nit, so motivation, approach, and risk are all identical.

Detailed Findings

No actionable correctness or design issues found. The prior review's only nit (a double blank line between the two new test methods) has now been resolved by the incremental commit.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 41.1 AIC · ⌖ 10.5 AIC · ⊞ 10K

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

area-System.Xml community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlNodeReader MoveToAttribute bug, leaves XmlReader in inconsistent state

4 participants