Skip to content
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

spi: Suppress null pointer warning #23994

Merged
merged 1 commit into from
Nov 21, 2024
Merged

spi: Suppress null pointer warning #23994

merged 1 commit into from
Nov 21, 2024

Conversation

MaEtUgR
Copy link
Member

@MaEtUgR MaEtUgR commented Nov 20, 2024

Solved Problem

Depending on defines px4_spi_buses can be NULL but often it's not and in those cases the compiler correctly warns about it.
Reported in #23869 (comment) and #23963 (comment)

Solution

Suppress the warning.

Test coverage

Locally built fmu-v4 on Ubuntu 24.04 with the packaged GCC 13.2.1

Copy link

FLASH Analysis

px4_fmu-v5x
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +8  +0.0%      +8    .text
  +0.0%      +6  [ = ]       0    .debug_line
    +0.4%      +6  [ = ]       0    ../../platforms/common/spi.cpp
  -0.0%      -6  [ = ]       0    [Unmapped]
  +0.0%      +8  +0.0%      +8    TOTAL

px4_fmu-v6x
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +8  +0.0%      +8    .text
  +0.0%      +6  [ = ]       0    .debug_line
    +0.5%      +6  [ = ]       0    ../../platforms/common/spi.cpp
  -0.0%      -6  [ = ]       0    [Unmapped]
  +0.0%      +8  +0.0%      +8    TOTAL

platforms/common/spi.cpp Outdated Show resolved Hide resolved
@PetervdPerk-NXP
Copy link
Member

We could avoid the use #pragma if we just check for the nullptr before looping.

if (px4_spi_buses == nullptr) {
   return -1;
}

Depending on defines px4_spi_buses can be NULL
but often it's not and in those cases the
compiler correctly warns about it.
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Nov 20, 2024

@PetervdPerk-NXP I generally agree but your suggestion does the same check there already is just in a sperate condition so it produces the same error.
image

Another option would be to disable that warning for the entire compile unit but I'd try to avoid that more than the pragma because it might hide actual mistakes.

Copy link

FLASH Analysis

px4_fmu-v5x
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +8  +0.0%      +8    .text
  +0.0%      +7  [ = ]       0    .debug_line
    +0.5%      +7  [ = ]       0    ../../platforms/common/spi.cpp
  -0.0%      -7  [ = ]       0    [Unmapped]
  +0.0%      +8  +0.0%      +8    TOTAL

px4_fmu-v6x
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +8  +0.0%      +8    .text
  +0.0%      +7  [ = ]       0    .debug_line
    +0.5%      +7  [ = ]       0    ../../platforms/common/spi.cpp
  -0.0%      -7  [ = ]       0    [Unmapped]
  +0.0%      +8  +0.0%      +8    TOTAL

@slgrobotics
Copy link
Contributor

slgrobotics commented Nov 20, 2024

Just FYI - Ubuntu 24.04, main branch:

BEFORE your changes
sudo apt install g++-arm-linux-gnueabi

  • when running "make emlid_navio2", the build fails:
