Skip to content

gh-153144: Avoid checking errno for atan2 - #153148

Merged
vstinner merged 23 commits into
python:mainfrom
hpkfft:atan2
Nov 24, 2026
Merged

vstinner merged 23 commits into
python:mainfrom
hpkfft:atan2

Conversation

@hpkfft

@hpkfft hpkfft commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

The C23 standard states that for atan2 and atan2pi:

A domain error may occur if both arguments are zero.
A range error occurs if x is positive and nonzero y/x is too close to zero.

Since Python should not raise ValueError in either of these cases (i.e., when both arguments are zero or when the computation underflows), this PR avoids checking errno when calling these trig functions. As a bonus, math.atan2() is about 4% faster.


The statement about range error in the standard should, I think, be interpreted as:

A range error occurs if (x is positive) and (y/x is nonzero) and (y/x is too close to zero).

@bedevere-app

bedevere-app Bot commented Jul 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@hpkfft

hpkfft commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

If this PR is merged, it looks to me like #146402 can (and should) be reverted.

@picnixz
picnixz requested a review from skirpichev July 6, 2026 23:41

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I doubt that this is a right approach. We must handle error in same way for all libm functions.

Perhaps, m_atan2() (like m_log1p() we have currently) could be restored (see #122681) to workaround broken platform functions.

@hpkfft

hpkfft commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

We must handle error in same way for all libm functions.

The mathmodule currently has 3 ways of handling 1-argument libm functions:

  • Functions that cannot overflow (e.g., acos, atan) use math_1(arg, func, /*can_overflow=*/0, err_msg)
  • Functions that can overflow (e.g., cosh, exp) use math_1(arg, func, /*can_overflow=*/1, err_msg)
  • Functions that are known to set errno properly (e.g., erf, gamma) use math_1a(arg, func, err_msg)

Maybe it's not so bad to have math_2ne for 2-argument functions that are known not to need error checking (e.g., atan2) as well as the current math_2 (for the others).

@skirpichev

Copy link
Copy Markdown
Member

Perhaps, we could modify math_2() helper to check that errno!=EDOM, if inputs and output are finite. When domain error occurs - result should be nan.

But I think that a little wrapper for libm's atan2 is better, if we are going to add some workaround for the given issue.

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert unrelated changes.

Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
@hpkfft

hpkfft commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

@vstinner, can you look at this at your convenience?
I anticipate that this PR will also fix Solaris to return the correct results (#138573), so the relevant math/cmath tests will not need to be skipped on that platform. Please advise whether I should revert #146402 as part of this PR. If so, I'll do that and would then ask you to run on the buildbots.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this PR is merged, it looks to me like #146402 can (and should) be reverted.

I would prefer to update tests for Solaris in the same PR.

Comment thread Modules/cmathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/cmathmodule.c Resolved
@skirpichev

Copy link
Copy Markdown
Member

You also could use new helper function for copysign.

On another hand, I would prefer just remove errno stuff from math_2(), per #156145. It should be possible for all two-argument functions: atan2, atan2pi, copysign, remainder and fmod (which could utilize same wrapper).

Comment thread Modules/cmathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
@hpkfft

hpkfft commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

You also could use new helper function for copysign.

My thinking is only to do what is necessary to fix the bug (the incorrect results when building with Intel or Solaris math libraries). Then, I would recommend applying this fix to Python 3.14 and 3.15. With that in mind, I don't want to make any enhancements that are not necessary to fix the reported bug.

On another hand, I would prefer just remove errno stuff from math_2(), per #156145. It should be possible for all two-argument functions: atan2, atan2pi, copysign, remainder and fmod (which could utilize same wrapper).

Yes, 156145 can have a more ambitious goal.

Can you trigger the buildbots to run this PR? I am not able to check myself that it works on Solaris. (I do think it will work based on what I read in the Solaris bug report.)

@skirpichev

Copy link
Copy Markdown
Member

My thinking is only to do what is necessary to fix the bug

Then we loose chance of using new helper function for copysign().

Can you trigger the buildbots to run this PR?

!buildbot Solaris

@skirpichev

Copy link
Copy Markdown
Member

!buildbot Solaris

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @skirpichev for commit 5b6d005 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F153148%2Fmerge

The command will test the builders whose names match following regular expression: Solaris

The builders matched are:

  • SPARCv9 Oracle Solaris 11.4 PR

@hpkfft

hpkfft commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Then we loose chance of using new helper function for copysign().

OK, I'll add that commit tomorrow. Since it will be a separate commit, it can easily be reverted (or skipped when squashing all the commits before the final merge). The same is true for the comments Victor requested--they're in their own separate commit.

@skirpichev

Copy link
Copy Markdown
Member

Do not click the "Update branch" button without a good reason because it notifies everyone watching the PR that there are new changes, when there are not, and it uses up limited CI resources.

@picnixz

picnixz commented Oct 2, 2026

Copy link
Copy Markdown
Member

We unfortunately get past 3.13 release and it is not security-only. I am ok with putting it in 3.16 and maybe 3.14/3.15 but I would be interested in having some NumPy's opinion just in case (cc @ngoldbaum). I think fixing the phase/atan2 inconsistency takes precedence over other behavioral change concerns.

@ngoldbaum

Copy link
Copy Markdown
Contributor

Thanks for the ping. NumPy already behaves the way CPython will behave after this PR. The only difference is that NumPy can optionally surface the underflow, and it reports it as an underflow, never as an overflow.

@picnixz

picnixz commented Oct 2, 2026

Copy link
Copy Markdown
Member

Yeah so this matches what users will expect. I am fine with this PR then! thanks for doing it.

Comment thread Modules/cmathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@skirpichev PTAL again

Comment thread Modules/cmathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c
Comment on lines +1064 to +1065
/* variant of math_2, to be used when the function being wrapped is known NOT
to need error checking (i.e., no overflow, invalid, or divide-by-zero). */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest expand this with atan2() example and remove other comments below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with leaving comments as they are: general comment on math_2ne(), and detailed comments on functions using math_2ne() below.

Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c Resolved
Comment thread Modules/mathmodule.c
C89 but for which HUGE_VAL is not an infinity.
For most two-argument functions (copysign, fmod, hypot, atan2)
For most two-argument functions (fmod, hypot)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the only remaining math_2()-type function is remainder()...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (fmod, hypot) in the comment above refers to the C math library functions.
Unless I've missed it, Python does not use the C math library function double remainder(double, double), so I don't think we should add it to this comment.

@skirpichev skirpichev Oct 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (fmod, hypot) in the comment above refers to the C math library functions.

I don't think so. Before 53876d9, both fmod and hypot were using FUNC2 macro.

Python does not use the C math library function double remainder(double, double)

Yes, but m_remainder (name in style m_log and co, suggesting a workarounded libm's function) is just an implementation of Annex F remainder.

Perhaps, nowadays we could replace this function by standard library call: at least this works well for me locally. See previous attempt of removing legacy stuff: #101679

Edit: FYI, #158826.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. But it seems sufficiently unrelated as to allow this PR to be merged without further tweaks to the comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand well if the comment should be updated or not. At least, I can say that with this change, the last math_2() user is FUNC2(remainder, m_remainder, ...). math.fmod() and math.hypot() no longer use it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, I had re-flowed this comment to avoid the first line looking silly for being short, and I was asked to revert that change, which I did.
This PR does not affect fmod, hypot, or remainder. It seems inconsistent to consider a substantive change to the comment as part of this PR if a white-space change was frowned upon.

@hpkfft

hpkfft commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@Yhg1s, please consider whether you'd like this PR made into a patch for 3.13. I see that:

The only changes made to a security branch are those fixing issues exploitable by attackers such as crashes, privilege escalation and, optionally, other issues such as denial of service attacks.

Using Linux/GNU, and taking a complex point z that is very close to a large, positive, real number:

>>> import cmath
>>> z = complex(1e200, 1e-200)  # Almost on positive real axis
>>> cmath.phase(z)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    cmath.phase(z)
    ~~~~~~~~~~~^^^
OverflowError: math range error

The correct phase angle for z is 0.0. Since the correct result of phase() is in the range [-Pi, Pi], overflow is (should be) impossible. A Python programmer would not be expected to use a try block. I don't know what you consider to be a crash or a DoS, but carefully chosen inputs, as demonstrated above, will crash a Python program on all currently shipping versions of Python. This is hard to catch in testing; note that Python itself missed it.

I cannot resist mentioning that the Ariane 5 launch failure might have been avoided if, instead of an exception, the calculated value had saturated, or wrapped, or been set to zero, etc. The result was not used after lift-off, so the return value didn't matter. But since exceptions that ought not to happen cause crashes, ....

@StanFromIreland

Copy link
Copy Markdown
Member

A Python programmer would not be expected to use a try block. I don't know what you consider to be a crash or a DoS, but carefully chosen inputs, as demonstrated above, will crash a Python program on all currently shipping versions of Python.

Unexpected exceptions aren't security vulnerabilities, they're regular bugs. I don't see a reason, or any user reports, to backport this.

Comment thread Modules/mathmodule.c Resolved

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Honestly, I don't understand the subtle details of this change, but overall it looks good to me.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skirpichev: Are you fine with the current PR or do you still have concerns?

@skirpichev

Copy link
Copy Markdown
Member

Are you fine with the current PR or do you still have concerns?

Yes, most of my comments are still valid, e.g. this thread or this (where edited comment is just misleading now).

I think that we shouldn't add references to issue in the code. Or add specific comments on concrete FUNC2NE use cases. We could drop math_2(), as it's used just for one function. We also could reduce patch for cmath.phase: keep L1007-1008, just drop errno handling.

But all that are minor nitpicks, feel free to ignore.

@vstinner
vstinner merged commit 45d64ef into python:main Nov 24, 2026
57 checks passed
@vstinner

Copy link
Copy Markdown
Member

I merged your fix, thanks @hpkfft!

This PR changes math and cmath functions in a subtle way, I'm not comfortable to consider them as bugfixes and backport them. Python has the same behavior for several years, users had to learn how to work around Python math/cmath issues anyway in the meanwhile. Backporting the change to 3.14 and 3.15 would change the behavior in bugfix 3.14.x and 3.15.x releases which would be bad.

@skirpichev:

But all that are minor nitpicks, feel free to ignore.

Ok, I chose to ignore these nitpicks and merge the PR as it is to unblock this fix. You can open a follow-up PR. For example, "We could drop math_2()" would be better done as a separated change, since it's not directly directly to this errno/OverflowError change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants