MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()
When using chvsnscanf() in ChibiOS, you may encounter a situation where the underlying chvscanf() function always fails to read any input due to an incorrectly initialized MemoryStream object.
Problem Description:
The function chvsnscanf() creates a local MemoryStream and initializes it with msObjectInit(&ms, (uint8_t*)str, size_wo_nul, 0). The final eos (end of stream) argument is set to 0.
Inside the stream implementation, the reading logic checks if (msp->eos - msp->offset <= 0), and returns MSG_RESET (EOF) if true. Since eos is 0, the stream is always considered exhausted, and no data is read.
Root Cause:
Passing 0 for the eos parameter incorrectly marks the stream as exhausted from the start.
Solution:
Replace the initialization with the actual buffer size as the end-of-stream marker:
c
msObjectInit(&ms, (uint8_t*)str, size_wo_nul, size_wo_nul);
This ensures the stream will read up to the end of the provided buffer, as intended.
Summary:
If you encounter chvscanf() immediately failing with no data read, verify that your msObjectInit call sets eos to the buffer length, not zero.
Compare:
https://github.com/ChibiOS/ChibiOS/blob ... anf.c#L788
https://github.com/ChibiOS/ChibiOS/blob ... ams.c#L122
Chibios Version:
Master, stable_21.11.x and propably older Versions
Compiler:
gcc version 14.2.1 20241119
Board:
custom (STM32L432)
Nature of the problem:
Maybe copy-paste from chprintf.c
Failure mode:
chvscanf() immediately aborts and reads no data because the stream is marked as empty (eos = 0).
MemoryStream Initialization Bug in chvsnscanf()/msObjectInit() Topic is solved
-
koppenwonga
- Posts: 3
- Joined: Thu Sep 18, 2025 2:28 pm
- Been thanked: 2 times
-
koppenwonga
- Posts: 3
- Joined: Thu Sep 18, 2025 2:28 pm
- Been thanked: 2 times
Re: MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()
The same bug applies for chvsnscanf() in
os/common/oop/src/oop_chscanf.c
Best regards
os/common/oop/src/oop_chscanf.c
Best regards
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()
Hi,
I committed a tentative fix, please try it.
Giovanni
I committed a tentative fix, please try it.
Giovanni
-
koppenwonga
- Posts: 3
- Joined: Thu Sep 18, 2025 2:28 pm
- Been thanked: 2 times
Re: MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()
Hi Giovanni,
I have tested your fix, it works fine. Thank you very much! I am working with ChibiOS for a long time now and wanted to let you know that you are doing a great job!
Best regards
I have tested your fix, it works fine. Thank you very much! I am working with ChibiOS for a long time now and wanted to let you know that you are doing a great job!
Best regards