Skip to content

Undefined behaviour in Objects/complexobject.c's complex_pow #88864

Closed

Description

@Yhg1s
BPO 44698
Nosy @tim-one, @Yhg1s, @gpshead, @mdickinson, @ambv, @seberg, @pablogsal, @miss-islington
PRs
  • bpo-44698: Fix undefined behaviour in complex exponentiation. #27278
  • [3.10] bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278) #27366
  • [3.9] bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278) #27367
  • bpo-44698: Restore complex pow behaviour for small integral exponents #27772
  • [3.10] bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772) #27796
  • [3.9] bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772) #27797
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/mdickinson'
    closed_at = <Date 2021-08-17.18:22:58.553>
    created_at = <Date 2021-07-21.15:06:49.955>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11', '3.7']
    title = "Undefined behaviour in Objects/complexobject.c's complex_pow"
    updated_at = <Date 2021-08-17.18:22:58.552>
    user = 'https://github.com/Yhg1s'

    bugs.python.org fields:

    activity = <Date 2021-08-17.18:22:58.552>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2021-08-17.18:22:58.553>
    closer = 'mark.dickinson'
    components = []
    creation = <Date 2021-07-21.15:06:49.955>
    creator = 'twouters'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44698
    keywords = ['patch']
    message_count = 19.0
    messages = ['397947', '398239', '398249', '398250', '398928', '398930', '398932', '398935', '398937', '398940', '398941', '399610', '399620', '399621', '399666', '399767', '399775', '399776', '399779']
    nosy_count = 8.0
    nosy_names = ['tim.peters', 'twouters', 'gregory.p.smith', 'mark.dickinson', 'lukasz.langa', 'seberg', 'pablogsal', 'miss-islington']
    pr_nums = ['27278', '27366', '27367', '27772', '27796', '27797']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44698'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. Yhg1s commented on Jul 21, 2021

      @Yhg1s
      MemberAuthor

      Objects/complexobject.c's complex_pow uses undefined behaviour, by casting a float of unknown magnitude to a long:

          int_exponent = (long)exponent.real;

      At Google we build with clang and -fsanitize=float-cast-overflow by default, which catches this particular kind of undefined behaviour. We didn't notice, however, because the only code we've come across that exercises this behaviour was a commented-out test in test_complex, which was uncommented in 3.8. Running the test, or just '1e19+1j ** 1e19', is enough to trigger the undefined behaviour. I'll prepare a PR to fix it.

    2. added
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      3.10 (EOL)
      3.11
      type-bug
      on Jul 21, 2021
    3. 14 remaining items

    4. mdickinson commented on Aug 4, 2021

      @mdickinson
      Member

      brave or foolhardy enough to try to figure out what all of the special case results *should* be for complex pow

      Addendum: even the C standards give up at this point. For everything else the special cases are spelled out in detail, but for cpow, from §G.6.4.1 of C17 (in Annex G), we have:

      The cpow functions raise floating-point exceptions if appropriate for the calculation of the parts of the result, and may also raise spurious floating-point exceptions.

      And that's it. (Well, not quite: there's a footnote, which says:

      This allows cpow(z, c) to be implemented as cexp(cclog(z)) without precluding implementations that treat special cases more carefully.

      )

    5. seberg commented on Aug 4, 2021

      sebergmannequin
      Mannequin

      Thanks for looking into it!

      cpow is indeed complicated. We had some discussion in NumPy about 0**y branch cuts (I did yet not finish it, because thinking about the branch cuts is tricky).

      It is almost reassuring that the C standard also hedges out :). Although, it means that there is no reliance on math libraries if we want to treat the special cases more carefully :(.

    6. mdickinson commented on Aug 15, 2021

      @mdickinson
      Member

      @pablo: I'm adding you to the nosy list to get your opinion on whether (the backport of) #71959 can go into 3.10.0.rc2.

      Summary: #71465 (which I reviewed and approved) and its backports fixed some undefined behaviour in complex exponentiation, but also introduced a behaviour change that affected NumPy: some exponentiation expressions that returned a result in 3.10.0b4 raise an OverflowError in 3.10.0.rc1.

      #71959 reverts the behaviour change but keeps the fix for the undefined behaviour. Can the backport of #71959 to the 3.10 branch go in before 3.10.0 final?

    7. pablogsal commented on Aug 15, 2021

      @pablogsal
      Member

      #71959 reverts the behaviour change but keeps the fix for the undefined behaviour. Can the backport of #71959 to the 3.10 branch go in before 3.10.0 final?

      Thanks for checking with me!

      Given that this is fixing a bug that we introduced in rc1, I think it makes sense to backport to fix it. I have reviewed the code in #71959 and is contained enough, so I think is a good idea to go ahead.

      Thanks Mark for all the work!

    8. pablogsal commented on Aug 15, 2021

      @pablogsal
      Member

      I'm keeping track to the 3.10 backports to the second rc so could you add me as a reviewer for the backport?

    9. mdickinson commented on Aug 16, 2021

      @mdickinson
      Member

      Thanks, Pablo!

      could you add me as a reviewer for the backport

      Will do! I'll waiting on review for the PR against the main branch, but hope to get the backport PR up by this coming weekend at the latest.

    10. mdickinson commented on Aug 17, 2021

      @mdickinson
      Member

      New changeset 4b9a2dc by Mark Dickinson in branch 'main':
      bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772)
      4b9a2dc

    11. mdickinson commented on Aug 17, 2021

      @mdickinson
      Member

      New changeset 3f81e96 by Miss Islington (bot) in branch '3.10':
      bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772) (GH-27796)
      3f81e96

    12. mdickinson commented on Aug 17, 2021

      @mdickinson
      Member

      New changeset beb3a83 by Miss Islington (bot) in branch '3.9':
      bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772) (GH-27797)
      beb3a83

    13. mdickinson commented on Aug 17, 2021

      @mdickinson
      Member

      Re-closing; we should be good now.

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.10 (EOL)3.113.7 (EOL)3.8 (EOL)3.9 (EOL)type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions