tests/pkg_fatfs: fix sprintf format overflow warning#12503
tests/pkg_fatfs: fix sprintf format overflow warning#12503gschorcht merged 1 commit intoRIOT-OS:masterfrom
Conversation
Some platforms issue this:
tests/pkg_fatfs/main.c: In function '_mkfs':
tests/pkg_fatfs/main.c:355:26: error: '%d' directive writing between 1 and 11 bytes into a region of size 8 [-Werror=format-overflo
w=]
355 | sprintf(volume_str, "%d:/", vol_idx);
| ^~
tests/pkg_fatfs/main.c:355:25: note: directive argument in the range [-2147483648, 0]
355 | sprintf(volume_str, "%d:/", vol_idx);
| ^~~~~~
tests/pkg_fatfs/main.c:355:5: note: 'sprintf' output between 4 and 14 bytes into a destination of size 8
355 | sprintf(volume_str, "%d:/", vol_idx);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
The cause is vol_idx being converted by atoi(), so it might be negative.
This commit increases the stack array so it cannot overflow.
|
@kb2ma This hits in a rare corner case (user gives too large negative number as vol_idx parameter) and might cause stack corruption. Not sure this warrants the effort to backport. |
|
Thanks for the mention. I'll look into it. |
We can probably get away with a known issue entry. |
|
Tested with new and old MSP430 toolchain. |
|
Thanks! |
Could it be #8408 ? If yes, we can close it. |
I think that's unrelated. The format overflow actually happens on all platforms. |
|
So let me make sure I understand. The issue is a compiler error on a test, and we're just now seeing this because we're using a modern TI toolchain? If so, I don't see running tests with this toolchain on a released version as something worth a backport. |
|
No, the issue is a not properly checked argument (vol_idx) in a test shell command, triggering a valid sprintf "format overflow" warning. This PR mitigates the buffer overflow by increasing the buffer. The code will probably error later on, anyways. I didn't check that. I saw this warning trigger with the TI toolchain. Maybe different default warning flags? |
Contribution description
Some platforms issue this:
The cause is vol_idx being converted by atoi(), so it might be any negative int, which the later code doesn't check for.
This commit increases the stack array so it cannot overflow. It probably increases stack usage by 6-8 bytes, but this is a test shell command, so I think that's acceptable...
Testing procedure
Caught this in #12457 (with TI's toolchain). Somehow this doesn't trigger in master.
Maybe a good look at the code is sufficient?
Issues/PRs references
#12457 fails compiling the test due to this.