Warning spi.c in function 'spi_tostring'
Closed this issue · 3 comments
Hi!
There are some "directive" warning in the spi.c.
Can you fix it to keep the "clean-coding"?
c-periphery/src/spi.c: In function 'spi_tostring':
c-periphery/src/spi.c:300:65: warning: '%u' directive output may be truncated writing between 1 and 3 bytes into a region of size 2 [-Wformat-truncation=]
snprintf(bits_per_word_str, sizeof(bits_per_word_str), "%u", bits_per_word);
^~
c-periphery/src/spi.c:300:64: note: directive argument in the range [0, 255]
snprintf(bits_per_word_str, sizeof(bits_per_word_str), "%u", bits_per_word);
^~~~
c-periphery/src/spi.c:300:9: note: 'snprintf' output between 2 and 4 bytes into a destination of size 2
snprintf(bits_per_word_str, sizeof(bits_per_word_str), "%u", bits_per_word);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
That's a pretty smart compiler to know it's not a problem with mode_str
. I think the two choices for silencing this are either masking off bits_per_word
in spi_get_bits_per_word()
or increasing the size of bits_per_word_str
to 4. Increasing the size is probably the better way.
Actually I do see the mode_str
truncation warning, along with others:
src/pwm.c: In function ‘pwm_tostring’:
src/pwm.c:424:51: warning: ‘%f’ directive output may be truncated writing between 3 and 317 bytes into a region of size 16 [-Wformat-truncation=]
424 | snprintf(period_str, sizeof(period_str), "%f", period);
| ^~
src/pwm.c:424:50: note: assuming directive output of 8 bytes
424 | snprintf(period_str, sizeof(period_str), "%f", period);
| ^~~~
src/pwm.c:424:9: note: ‘snprintf’ output between 4 and 318 bytes into a destination of size 16
424 | snprintf(period_str, sizeof(period_str), "%f", period);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/pwm.c:429:59: warning: ‘%f’ directive output may be truncated writing between 3 and 317 bytes into a region of size 16 [-Wformat-truncation=]
429 | snprintf(duty_cycle_str, sizeof(duty_cycle_str), "%f", duty_cycle);
| ^~
src/pwm.c:429:58: note: assuming directive output of 8 bytes
429 | snprintf(duty_cycle_str, sizeof(duty_cycle_str), "%f", duty_cycle);
| ^~~~
src/pwm.c:429:9: note: ‘snprintf’ output between 4 and 318 bytes into a destination of size 16
429 | snprintf(duty_cycle_str, sizeof(duty_cycle_str), "%f", duty_cycle);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc -std=gnu99 -pedantic -Wall -Wextra -Wno-unused-parameter -fPIC -Wformat-truncation=2 -DPERIPHERY_VERSION_COMMIT=\"v2.2.3-dirty\" -DPERIPHERY_GPIO_CDEV_SUPPORT=1 -c src/spi.c -o obj/spi.o
src/spi.c: In function ‘spi_tostring’:
src/spi.c:285:47: warning: ‘%u’ directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]
285 | snprintf(mode_str, sizeof(mode_str), "%u", mode);
| ^~
src/spi.c:285:9: note: ‘snprintf’ output between 2 and 11 bytes into a destination of size 2
285 | snprintf(mode_str, sizeof(mode_str), "%u", mode);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/spi.c:300:65: warning: ‘%u’ directive output may be truncated writing between 1 and 3 bytes into a region of size 2 [-Wformat-truncation=]
300 | snprintf(bits_per_word_str, sizeof(bits_per_word_str), "%u", bits_per_word);
| ^~
src/spi.c:300:9: note: ‘snprintf’ output between 2 and 4 bytes into a destination of size 2
300 | snprintf(bits_per_word_str, sizeof(bits_per_word_str), "%u", bits_per_word);
| ^~~~~~~~~
It doesn't make much sense to address the mode_str
, period_str
, duty_cycle_str
warnings, since we're relying on truncation for the period and duty cycle floating point values, and we know know the mode is limited to values of 0 to 3 by the mask in spi_get_mode()
. There's no memory safety concerns, since snprintf()
will limit the total number of bytes written to the sizeof()
.
The bits_per_word
one is a legitimate bug, so I've gone ahead and fixed that and included it in the v2.2.3 release.
Thanks for fixing, now it builds well in new release.