{bp-19444} libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes - #19622
Merged
Merged
Conversation
Move uid_t/gid_t out of the CONFIG_SMALL_MEMORY #ifdef so they are always defined as unsigned int regardless of SMALL_MEMORY. Update include/nuttx/fs/hostfs.h to match: drop the int16_t variants of nuttx_gid_t/nuttx_uid_t and keep a single unsigned int definition so the hostfs RPC ABI stays in sync with sys/types.h. Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
A 16-bit ino_t can only address 65536 distinct file serial numbers, which is not enough for filesystems with large directory trees and breaks portable software (e.g. dropbear's scp) that expects a wider inode number space. Widen ino_t (and nuttx_ino_t in the hostfs ABI) to uint32_t to match common POSIX practice. Update fs/rpmsgfs/rpmsgfs.h accordingly: promote the 'ino' field in struct rpmsgfs_stat_priv_s from uint16_t to uint32_t and move 'nlink' into the trailing 16-bit slot previously occupied by the reserved field, keeping the overall packed-struct layout/size unchanged. Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Add the POSIX d_ino (file serial number) member to struct dirent and
populate it on every readdir() path, so portable callers (e.g. scp in
dropbear) that read dp->d_ino observe a meaningful, non-zero inode
number:
- include/dirent.h: declare d_ino in struct dirent and drop the
outdated comment claiming the field is unimplemented.
- include/nuttx/fs/hostfs.h: add d_ino to struct nuttx_dirent_s so
the hostfs ABI can carry the inode number across the VFS boundary.
- arch/sim/src/sim/posix/sim_hostfs.c: forward the host's
ent->d_ino into entry->d_ino.
- fs/vfs/fs_dir.c (read_pseudodir): copy the in-memory inode's
i_ino into entry->d_ino for the pseudo filesystem.
- fs/yaffs/yaffs_vfs.c: forward yaffs's dirent->d_ino into
entry->d_ino.
- fs/rpmsgfs: extend struct rpmsgfs_readdir_s with an 'ino' field
and propagate it across the RPC in both rpmsgfs_server (fills it
from the underlying entry) and rpmsgfs_client (writes it back to
the caller's nuttx_dirent_s).
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
…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>
jerpelea
requested review from
acassis,
cederom,
linguini1,
lupyuen and
xiaoxiang781216
August 3, 2026 08:28
jerpelea
requested review from
Donny9,
GUIDINGLI,
Ouss4,
davids5,
gustavonihei,
johannes-nivus,
masayuki2009,
pkarashchenko and
yamt
as code owners
August 3, 2026 08:28
xiaoxiang781216
approved these changes
Aug 3, 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 of st_blksize when a filesystem reports a block size larger than can be represented by blksize_t. As reported in #19432, this can occur with LittleFS configurations using large block sizes, where blksize_t is currently defined as int16_t. When the filesystem block size exceeds the range of int16_t, st_blksize is truncated to zero, causing an integer divide-by-zero when st_blocks is calculated.
This patch widens blksize_t from int16_t to int32_t in include/sys/types.h and updates the corresponding nuttx_blksize_t definition in include/nuttx/fs/hostfs.h so the two definitions remain consistent.
Fixes #19432
Includes:
#19179
Impact
RELEASE
Testing
CI