Opened 9 years ago

Closed 8 years ago

#14895 closed defect (fixed)

Segfault when exponentiating an integer with I.pyobject()

Reported by: eviatarbach Owned by: burcin
Priority: critical Milestone: sage-6.2
Component: symbolics Keywords: days57
Cc: Merged in:
Authors: Ralf Stephan Reviewers: Volker Braun
Report Upstream: N/A Work issues:
Branch: 4a1273f (Commits, GitHub, GitLab) Commit: 4a1273f25f872da4302978ce4ffc671f89389179
Dependencies: Stopgaps:

Status badges

Description (last modified by rws)

Try, for example, 2^I.pyobject().

This is a critical issue since any method of a class inheriting from BuiltinFunction will receive the I pyobject if I is passed as an argument to the function.

This in turn leads to segfaults like:

sage: K.<a> = NumberField(x^2+1)
sage: 2^a

or

sage: K.<sqrt2> = QuadraticField(2)
sage: 2^sqrt2

Change History (14)

comment:1 Changed 9 years ago by vbraun

Infinite recursion

#473 0x00007fffdf203eaf in __pyx_pf_4sage_5rings_8rational_8Rational_100__pow__ (
    __pyx_v_self=<sage.rings.rational.Rational at remote 0x576f470>, 
    __pyx_v_n=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, __pyx_v_dummy=<optimized out>) at sage/rings/rational.c:18767
#474 0x00007ffff7c3f806 in ternary_op (op_name=<synthetic pointer>, op_slot=48, z=None, 
    w=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, v=<sage.rings.rational.Rational at remote 0x576f470>) at Objects/abstract.c:1065
#475 PyNumber_Power (v=<optimized out>, w=<optimized out>, z=None) at Objects/abstract.c:1255
#476 0x00007fffd4847e64 in __pyx_pf_4sage_5rings_12number_field_20number_field_element_18NumberFieldElement_68__pow__ (__pyx_v_dummy=<optimized out>, __pyx_v_exp=<optimized out>, 
    __pyx_v_base=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x576e890>) at sage/rings/number_field/number_field_element.cpp:14267
#477 __pyx_pw_4sage_5rings_12number_field_20number_field_element_18NumberFieldElement_69__pow__ (
    __pyx_v_base=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x576e890>, 
    __pyx_v_exp=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, __pyx_v_dummy=<optimized out>)
    at sage/rings/number_field/number_field_element.cpp:14010
#478 0x00007ffff7c3f84c in ternary_op (op_name=<synthetic pointer>, op_slot=48, z=None, 
    w=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, 
    v=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x576e890>) at Objects/abstract.c:1065
#479 PyNumber_Power (v=<optimized out>, 
    w=w@entry=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, z=None) at Objects/abstract.c:1255
#480 0x00007fffdf203eaf in __pyx_pf_4sage_5rings_8rational_8Rational_100__pow__ (
    __pyx_v_self=<sage.rings.rational.Rational at remote 0x576f410>, 
    __pyx_v_n=<sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic at remote 0x25357d0>, __pyx_v_dummy=<optimized out>) at sage/rings/rational.c:18767

comment:2 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:3 Changed 8 years ago by rws

  • Description modified (diff)

Pingpong between parent and base.

rings/rational.pyx (Rational._pow_()):

            if PY_TYPE_CHECK(n, Element):
                return (<Element>n)._parent(self)**n
            try:
                return n.parent()(self)**n

rings/number_field/number_field_element.pyx (NumberFieldElement._pow_()):

            from sage.symbolic.ring import SR
            try:
                res = QQ(base)**exp
            except TypeError:
                pass

So which one to fix?

Last edited 8 years ago by rws (previous) (diff)

comment:4 Changed 8 years ago by rws

  • Branch set to u/rws/ticket/14895
  • Created changed from 07/15/13 23:44:22 to 07/15/13 23:44:22
  • Modified changed from 03/02/14 08:55:09 to 03/02/14 08:55:09

comment:5 Changed 8 years ago by rws

  • Commit set to 37381336f7324de5ab98224459be69d1918da3dc
  • Status changed from new to needs_review

This passes all tests in rings, and adds some tests. If you change it please keep the tests.


New commits:

3738133Trac #14895: remove code prone to infinite loops

comment:6 Changed 8 years ago by rws

  • Authors set to Ralf Stephan

comment:7 Changed 8 years ago by git

  • Commit changed from 37381336f7324de5ab98224459be69d1918da3dc to 29a7db39373dce5c930cf5cbbd5a845321386cec

Branch pushed to git repo; I updated commit sha1. New commits:

4c34aa6Merge branch 'develop' into ticket/14895
29a7db3Merge branch 'develop' into ticket/14895

comment:8 Changed 8 years ago by vbraun

  • Keywords days57 added
  • Reviewers set to Volker Braun

Looks good to me, but can you remove the commented-out block? If you want to know what was there before there is always git. Lots of code has been edited at one point, if everybody would have left commented-out crap around then the source would be really hard to read.

comment:9 Changed 8 years ago by rws

  • Branch changed from u/rws/ticket/14895 to u/rws/ticket/14895-1
  • Commit changed from 29a7db39373dce5c930cf5cbbd5a845321386cec to 17af5a15f01fcefc85bba52f1de00308d83f200d
  • Status changed from needs_review to positive_review

New commits:

17af5a1Trac 14895: remove code prone to infinite loops

comment:10 Changed 8 years ago by vbraun

sage -t --long src/sage/symbolic/expression.pyx
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 3207, in sage.symbolic.expression.Expression.__pow__
Failed example:
    t^(1/2)
Expected:
    2*sqrt(2)
Got:
    sqrt(8)
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 3209, in sage.symbolic.expression.Expression.__pow__
Failed example:
    (t^2)^(1/4)
Expected:
    2*4^(1/4)
Got:
    64^(1/4)
**********************************************************************

So that what it was about, we want the simplified symbolic form. We should just try QQ(base)**QQ(exp), the QQ.__pow__ method will then find the simplified form.

comment:11 Changed 8 years ago by vbraun

  • Status changed from positive_review to needs_work

comment:12 Changed 8 years ago by rws

  • Branch changed from u/rws/ticket/14895-1 to u/rws/ticket/14895-2
  • Commit changed from 17af5a15f01fcefc85bba52f1de00308d83f200d to 0b6149bed7de996d0c12f24380514e992a83f32f
  • Status changed from needs_work to needs_review

Indeed, that works, and tests OK --long in symbolic/ and rings/ and not long elsewhere.


New commits:

0b6149bTrac 14895: fix segfault

comment:13 Changed 8 years ago by git

  • Commit changed from 0b6149bed7de996d0c12f24380514e992a83f32f to 4a1273f25f872da4302978ce4ffc671f89389179

Branch pushed to git repo; I updated commit sha1. New commits:

4a1273f14895: add doctest

comment:14 Changed 8 years ago by vbraun

  • Branch changed from u/rws/ticket/14895-2 to 4a1273f25f872da4302978ce4ffc671f89389179
  • Resolution set to fixed
  • Status changed from needs_review to closed
Note: See TracTickets for help on using tickets.