Compiler warning about overflow in function vPortGetHeapStats from heap_4.c

If the macro configTICK_TYPE_WIDTH_IN_BITS set to TICK_TYPE_WIDTH_64_BITS, the compiler will show warning like this:

myProject/FreeRTOS-Kernel/portable/MemMang/heap_4.c: In function 'vPortGetHeapStats':
myProject/FreeRTOS-Kernel/portable/GCC/ARM_CM4F/portmacro.h:74:40: warning: conversion from 'long long unsigned int' to 'size_t' {aka 'unsigned int'} changes value from '18446744073709551615' to '4294967295' [-Woverflow]
   74 |     #define portMAX_DELAY              ( TickType_t ) 0xffffffffffffffffULL
      |                                        ^
myProject/FreeRTOS-Kernel/portable/MemMang/heap_4.c:575:50: note: in expansion of macro 'portMAX_DELAY'
  575 |     size_t xBlocks = 0, xMaxSize = 0, xMinSize = portMAX_DELAY; /* portMAX_DELAY used as a portable way of getting the maximum value. */
      |                                                  ^~~~~~~~~~~~~

Target device is STM32F407IGHx, Compiler is arm-none-eabi-gcc 14.2.1
FreeRTOS-Kernel version is the same as main branch on github (commit hash is e9440d4079894e0d6c52ce80e4c79fc072743d9d)

I have learned the comment after the local variable xMinSize. In this project, the
overflowed value just right equals to UINT32_MAX and the width of size_t is 32. However, if we set configTICK_TYPE_WIDTH_IN_BITS to TICK_TYPE_WIDTH_16_BITS, then the value of portMAX_DELAY is UINT16_MAX, it’s not the maximum value of size_t type variable.

To fix it, I suggest to change it into:

size_t xBlocks = 0, xMaxSize = 0, xMinSize = SIZE_MAX;  /* in stdint.h */

or

size_t xBlocks = 0, xMaxSize = 0, xMinSize = ( ( size_t ) -1 );

If this suggestion could works, I can create a new PR on github.

Out of curiosity - what command are you using to compile? You may need to specify the bit-width of your target system through the use of -m64 (for your compiler this may be `-Wm64).

The compile command is

arm-none-eabi-gcc -DprojCOVERAGE_TEST=0 -ImyProject/FreeRTOS-Kernel/include/. -ImyProject/stm32f407ighx -ImyProject/FreeRTOS-Kernel/portable/GCC/ARM_CM4F  -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard  -fdata-sections -ffunction-sections -g -std=gnu11 -fdiagnostics-color=always -o FreeRTOS-Kernel/CMakeFiles/freertos_kernel.dir/portable/MemMang/heap_4.c.obj -c myProject/FreeRTOS-Kernel/portable/MemMang/heap_4.c

I’m using CMake in fact, and this command is copied from compile_commands.json file exported by CMake.

I have tried to add compile option -m64 or -Wm64, but it shows as follows:

arm-none-eabi-gcc.exe: error: unrecognized command-line option '-m64'
arm-none-eabi-gcc.exe: error: unrecognized command-line option '-Wm64'

My target device STM32F4 is based on Arm Cortex-M4. It’s 32-bit CPU architecture and unable to execute 64-bit instructions.

We already use SIZE_MAX here, so lets use it. Please raise the PR.

OK, I’ll do it. Thanks your reply!

1 Like