[437/1121] Building CXX object platforms/common/CMakeFiles/px4_platform.dir/spi.cpp.o
FAILED: platforms/common/CMakeFiles/px4_platform.dir/spi.cpp.o 
/usr/bin/arm-linux-gnueabihf-g++ -DCONFIG_ARCH_BOARD_EMLID_NAVIO2 -D__CUSTOM_FILE_IO__ -D__PX4_LINUX -D__PX4_POSIX -D__STDC_FORMAT_MACROS -Dnoreturn_function="__attribute__((noreturn))" -I/home/sergei/src0/PX4-Autopilot/boards/emlid/navio2/src -I/home/sergei/src0/PX4-Autopilot/platforms/posix/src/px4/common/include -I/home/sergei/src0/PX4-Autopilot/build/emlid_navio2_default -I/home/sergei/src0/PX4-Autopilot/build/emlid_navio2_default/src/lib -I/home/sergei/src0/PX4-Autopilot/platforms/posix/src/px4/generic/generic/include -I/home/sergei/src0/PX4-Autopilot/platforms/common -I/home/sergei/src0/PX4-Autopilot/platforms/common/include -I/home/sergei/src0/PX4-Autopilot/src -I/home/sergei/src0/PX4-Autopilot/src/include -I/home/sergei/src0/PX4-Autopilot/src/lib -I/home/sergei/src0/PX4-Autopilot/src/lib/matrix -I/home/sergei/src0/PX4-Autopilot/src/modules -I/home/sergei/src0/PX4-Autopilot/boards/emlid/navio2/include -I/home/sergei/src0/PX4-Autopilot/platforms/posix/include -I/home/sergei/src0/PX4-Autopilot/build/emlid_navio2_default/external/Install/include -O2 -g -DNDEBUG -std=gnu++14 -fPIC -g -fdata-sections -ffunction-sections -fomit-frame-pointer -fmerge-all-constants -fno-signed-zeros -fno-trapping-math -freciprocal-math -fno-math-errno -fno-strict-aliasing -fvisibility=hidden -include visibility.h -Wall -Wextra -Werror -Warray-bounds -Wcast-align -Wdisabled-optimization -Wdouble-promotion -Wfatal-errors -Wfloat-equal -Wformat-security -Winit-self -Wlogical-op -Wpointer-arith -Wshadow -Wuninitialized -Wunknown-pragmas -Wunused-variable -Wno-missing-field-initializers -Wno-missing-include-dirs -Wno-unused-parameter -fdiagnostics-color=always -Wno-stringop-truncation -fno-builtin-printf -fno-strength-reduce -Wformat=1 -Wunused-but-set-variable -Wno-format-truncation -fcheck-new -Wreorder -Wno-overloaded-virtual -fno-rtti -MD -MT platforms/common/CMakeFiles/px4_platform.dir/spi.cpp.o -MF platforms/common/CMakeFiles/px4_platform.dir/spi.cpp.o.d -o platforms/common/CMakeFiles/px4_platform.dir/spi.cpp.o -c /home/sergei/src0/PX4-Autopilot/platforms/common/spi.cpp
/home/sergei/src0/PX4-Autopilot/platforms/common/spi.cpp: In function ‘int px4_find_spi_bus(uint32_t)’:
/home/sergei/src0/PX4-Autopilot/platforms/common/spi.cpp:90:59: error: the address of ‘px4_spi_buses’ will never be NULL [-Werror=address]
   90 |         for (int i = 0; ((px4_spi_bus_t *) px4_spi_buses) != nullptr && i < SPI_BUS_MAX_BUS_ITEMS; ++i) {
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
compilation terminated due to -Wfatal-errors.
cc1plus: all warnings being treated as errors
[446/1121] Building CXX object platforms/posix/src/px4/common/px4_daemon/CMakeFiles/px4_daemon.dir/pxh.cpp.o
ninja: build stopped: subcommand failed.
make: *** [Makefile:232: emlid_navio2] Error 1
  • same happens on "make emlid_navio2_arm64" - that needed some adjustments:
    sudo apt install g++-aarch64-linux-gnu
    add file boards/emlid/navio2/arm64.px4board containing a single line:
CONFIG_BOARD_TOOLCHAIN="aarch64-linux-gnu"

Both ARM and ARM64 compilers treat this line as fatal error

WITH YOUR CHANGES APPLIED

  • both ARM and ARM64 builds complete fine

Thanks!

@dagar
Copy link
Member

dagar commented Nov 21, 2024

I'd also prefer to not carry the pragma, but perhaps we can revisit this once the updated toolchain(s) are in place and we can easily test everything at once.

@dagar dagar merged commit b8c2805 into main Nov 21, 2024
60 of 62 checks passed
@dagar dagar deleted the spi-warning-fix branch November 21, 2024 03:36
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Nov 21, 2024

we can easily test everything at once

@dagar I'm happy to test. Just give me a hint at what would be better.

both ARM and ARM64 builds complete fine

@slgrobotics Thanks for verifying. I ran these builds on my test setup as well now 👍 It's not a surprise since both compilers are GCC. Should we add the build 64Bit build? Is it in use anywhere?

@slgrobotics
Copy link
Contributor

@MaEtUgR - the 64-bit ARM build is important for Raspberry Pi 4 and 5 under Ubuntu. I use it under modified navio2, there's another one too:

make emlid_navio2_arm64
make scumaker_pilotpi_arm64

I opened #24006 to make sure it is available for navio2

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.

5 participants