Skip to content

Fix NullReferenceException in FindIndexOfNextParaMark#124

Open
aslakhellesoy wants to merge 2 commits intoJSv4:mainfrom
papyria:fix-null-ref-in-find-index-of-next-para-mark
Open

Fix NullReferenceException in FindIndexOfNextParaMark#124
aslakhellesoy wants to merge 2 commits intoJSv4:mainfrom
papyria:fix-null-ref-in-find-index-of-next-para-mark

Conversation

@aslakhellesoy
Copy link
Copy Markdown

@aslakhellesoy aslakhellesoy commented Apr 22, 2026

Hello, and thanks for this great library!

I found a bug where an NRE is thrown when there are certain bookmarks. This PR has a new test that reproduces the bug, and a change to fix it. All other tests are passing.

Summary

  • Fix NullReferenceException in WmlComparer.FindIndexOfNextParaMark when comparing documents where one has bookmarkStart/bookmarkEnd as direct children of w:body (siblings of w:p rather than children)
  • The method assumed all elements in the comparison unit array were ComparisonUnitWord, but body-level bookmarks produce other ComparisonUnit types. The as ComparisonUnitWord cast returned null, and .DescendantContentAtoms() threw
  • Added null guards for both the as cast (skip non-word units) and the LastOrDefault() result

Test plan

  • Added WmlComparerBodyLevelBookmarkTests with test documents that reproduce the crash
  • Test fails before the fix with NullReferenceException at FindIndexOfNextParaMark
  • Test passes after the fix
  • Full test suite passes (1206 passed, 0 failed, 1 skipped)

FindIndexOfNextParaMark assumed all elements in the comparison unit
array were ComparisonUnitWord, but documents with bookmarkStart/
bookmarkEnd as direct children of w:body produce other ComparisonUnit
types. The `as ComparisonUnitWord` cast returned null, and the
subsequent .DescendantContentAtoms() call threw NullReferenceException.

Added null guards for both the `as` cast (skip non-word units) and
the LastOrDefault() result.
Copilot AI review requested due to automatic review settings April 22, 2026 09:21
Copy link
Copy Markdown

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

Fixes a crash in WmlComparer.FindIndexOfNextParaMark when comparing documents that can produce non-ComparisonUnitWord entries in the comparison unit arrays (notably with body-level bookmarks), and adds a regression test + changelog entry.

Changes:

  • Added guards in FindIndexOfNextParaMark to avoid NullReferenceException when encountering non-word comparison units and when LastOrDefault() returns null.
  • Added WmlComparerBodyLevelBookmarkTests to reproduce and prevent the crash.
  • Documented the fix in CHANGELOG.md.

Reviewed changes

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

File Description
Docxodus/WmlComparer.cs Adds null/type guards in FindIndexOfNextParaMark to prevent a crash during LCS processing.
Docxodus.Tests/WmlComparerBodyLevelBookmarkTests.cs Regression test ensuring comparisons don’t throw NullReferenceException for body-level bookmarks scenario.
CHANGELOG.md Notes the fix in the “Fixed” section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Docxodus/WmlComparer.cs Outdated
Comment thread CHANGELOG.md Outdated
- Handle any ComparisonUnit with Contents (including ComparisonUnitGroup)
  in FindIndexOfNextParaMark, not just ComparisonUnitWord, since groups
  can also end with a w:pPr atom
- Fix CHANGELOG wording to match the actual implementation
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.

2 participants