Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault in monomial_coefficient #39255

Open
2 tasks done
keltecc opened this issue Jan 3, 2025 · 0 comments · May be fixed by #39256
Open
2 tasks done

Segmentation fault in monomial_coefficient #39255

keltecc opened this issue Jan 3, 2025 · 0 comments · May be fixed by #39256
Labels

Comments

@keltecc
Copy link

keltecc commented Jan 3, 2025

Steps To Reproduce

P.<x, y> = PolynomialRing(ZZ)
(x + y).monomial_coefficient(x - x)

Expected Behavior

Raise ValueError with error text.

Actual Behavior

Segmentation fault.

Additional Information

There is a missing check for zero monomial in monomial_coefficient() for multivariate polynomial:

def monomial_coefficient(self, MPolynomial_libsingular mon):
"""
Return the coefficient in the base ring of the monomial mon in
``self``, where mon must have the same parent as ``self``.
This function contrasts with the function ``coefficient``
which returns the coefficient of a monomial viewing this
polynomial in a polynomial ring over a base ring having fewer
variables.
INPUT:
- ``mon`` -- a monomial
OUTPUT: coefficient in base ring
.. SEEALSO::
For coefficients in a base ring of fewer variables,
look at ``coefficient``.
EXAMPLES::
sage: P.<x,y> = QQ[]
The parent of the return is a member of the base ring.
sage: f = 2 * x * y
sage: c = f.monomial_coefficient(x*y); c
2
sage: c.parent()
Rational Field
sage: f = y^2 + y^2*x - x^9 - 7*x + 5*x*y
sage: f.monomial_coefficient(y^2)
1
sage: f.monomial_coefficient(x*y)
5
sage: f.monomial_coefficient(x^9)
-1
sage: f.monomial_coefficient(x^10)
0
"""
cdef poly *p = self._poly
cdef poly *m = mon._poly
cdef ring *r = self._parent_ring
if mon._parent is not self._parent:
raise TypeError("mon must have same parent as self.")
while p:
if p_ExpVectorEqual(p, m, r) == 1:
return si2sa(p_GetCoeff(p, r), r, self._parent._base)
p = pNext(p)
return self._parent._base._zero_element

Value mon._poly == NULL is passed to Singular and then NULL dereference occurs:

https://github.com/Singular/Singular/blob/9cdbd118fe4118123a2dd5df729ca3a516305b64/libpolys/polys/monomials/p_polys.h#L1505-L1521

Environment

  • OS: Ubuntu 22.04, macOS Ventura 13.0.1
  • Sage Version: SageMath version 10.6.beta2

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@keltecc keltecc added the t: bug label Jan 3, 2025
@keltecc keltecc linked a pull request Jan 3, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant