Skip to content

libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes#19444

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
ML-dev-crypto:fix-19432-blksize-overflow
Jul 16, 2026
Merged

libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes#19444
xiaoxiang781216 merged 1 commit into
apache:masterfrom
ML-dev-crypto:fix-19432-blksize-overflow

Conversation

@ML-dev-crypto

@ML-dev-crypto ML-dev-crypto commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

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


Impact

This change allows st_blksize to 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 the blksize_t typedef together with the matching nuttx_blksize_t definition used by hostfs.


Testing

Host: Ubuntu 24.04 (Docker), x86_64

The simulator was configured with CONFIG_RAMMTD_ERASESIZE=4096 and CONFIG_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:

LD:  nuttx
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz

After booting the simulator, LittleFS was mounted successfully. A file was created and listed successfully using ls -l, which exercises the stat() path involved in the reported issue.

nsh> mount
  /mnt/lfs type littlefs

nsh> echo "test" > /mnt/lfs/testfile

nsh> ls -l /mnt/lfs
/mnt/lfs:
 drwxrwxrwx           0 .
 drwxrwxrwx           0 ..
 -rw-rw-rw-           5 2026-07-15 17:24 testfile

nsh> ls -l /mnt/lfs/testfile
-rw-rw-rw-           5 2026-07-15 17:24 /mnt/lfs/testfile

With this configuration, no divide-by-zero or simulator crash was observed while listing files on the LittleFS mount.

@linguini1 linguini1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the contribution guide and PR template :)
Also, please include some logs from your tests of this change.

@ML-dev-crypto

Copy link
Copy Markdown
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.

@github-actions github-actions Bot added Area: File System File System issues Size: XS The size of the change in this PR is very small labels Jul 15, 2026
@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

esp32-devkitc

  • ROM: .flash.text -8 B (-0.0%, 122,708 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -8 B (-0.0%, 87,076 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text -12 B (-0.0%, 83,240 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.arm64_fpu_procfs_stat -4 B, .text.file_vioctl -4 B, .text.inode_stat -4 B (-0.0%, 315,362 B)

qemu-intel64

  • Code: .text -11 B (-0.0%, 8,655,434 B)

stm32-nucleo-f103rb

linguini1
linguini1 previously approved these changes Jul 15, 2026
@linguini1

Copy link
Copy Markdown
Contributor

It looks like you will need to fix the log statements that expect the block size to be 16-bit

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Area: Drivers Drivers issues Size: S The size of the change in this PR is small and removed Area: File System File System issues Size: XS The size of the change in this PR is very small labels 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
ML-dev-crypto force-pushed the fix-19432-blksize-overflow branch from a012672 to 092464c Compare July 16, 2026 09:15
@xiaoxiang781216
xiaoxiang781216 merged commit 54b0066 into apache:master Jul 16, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Divided by zero in Littlefs and MTD NAND with int16_t of blksize in stat.

5 participants