shikhabhadoria opened a new pull request, #22553:
URL: https://github.com/apache/nuttx/pull/22553

   fs/procfs: Return 0 from procfs_snprintf on format error.
   
   ## Summary
   
     * `procfs_snprintf()` is supposed to return the number of bytes actually 
written into `buf`, not the unbounded `snprintf` length.
     * On current master, `size == 0` already returns 0, but a negative 
`vsnprintf()` result is still compared with `size_t`. That promotes `-1` to 
`SIZE_MAX`, the comparison fails, and the helper returns `size - 1`. Callers 
then treat uninitialized buffer bytes as valid output.
     * Guard `size == 0` before calling `vsnprintf()` (POSIX allows `buf` to be 
NULL), return 0 when `n < 0`, and cap a truncated write at `size - 1` with an 
unsigned comparison.
     * Related issue: https://github.com/apache/nuttx/issues/22550
   
   ## Impact
   
     * Is new feature added? Is existing feature changed? NO. Bugfix of the 
return value on format error and empty buffer.
     * Impact on user (will user need to adapt to change)? NO.
     * Impact on build (will build process change)? NO.
     * Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO. 
Architecture-independent C in `fs/procfs`.
     * Impact on documentation (is update required / provided)? YES. Function 
description in `fs_procfsutil.c` and `include/nuttx/fs/procfs.h` now states 
that size 0 and a failed `vsnprintf()` return 0.
     * Impact on security (any sort of implications)? YES. The old path could 
report `size - 1` successful bytes after a failed format, so procfs readers 
could consume uninitialized stack/buffer contents.
     * Impact on compatibility (backward/forward/interoperability)? NO. 
Successful writes still return the written length, truncated at `size - 1`.
     * Anything else to consider or add? The issue sample still used `return n 
< size - 1 ? n : size - 1;`. Master already had a `size > 0` guard; the 
remaining defect is the signed/unsigned `n < size - 1` comparison.
   
   ## Testing
   
     I confirm that changes are verified on local setup and works as intended:
     * Build Host(s): Ubuntu 24.04, x86_64, gcc 13.3.0
     * Target(s): host unit test of the return mapping and a host clone of 
`procfs_snprintf()` (arch-independent helper; no board-specific behavior)
     * Testing logs:
   
   checkpatch:
   
   ```
   $ ./tools/checkpatch.sh -f fs/procfs/fs_procfsutil.c 
include/nuttx/fs/procfs.h
   All checks pass.
   ```
   
   Host mapping test (old master expression vs new logic). gcc `-Wsign-compare` 
flags the old `n < size - 1` comparison. The old mapping returns 127 for `n = 
-1`, `size = 128`; the new mapping returns 0:
   
   ```
   PASS old n=-1 size=128: 127
   PASS new n=-1 size=128: 0
   PASS old n=-1 size=0: 0
   PASS new n=-1 size=0: 0
   PASS new n=-1 size=1: 0
   PASS new n=0 size=10: 0
   PASS new n=5 size=10: 5
   PASS new n=9 size=10: 9
   PASS new n=10 size=10: 9
   PASS new n=20 size=10: 9
   PASS new size=0 n=5: 0
   PASS size=0 null buf: 0
   PASS size=0 non-null buf: 0
   PASS fits: 5
   PASS fits payload
   PASS truncated return: 3
   PASS truncated payload
   ALL TESTS PASSED
   ```
   
   The new mapping also compiles clean with `gcc -Wall -Wextra -Werror`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to