libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes#19444
Merged
xiaoxiang781216 merged 1 commit intoJul 16, 2026
Merged
Conversation
ML-dev-crypto
requested review from
jerpelea,
xiaoxiang781216 and
yamt
as code owners
July 15, 2026 16:30
linguini1
requested changes
Jul 15, 2026
linguini1
left a comment
Contributor
There was a problem hiding this comment.
Please follow the contribution guide and PR template :)
Also, please include some logs from your tests of this change.
Contributor
Author
|
Thanks! I'll update the PR to follow the template and rerun the tests locally so I can include the requested build and test logs. |
xiaoxiang781216
previously approved these changes
Jul 15, 2026
|
linguini1
previously approved these changes
Jul 15, 2026
Contributor
|
It looks like you will need to fix the log statements that expect the block size to be 16-bit |
ML-dev-crypto
dismissed stale reviews from linguini1 and xiaoxiang781216
via
July 16, 2026 04:13
a012672
ML-dev-crypto
force-pushed
the
fix-19432-blksize-overflow
branch
from
July 16, 2026 04:13
7c0249f to
a012672
Compare
ML-dev-crypto
requested review from
Donny9,
Ouss4,
davids5,
gustavonihei,
johannes-nivus,
masayuki2009 and
pkarashchenko
as code owners
July 16, 2026 04:13
xiaoxiang781216
approved these changes
Jul 16, 2026
jerpelea
approved these changes
Jul 16, 2026
…izes blksize_t is currently defined as int16_t, which overflows when a filesystem reports a block size larger than 32767 bytes. This causes st_blksize to become zero, leading to an integer divide-by-zero when st_blocks is calculated in stat(). Widen blksize_t to int32_t to support larger filesystem block sizes. Update nuttx_blksize_t in include/nuttx/fs/hostfs.h to keep it consistent with include/sys/types.h. struct geometry.geo_sectorsize (include/nuttx/fs/ioctl.h) is also typed blksize_t, so every debug print of that field using a 16-bit format specifier is updated to PRId32 to match the new width: drivers/misc/ramdisk.c, drivers/mmcsd/mmcsd_spi.c, drivers/mtd/ftl.c, fs/driver/fs_blockmerge.c, drivers/mtd/smart.c, drivers/usbhost/usbhost_storage.c, drivers/mmcsd/mmcsd_sdio.c, arch/arm/src/s32k1xx/s32k1xx_eeeprom.c, arch/arm/src/lc823450/lc823450_mmcl.c. Signed-off-by: Ansh Rai <anshrai331@gmail.com> Signed-off-by: root <root@LAPTOP-9C7LKDC5.localdomain>
ML-dev-crypto
force-pushed
the
fix-19432-blksize-overflow
branch
from
July 16, 2026 09:15
a012672 to
092464c
Compare
xiaoxiang781216
approved these changes
Jul 16, 2026
acassis
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This change fixes a divide-by-zero in
stat()caused by overflow ofst_blksizewhen a filesystem reports a block size larger than can be represented byblksize_t. As reported in #19432, this can occur with LittleFS configurations using large block sizes, whereblksize_tis currently defined asint16_t. When the filesystem block size exceeds the range ofint16_t,st_blksizeis truncated to zero, causing an integer divide-by-zero whenst_blocksis calculated.This patch widens
blksize_tfromint16_ttoint32_tininclude/sys/types.hand updates the correspondingnuttx_blksize_tdefinition ininclude/nuttx/fs/hostfs.hso the two definitions remain consistent.Fixes #19432
Impact
This change allows
st_blksizeto correctly represent larger filesystem block sizes and prevents the divide-by-zero reported in #19432. It does not introduce any new configuration options or dependencies, and only updates theblksize_ttypedef together with the matchingnuttx_blksize_tdefinition used by hostfs.Testing
Host: Ubuntu 24.04 (Docker), x86_64
The simulator was configured with
CONFIG_RAMMTD_ERASESIZE=4096andCONFIG_FS_LITTLEFS_BLOCK_SIZE_FACTOR=16, resulting in a LittleFS block size of 65536 bytes. The project built successfully using:make -j$(nproc)which produced:
After booting the simulator, LittleFS was mounted successfully. A file was created and listed successfully using
ls -l, which exercises thestat()path involved in the reported issue.With this configuration, no divide-by-zero or simulator crash was observed while listing files on the LittleFS mount.