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

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


The following commit(s) were added to refs/heads/master by this push:
     new c50ff7ff6 Fixes in asprintf usage.
c50ff7ff6 is described below

commit c50ff7ff61fc454ca847e3ac848e42febab71477
Author: Fotis Panagiotopoulos <[email protected]>
AuthorDate: Wed Apr 12 18:42:28 2023 +0300

    Fixes in asprintf usage.
---
 graphics/twm4nx/src/cresize.cxx    |  6 ++++--
 graphics/twm4nx/src/ctwm4nx.cxx    |  5 ++++-
 netutils/ftpc/ftpc_transfer.c      | 15 ++++++++++++---
 netutils/ftpd/ftpd.c               |  9 +++++----
 system/critmon/critmon.c           | 21 +++++++--------------
 system/stackmonitor/stackmonitor.c | 15 +++++----------
 system/zmodem/zm_receive.c         | 10 +++++-----
 7 files changed, 42 insertions(+), 39 deletions(-)

diff --git a/graphics/twm4nx/src/cresize.cxx b/graphics/twm4nx/src/cresize.cxx
index 93d18be72..9558f5d6c 100644
--- a/graphics/twm4nx/src/cresize.cxx
+++ b/graphics/twm4nx/src/cresize.cxx
@@ -413,6 +413,8 @@ bool CResize::setWindowSize(FAR struct nxgl_size_s *size)
 
 void CResize::updateSizeLabel(FAR struct nxgl_size_s &windowSize)
 {
+  int ret;
+
   // Do nothing if the size has not changed
 
   struct nxgl_size_s labelSize;
@@ -424,8 +426,8 @@ void CResize::updateSizeLabel(FAR struct nxgl_size_s 
&windowSize)
     }
 
   FAR char *str;
-  asprintf(&str, " %4d x %-4d ", windowSize.w, windowSize.h);
-  if (str == (FAR char *)0)
+  ret = asprintf(&str, " %4d x %-4d ", windowSize.w, windowSize.h);
+  if (ret < 0)
     {
       twmerr("ERROR: Failed to get size string\n");
       return;
diff --git a/graphics/twm4nx/src/ctwm4nx.cxx b/graphics/twm4nx/src/ctwm4nx.cxx
index e88c773d1..8b9e9ca32 100644
--- a/graphics/twm4nx/src/ctwm4nx.cxx
+++ b/graphics/twm4nx/src/ctwm4nx.cxx
@@ -382,7 +382,10 @@ void CTwm4Nx::genMqName(void)
   unsigned long randvalue =
     (unsigned long)std::random() & 0x00fffffful;
 
-  std::asprintf(&m_queueName, "Twm4Nx%06lu", randvalue);
+  if (std::asprintf(&m_queueName, "Twm4Nx%06lu", randvalue) < 0)
+    {
+      m_queueName = NULL;
+    }
 }
 
 /**
diff --git a/netutils/ftpc/ftpc_transfer.c b/netutils/ftpc/ftpc_transfer.c
index 3517e30e2..168647918 100644
--- a/netutils/ftpc/ftpc_transfer.c
+++ b/netutils/ftpc/ftpc_transfer.c
@@ -247,7 +247,10 @@ static FAR char *ftpc_abspath(FAR struct ftpc_session_s 
*session,
 
       else if (relpath[1] == '/')
         {
-          asprintf(&ptr, "%s%s", homedir, &relpath[1]);
+          if (asprintf(&ptr, "%s%s", homedir, &relpath[1]) < 0)
+            {
+              ptr = NULL;
+            }
         }
 
       /* Hmmm... this pretty much guaranteed to fail */
@@ -264,7 +267,10 @@ static FAR char *ftpc_abspath(FAR struct ftpc_session_s 
*session,
 
   else if (strncmp(relpath, "./", 2) == 0)
     {
-      asprintf(&ptr, "%s%s", curdir, relpath + 1);
+      if (asprintf(&ptr, "%s%s", curdir, relpath + 1) < 0)
+        {
+          ptr = NULL;
+        }
     }
 
   /* Check for an absolute path */
@@ -278,7 +284,10 @@ static FAR char *ftpc_abspath(FAR struct ftpc_session_s 
*session,
 
   else
     {
-      asprintf(&ptr, "%s/%s", curdir, relpath);
+      if (asprintf(&ptr, "%s/%s", curdir, relpath) < 0)
+        {
+          ptr = NULL;
+        }
     }
 
   return ptr;
diff --git a/netutils/ftpd/ftpd.c b/netutils/ftpd/ftpd.c
index d58136586..80255baa4 100644
--- a/netutils/ftpd/ftpd.c
+++ b/netutils/ftpd/ftpd.c
@@ -1007,12 +1007,13 @@ static ssize_t ftpd_response(int sd, int timeout, FAR 
const char *fmt, ...)
   FAR char *buffer;
   ssize_t bytessent;
   va_list ap;
+  int ret;
 
   va_start(ap, fmt);
-  vasprintf(&buffer, fmt, ap);
+  ret = vasprintf(&buffer, fmt, ap);
   va_end(ap);
 
-  if (buffer == NULL)
+  if (ret < 0)
     {
       return -ENOMEM;
     }
@@ -2417,8 +2418,8 @@ static int fptd_listscan(FAR struct ftpd_session_s 
*session, FAR char *path,
             }
         }
 
-      asprintf(&temp, "%s/%s", path, entry->d_name);
-      if (temp == NULL)
+      ret = asprintf(&temp, "%s/%s", path, entry->d_name);
+      if (ret < 0)
         {
           continue;
         }
diff --git a/system/critmon/critmon.c b/system/critmon/critmon.c
index 3b3089d15..46331efa1 100644
--- a/system/critmon/critmon.c
+++ b/system/critmon/critmon.c
@@ -121,7 +121,6 @@ static int critmon_process_directory(FAR struct dirent 
*entryp)
   FAR char *maxrun;
   FAR char *endptr;
   FILE *stream;
-  int errcode;
   int len;
   int ret;
 
@@ -134,13 +133,11 @@ static int critmon_process_directory(FAR struct dirent 
*entryp)
   ret = asprintf(&filepath,
                  CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/%s/status",
                  entryp->d_name);
-  if (ret < 0 || filepath == NULL)
+  if (ret < 0)
     {
-      errcode = errno;
       fprintf(stderr,
-              "Csection Monitor: Failed to create path to status file: %d\n",
-              errcode);
-      return -errcode;
+              "Csection Monitor: Failed to create path to status file\n");
+      return -ENOMEM;
     }
 
   /* Open the status file */
