Skip to content

Chemistry: fix crashes in has_numerical_prescripts - #1071

Open
dkager wants to merge 4 commits into
daisy:mainfrom
dkager:fix-prescripts-crash
Open

dkager wants to merge 4 commits into
daisy:mainfrom
dkager:fix-prescripts-crash

Conversation

@dkager

@dkager dkager commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Fix two crashes with prescripts in mmultiscripts:

  1. An mmultiscripts with mprescripts as its last child crashed, because MathCAT assumed that at least one pair of presubscript and presuperscript follows. Although this is expected, the MathML grammar allows omitting them. We now check for that.
  2. The check for a beta decay presubscript never triggered, because of presumed typos. When the code actually ran (under admittedly very specific conditions), it would crash on accessing index 3 of a vector of length 3.

The above came to light when I ran various types of prescripts through MathCAT to verify the Dedicon notation for them (its rule that "space ends a subscript" meant that I ran some interesting examples to test edge cases).

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Linux library size: 0.60 MiB (+0.00%)
Revision Release liblibmathcat.so
Base (32fb145) 0.60 MiB (627,936 bytes)
PR (ec58a15) 0.60 MiB (627,952 bytes)
Change +16 bytes (+0.00%)

Built with default features, Rust 1.96.0, and Ubuntu 24.04. Workflow run.

@NSoiffer

Copy link
Copy Markdown
Collaborator

The MathML grammar for mmultiscripts is

<mmultiscripts>
 base
 (subscript superscript)*
 [ <mprescripts/> (presubscript presuperscript)* ]
</mmultiscripts>

so it is not legal MathML to end with an mprescripts. Probably the place to catch this is in assure_mathml. Is the generation of this case something you can fix in your software? If not, the other solution is to fix it in the cleanup code.

@dkager

dkager commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

[ (presubscript presuperscript)* ]

I read this as:

  1. The entire line above is optional: it can occur zero or one times.
  2. The bit (presubscript presuperscript) has an asterisk: it may occur zero or more times.

This matches the grammar in Relax NG:

mmultiscripts = element mmultiscripts {mmultiscripts.attributes, MathExpression,MultiScriptExpression*,(mprescripts,MultiScriptExpression*)?}

Regardless, it's not very useful. I don't expect to see it in real books. However, I also don't want MathCAT to crash should this notation ever show up. My suggestion is to clean up:

<math><mmultiscripts><mi>a</mi><mi>b</mi><mi>c</mi><mprescripts/></mmultiscripts></math>

into:

<math><mmultiscripts><mi>a</mi><mi>b</mi><mi>c</mi></mmultiscripts></math>

@dkager

dkager commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

I just pushed a new solution on this branch. This is actually much better, thanks for pointing out the proper solution!

What changed:

  1. We now clean up mprescripts as last child of mmultiscripts. That means that the chemistry code no longer needs the safeguard.
  2. MathCAT cleanly fails if mmultiscripts has more than one mprescripts.
  3. MathCAT no longer crashes on a missing element in a sub/sup pair. This is a change to the existing code.
  4. Refactored some incorrect/unreachable code.
  5. The chemistry test I added now yields better output (though I am by no means a domain expert).

I believe that this covers all reasonable types of valid and invalid input, with some content improvements as well. One disclaimer: I was very much spot-welding instead of considering the entire codebase. So do keep pointing out better ways to do things, it makes for better solutions.

A side thought while I was working on this: perhaps instead of catching many types of invalid MathML, MathCAT could validate against a schema?

@NSoiffer

Copy link
Copy Markdown
Collaborator

FYI: I filed a MathML issue: w3c/mathml#605

@dkager

dkager commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I do agree that this particular notation is not useful. I don't think that it makes sense for a speech/braille notation to indicate that yes, there are prescripts, but then no, the list is actually empty. Therefore, this PR silently cleans up mprescripts with no following pairs. The case of 2 or more mprescripts is a hard fail.

@davidcarlisle

Copy link
Copy Markdown
Contributor

cleaning up the empty list seems like a good plan to me, despite the fact that we closed the referenced MathML issue with no change, confirming that the trailing <mprescripts/> is technically valid, it clearly isn't useful or have any sensible reading.

dkager and others added 2 commits November 25, 2026 12:55
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dkager

dkager commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

As a follow-up to the above, after some more reflection I would suggest that MathCAT also rejects mmultiscripts with one or mroe uneven pairs of sub/superscript, e.g.:

<mmultiscripts>
  <mi>x</mi> <!-- base -->
  <mi>a</mi> <!-- unclear: is this sub or super? -->
  <mprescripts/>
  <mi>b</mi> <!-- unclear: is this sub or super? -->
</mmultiscripts>

Currently, missing element(s) get added as <mtext>&#xA0;</mtext> in the superscript position. Better would be to reject the input: the valid way to skip a position is using <none/> and that already works well in MathCAT.

Related: #1086

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

Status: Triage

Development

Successfully merging this pull request may close these issues.

3 participants