Skip to content

Segmentation fault in monomial_coefficient #39255

@keltecc

Description

@keltecc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions