Skip to content

Fix out-of-bounds read in copy_poly_in_matrix for monomials - #379

Merged
mohabsafey merged 1 commit into
algebraic-solving:masterfrom
wegank:fix/copy-poly-in-matrix-oob
Oct 2, 2026
Merged

mohabsafey merged 1 commit into
algebraic-solving:masterfrom
wegank:fix/copy-poly-in-matrix-oob

Conversation

@wegank

@wegank wegank commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

When a Gröbner basis element whose leading monomial is hit by the multiplication by x_n is a monomial (length 1), copy_poly_in_matrix compared the exponent at lmb + (len - 2) * nv, i.e. lmb - nv, with the exponent following the leading term, which belongs to the next polynomial or lies past the end of bexp.

AddressSanitizer reports this as a heap-buffer-overflow in the unit test fglm_build_matrixn_nonradical-radicalshape-31:

READ of size 4 in is_equal_exponent hilbert.c:545
    copy_poly_in_matrix hilbert.c:760
    build_matrixn hilbert.c:1568
0x... is located 8 bytes before 24-byte region allocated by monomial_basis hilbert.c:261

A monomial has no tail, so its row in the multiplication matrix is zero; since dense_mat is zero-initialized by all callers, the function now returns early in that case. The same guard is added to copy_poly_in_matrixcol, which contains identical code.

Testing

  • make check with AddressSanitizer (LLVM clang, -g -O1 -fsanitize=address -fno-omit-frame-pointer, ASAN_OPTIONS=detect_leaks=0): 68/68 pass (previously fglm_build_matrixn_nonradical-radicalshape-31 failed).
  • make check with a normal -O2 build: 68/68 pass.

This change was made with the assistance of generative AI (Claude Code).

Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

🤖 Generated with Claude Code

When a Groebner basis element whose leading monomial is hit by the
multiplication by x_n is a monomial (length 1), copy_poly_in_matrix
compared the exponent at lmb + (len - 2) * nv, i.e. lmb - nv, with the
exponent following the leading term, which belongs to the next
polynomial or lies past the end of bexp. This was reported by
AddressSanitizer as a heap-buffer-overflow in the unit test
fglm_build_matrixn_nonradical-radicalshape-31.

A monomial has no tail, so its row in the multiplication matrix is
zero; since dense_mat is zero-initialized, return early in that case.
The same guard is added to copy_poly_in_matrixcol, which contains the
same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mohabsafey
mohabsafey merged commit c3b9008 into algebraic-solving:master Oct 2, 2026
10 checks passed
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.

3 participants