Search code examples
gccarmgcc-warning

GCC - Truncation due to type conversion warning


Why is the compiler not throwing a value truncation due to the conversion from a uint32 to a uint8 variable type? Precisely the line result = reg_value;

void read_byte_register(uint32_t u32_address_rd_byte_reg,
        uint8_t *pu8_Data_rd_byte_reg)
{
    uint8_t result;
    uint32_t reg_value;
    uint32_t *reg_ptr;

    reg_ptr = (uint32_t *) u32_address_rd_byte_reg;

    reg_value = *(reg_ptr);

    result = reg_value;

    *(pu8_Data_rd_byte_reg) = result;
}

The build command. You can see it is building for an ARM Cortex M3:

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -pedantic -Wall -Wextra  -g3 -T "linker_script.ld" -Xlinker --gc-sections -Wl,-Map,"output_map.map" --specs=nano.specs -o "output.elf"

Solution

  • Adding the flag -Wconversion as stated in a comment to the question fixed the problem. Apparently it is disabled by default. -Wconversion enables warnings about implicit type conversions. Attaching a screenshot about the warning:

    enter image description here

    arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -pedantic -Wall -Wextra -Wconversion -g3 -T "linker_script.ld" -Xlinker --gc-sections -Wl,-Map,"output_map.map" --specs=nano.specs -o "output.elf"