Skip to content

MAINT: Avoid dereferencing/strict aliasing warnings #28915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025

Conversation

charris
Copy link
Member

@charris charris commented May 6, 2025

Backport of #28892.

Happens in npy_math.h during complex casts in for C++ runs

I am aware that this file went through many iterations and complex support is a constant headache, and if there is any more appetite left, I'd like to offer another minor change.

We have been having some warnings coming from non-C99-compliant compilers and C++ runs over SciPy since NumPy 2.0. This is mainly due to the npy_crealX/npy_cimagX functions, casting the input struct first and then dereferencing it in the new type, causing the strict-aliasing (type punning) rule to fire.

Here is a recent example how this manifests itself in the build process https://github.com/scipy/scipy/actions/runs/14770722795/job/41470198162#step:6:300

[snip]
 In file included from ..\scipy\_build_utils\src/npy_2_complexcompat.h:4,
                 from ../scipy/special/xsf_wrappers.h:13,
                 from ../scipy/special/xsf_wrappers.cpp:1:
C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\site-packages\numpy\_core\include/numpy/npy_math.h: In function 'npy_longdouble npy_creall(npy_clongdouble)':
C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\site-packages\numpy\_core\include/numpy/npy_math.h:422:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  422 |     return ((longdouble_t *) &z)[0];
      |            ~^~~~~~~~~~~~~~~~~~~~ 
[snip]

I don't know if the SciPy code is also commiting some C++ sins but the NumPy code can do better to avoid strange bugs. I don't claim to have the right solution but seems like the most obvious one to me to let the compiler handle the possible padding/endianness issues with a straight struct field access. However I don't have a strong opinion since I'm not a C++ expert.

This is also somewhat related to our ambition to be warning free at some point.

  • MAINT: Avoid type-punning complex casts in npy_math.h

  • MAINT: Add missing typecast in npy_cimagl

…sts in `npy_math.h` for C++ runs (numpy#28892)

* MAINT: Avoid type-punning complex casts in npy_math.h

* MAINT: Add missing typecast in npy_cimagl
@charris charris added this to the 2.2.6 release milestone May 6, 2025
@charris charris added 03 - Maintenance 08 - Backport Used to tag backport PRs labels May 6, 2025
@charris charris merged commit 6c42775 into numpy:maintenance/2.2.x May 6, 2025
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance 08 - Backport Used to tag backport PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy