Repository navigation
Closed
Description
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
Yhg1s commentedon Jul 21, 2021
Objects/complexobject.c's complex_pow uses undefined behaviour, by casting a float of unknown magnitude to a long:
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.
14 remaining items
mdickinson commentedon Aug 4, 2021
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:
And that's it. (Well, not quite: there's a footnote, which says:
)
seberg commentedon Aug 4, 2021
Thanks for looking into it!
cpowis indeed complicated. We had some discussion in NumPy about0**ybranch 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 :(.
mdickinson commentedon Aug 15, 2021
@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?
pablogsal commentedon Aug 15, 2021
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!
pablogsal commentedon Aug 15, 2021
I'm keeping track to the 3.10 backports to the second rc so could you add me as a reviewer for the backport?
mdickinson commentedon Aug 16, 2021
Thanks, Pablo!
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.
mdickinson commentedon Aug 17, 2021
New changeset 4b9a2dc by Mark Dickinson in branch 'main':
bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772)
4b9a2dc
mdickinson commentedon Aug 17, 2021
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
mdickinson commentedon Aug 17, 2021
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
mdickinson commentedon Aug 17, 2021
Re-closing; we should be good now.