MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()

Report here problems in any of ChibiOS components. This forum is NOT for support.
koppenwonga
Posts: 2
Joined: Thu Sep 18, 2025 2:28 pm
Been thanked: 1 time

MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()

Postby koppenwonga » Fri Sep 19, 2025 6:46 pm

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).

koppenwonga
Posts: 2
Joined: Thu Sep 18, 2025 2:28 pm
Been thanked: 1 time

Re: MemoryStream Initialization Bug in chvsnscanf()/msObjectInit()

Postby koppenwonga » Wed Oct 01, 2025 9:54 am

The same bug applies for chvsnscanf() in
os/common/oop/src/oop_chscanf.c

Best regards


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 33 guests