@@ -187,12 +184,10 @@ static int critmon_process_directory(FAR struct dirent 
*entryp)
   ret = asprintf(&filepath,
                  CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/%s/critmon",
                  entryp->d_name);
-  if (ret < 0 || filepath == NULL)
+  if (ret < 0)
     {
-      errcode = errno;
       fprintf(stderr, "Csection Monitor: "
-              "Failed to create path to Csection file: %d\n",
-              errcode);
+              "Failed to create path to Csection file\n");
       ret = -EINVAL;
       goto errout_with_name;
     }
@@ -324,12 +319,10 @@ static void critmon_global_crit(void)
 
   ret = asprintf(&filepath,
                  CONFIG_SYSTEM_CRITMONITOR_MOUNTPOINT "/critmon");
-  if (ret < 0 || filepath == NULL)
+  if (ret < 0)
     {
-      errcode = errno;
       fprintf(stderr, "Csection Monitor: "
-              "Failed to create path to Csection file: %d\n",
-              errcode);
+              "Failed to create path to Csection file\n");
       return;
     }
 
diff --git a/system/stackmonitor/stackmonitor.c 
b/system/stackmonitor/stackmonitor.c
index 0ba1dc046..2aca2c17b 100644
--- a/system/stackmonitor/stackmonitor.c
+++ b/system/stackmonitor/stackmonitor.c
@@ -121,7 +121,6 @@ static int stkmon_process_directory(FAR struct dirent 
*entryp)
   FILE *stream;
   unsigned long stack_size;
   unsigned long stack_used;
-  int errcode;
   int len;
   int ret;
 
@@ -134,13 +133,11 @@ static int stkmon_process_directory(FAR struct dirent 
*entryp)
   ret = asprintf(&filepath,
                  CONFIG_SYSTEM_STACKMONITOR_MOUNTPOINT "/%s/status",
                  entryp->d_name);
-  if (ret < 0 || filepath == NULL)
+  if (ret < 0)
     {
-      errcode = errno;
       fprintf(stderr,
-              "Stack Monitor: Failed to create path to status file: %d\n",
-              errcode);
-      return -errcode;
+              "Stack Monitor: Failed to create path to status file\n");
+      return -ENOMEM;
     }
 
   /* Open the status file */
@@ -189,12 +186,10 @@ static int stkmon_process_directory(FAR struct dirent 
*entryp)
   ret = asprintf(&filepath,
                  CONFIG_SYSTEM_STACKMONITOR_MOUNTPOINT "/%s/stack",
                  entryp->d_name);
-  if (ret < 0 || filepath == NULL)
+  if (ret < 0)
     {
-      errcode = errno;
       fprintf(stderr,
-              "Stack Monitor: Failed to create path to stack file: %d\n",
-              errcode);
+              "Stack Monitor: Failed to create path to stack file\n");
       ret = -EINVAL;
       goto errout_with_name;
     }
diff --git a/system/zmodem/zm_receive.c b/system/zmodem/zm_receive.c
index 21a7c5075..b93b95085 100644
--- a/system/zmodem/zm_receive.c
+++ b/system/zmodem/zm_receive.c
@@ -1143,8 +1143,8 @@ static int zmr_parsefilename(FAR struct zmr_state_s *pzmr,
 
   /* Extend the relative path to the file storage directory */
 
-  asprintf(&pzmr->filename, "%s/%s", pzmr->pathname, namptr);
-  if (!pzmr->filename)
+  ret = asprintf(&pzmr->filename, "%s/%s", pzmr->pathname, namptr);
+  if (ret < 0)
     {
       zmdbg("ERROR: Failed to allocate full path %s/%s\n",
             CONFIG_SYSTEM_ZMODEM_MOUNTPOINT, namptr);
@@ -1331,9 +1331,9 @@ static int zmr_parsefilename(FAR struct zmr_state_s *pzmr,
               {
                 /* Create a candidate file name */
 
-                asprintf(&candidate, "%s_%" PRId32, pzmr->filename,
-                         ++uniqno);
-                if (!candidate)
+                ret = asprintf(&candidate, "%s_%" PRId32, pzmr->filename,
+                               ++uniqno);
+                if (ret < 0)
                   {
                     zmdbg("ERROR:  Failed to allocate candidate %s_%d\n",
                           pzmr->filename, uniqno);

Reply via email to