e/matrices: fix SIGSEGV in coeffs_of_vec for inexact coefficient rings#4429
Open
d-torrance wants to merge 1 commit into
Open
e/matrices: fix SIGSEGV in coeffs_of_vec for inexact coefficient rings#4429d-torrance wants to merge 1 commit into
d-torrance wants to merge 1 commit into
Conversation
coeffs_of_vec iterates over the Nterms of a polynomial and, for each
term whose monomial appears in the exponent table, calls:
ring_elem t = P->make_flat_term(h.coeff, mon);
vec v = P->make_vec(val - 1, t);
v->next = result; // <-- crashed here
make_flat_term returns ZERO_RINGELEM (a null Nterm pointer) when the
coefficient is zero under K_->is_zero. make_vec in turn returns nullptr
for a zero polynomial. The subsequent unconditional dereference of v
is therefore a null pointer dereference, caught as SIGSEGV.
For exact coefficient rings (ZZ, QQ, finite fields) a polynomial never
carries a zero-coefficient Nterm in practice, so the bug lay dormant.
For inexact fields (RR, CC / MPFR-backed) it surfaces non-deterministically:
the polyheap that accumulates products in Matrix multiplication removes
cancelling terms through add_to's case-0 branch only when both sides
present the same monomial simultaneously; in edge cases -- e.g. when
mult_by_term is called with a scalar that is exactly 0.0 in MPFR, or
when terms with a unique monomial survive from an intermediate polynomial
that was multiplied by zero -- a zero-coefficient Nterm can reach the
final polynomial without being cleaned up.
The symptom appeared in the NoetherianOperators test suite after new
tests were added that call truncatedDual / eliminatingDual over CC[x,y].
Those functions build the matrix product E * H.BMcoefs (a CC[x,y]
polynomial matrix times a numerical CC kernel). Floating-point sums of
products can land on exactly 0.0+0.0i non-deterministically depending on
arithmetic order and memory layout, producing the latent zero-coefficient
Nterms that trigger this path.
Fix: guard the vec construction with a null check. A zero-coefficient
term contributes nothing to the coefficient matrix, so skipping it is
both safe and correct.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
In the last couple days, I started seeing the following segfault in the PPA builds (specifically, Ubuntu 22.04 amd64):
Full log: https://launchpadlibrarian.net/864313612/buildlog_ubuntu-jammy-amd64.macaulay2_1.26.05+git202606070040-0ppa202606080242~ubuntu22.04.1_BUILDING.txt.gz
I had Claude check it out -- here is its proposed fix, which seems quite reasonable!
AI Disclosure
This was all Claude. Here's its commit message:
coeffs_of_vec iterates over the Nterms of a polynomial and, for each term whose monomial appears in the exponent table, calls:
make_flat_term returns ZERO_RINGELEM (a null Nterm pointer) when the coefficient is zero under K_->is_zero. make_vec in turn returns nullptr for a zero polynomial. The subsequent unconditional dereference of v is therefore a null pointer dereference, caught as SIGSEGV.
For exact coefficient rings (ZZ, QQ, finite fields) a polynomial never carries a zero-coefficient Nterm in practice, so the bug lay dormant. For inexact fields (RR, CC / MPFR-backed) it surfaces non-deterministically: the polyheap that accumulates products in Matrix multiplication removes cancelling terms through add_to's case-0 branch only when both sides present the same monomial simultaneously; in edge cases -- e.g. when mult_by_term is called with a scalar that is exactly 0.0 in MPFR, or when terms with a unique monomial survive from an intermediate polynomial that was multiplied by zero -- a zero-coefficient Nterm can reach the final polynomial without being cleaned up.
The symptom appeared in the NoetherianOperators test suite after new tests were added that call truncatedDual / eliminatingDual over CC[x,y]. Those functions build the matrix product E * H.BMcoefs (a CC[x,y] polynomial matrix times a numerical CC kernel). Floating-point sums of products can land on exactly 0.0+0.0i non-deterministically depending on arithmetic order and memory layout, producing the latent zero-coefficient Nterms that trigger this path.
Fix: guard the vec construction with a null check. A zero-coefficient term contributes nothing to the coefficient matrix, so skipping it is both safe and correct.