Title: Undefined behaviour in Objects/complexobject.c's complex_pow
Type: behavior Stage: patch review
Components: Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: twouters Nosy List: gregory.p.smith, mark.dickinson, tim.peters, twouters
Priority: normal Keywords: patch

Created on 2021-07-21 15:06 by twouters, last changed 2021-07-21 23:59 by rhettinger.

Pull Requests
URL Status Linked Edit
PR 27278 open twouters, 2021-07-21 15:26
Messages (1)
msg397947 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2021-07-21 15:06
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.
Date User Action Args
2021-07-21 23:59:02rhettingersetnosy: + tim.peters
2021-07-21 23:46:07rhettingersetnosy: + mark.dickinson
2021-07-21 15:26:21twouterssetkeywords: + patch
stage: patch review
pull_requests: + pull_request25823
2021-07-21 15:06:49twouterscreate