[MNT]: c++11 narrowing error when building for 32 bit targets · Issue #30413 · matplotlib/matplotlib · GitHub
Skip to content

[MNT]: c++11 narrowing error when building for 32 bit targets #30413

Description

@z-erica

Summary

this happens because get_height() and get_width() return an unsigned int, which can be losslessly narrowed into a ssize_t in 64 bit targets but not in 32 bit targets

FAILED: [code=1] src/_backend_agg.cpython-313-powerpc-linux-musl.so.p/_backend_agg_wrapper.cpp.o
clang++ -Isrc/_backend_agg.cpython-313-powerpc-linux-musl.so.p -Isrc -I../src -I../extern/agg24-svn/include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-6 -I/usr/share/pkgconfig/../../lib/python3.13/site-packages/pybind11/share/pkgconfig/../../include -I/usr/include/python3.13 -fvisibility=hidden -fvisibility-inlines-hidden -flto -fdiagnostics-color=always -DNDEBUG -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++17 -O3 -ffile-prefix-map=/builddir/python-matplotlib-3.10.5=. -Wformat -Werror=format-security -ftrivial-auto-var-init=zero -fno-omit-frame-pointer -mtune=G4 -O2 -g1 -fPIC -DWITH_GZFILEOP -pthread -MD -MQ src/_backend_agg.cpython-313-powerpc-linux-musl.so.p/_backend_agg_wrapper.cpp.o -MF src/_backend_agg.cpython-313-powerpc-linux-musl.so.p/_backend_agg_wrapper.cpp.o.d -o src/_backend_agg.cpython-313-powerpc-linux-musl.so.p/_backend_agg_wrapper.cpp.o -c ../src/_backend_agg_wrapper.cpp
../src/_backend_agg_wrapper.cpp:253:17: error: non-constant-expression cannot be narrowed from type 'unsigned int' to 'int' in initializer list [-Wc++11-narrowing]
253 | renderer->get_height(),
| ^~~~~~~~~~~~~~~~~~~~~~
../src/_backend_agg_wrapper.cpp:253:17: note: insert an explicit cast to silence this issue
253 | renderer->get_height(),
| ^~~~~~~~~~~~~~~~~~~~~~
| static_cast<int>( )
../src/_backend_agg_wrapper.cpp:254:17: error: non-constant-expression cannot be narrowed from type 'unsigned int' to 'int' in initializer list [-Wc++11-narrowing]
254 | renderer->get_width(),
| ^~~~~~~~~~~~~~~~~~~~~
../src/_backend_agg_wrapper.cpp:254:17: note: insert an explicit cast to silence this issue
254 | renderer->get_width(),
| ^~~~~~~~~~~~~~~~~~~~~
| static_cast<int>( )
../src/_backend_agg_wrapper.cpp:258:17: error: non-constant-expression cannot be narrowed from type 'unsigned int' to 'int' in initializer list [-Wc++11-narrowing]
258 | renderer->get_width() * 4,
| ^~~~~~~~~~~~~~~~~~~~~~~~~
../src/_backend_agg_wrapper.cpp:258:17: note: insert an explicit cast to silence this issue
258 | renderer->get_width() * 4,
| ^~~~~~~~~~~~~~~~~~~~~~~~~
| static_cast<int>( )
3 errors generated.

full build log at https://build.chimera-linux.org/#/builders/7/builds/1457/steps/4/logs/pkg_user_python-matplotlib_3_10_5-r0

Proposed fix

potentially just adding the static casts as the compiler note says

diff -ruN a/src/_backend_agg_wrapper.cpp b/src/_backend_agg_wrapper.cpp
--- a/src/_backend_agg_wrapper.cpp	2025-07-31 19:00:28.000000000 +0200
+++ b/src/_backend_agg_wrapper.cpp	2025-08-10 21:50:30.146295804 +0200
@@ -250,12 +250,12 @@
 
         .def_buffer([](RendererAgg *renderer) -> py::buffer_info {
             std::vector<py::ssize_t> shape {
-                renderer->get_height(),
-                renderer->get_width(),
+                static_cast<py::ssize_t>(renderer->get_height()),
+                static_cast<py::ssize_t>(renderer->get_width()),
                 4
             };
             std::vector<py::ssize_t> strides {
-                renderer->get_width() * 4,
+                static_cast<py::ssize_t>(renderer->get_width() * 4),
                 4,
                 1
             };

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions