This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new b304dfef2ab fs/procfs: Return 0 from procfs_snprintf on format error.
b304dfef2ab is described below

commit b304dfef2aba332282e834af2de806b4b629cb11
Author: Shikha Bhadoria <[email protected]>
AuthorDate: Sun Nov 29 10:49:34 2026 +0000

    fs/procfs: Return 0 from procfs_snprintf on format error.
    
    A negative vsnprintf() result was compared with size_t and promoted to
    SIZE_MAX, so the helper returned size - 1 and callers could treat
    uninitialized buffer bytes as valid. Guard size == 0 before the call,
    return 0 on n < 0, and cap a truncated write at size - 1.
    
    Assisted-by: Cursor:Grok-4.6
    Signed-off-by: Shikha Bhadoria <[email protected]>
---
 fs/procfs/fs_procfsutil.c | 23 ++++++++++++++++++++++-
 include/nuttx/fs/procfs.h |  1 +
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/fs/procfs/fs_procfsutil.c b/fs/procfs/fs_procfsutil.c
index 6bfac1db816..28f04286f6a 100644
--- a/fs/procfs/fs_procfsutil.c
+++ b/fs/procfs/fs_procfsutil.c
@@ -128,6 +128,7 @@ size_t procfs_memcpy(FAR const char *src, size_t srclen,
  *   If buf has no enough space and output was truncated due to size limit,
  *   snprintf:        return formatted string len.
  *   procfs_snprintf: return string len which has written to buf.
+ *   If size is 0 or vsnprintf() fails, return 0.
  *
  * Input Parameters:
  *   Same with snprintf
@@ -143,10 +144,30 @@ int procfs_snprintf(FAR char *buf, size_t size,
   va_list ap;
   int n;
 
+  /* size - 1 underflows when size is 0, and POSIX allows buf to be NULL. */
+
+  if (size == 0)
+    {
+      return 0;
+    }
+
   va_start(ap, format);
   n = vsnprintf(buf, size, format, ap);
   va_end(ap);
-  return size > 0 && n < size - 1 ? n : size > 0 ? size - 1 : 0;
+
+  /* A negative result is an error.  Do not promote it to size_t. */
+
+  if (n < 0)
+    {
+      return 0;
+    }
+
+  if ((size_t)n >= size)
+    {
+      n = size - 1;
+    }
+
+  return n;
 }
 
 /****************************************************************************
diff --git a/include/nuttx/fs/procfs.h b/include/nuttx/fs/procfs.h
index 1dc1b673479..6a61f76d2bf 100644
--- a/include/nuttx/fs/procfs.h
+++ b/include/nuttx/fs/procfs.h
@@ -215,6 +215,7 @@ size_t procfs_memcpy(FAR const char *src, size_t srclen,
  *   If buf has no enough space and output was truncated due to size limit,
  *   snprintf:        return formatted string len.
  *   procfs_snprintf: return string len which has written to buf.
+ *   If size is 0 or vsnprintf() fails, return 0.
  *
  * Input Parameters:
  *   Same with snprintf

Reply via email to