You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2023/05/08 18:35:24 UTC

[nuttx-apps] 01/05: Replace all strcat with strlcat

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

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

commit 134b8b538fcfb169f5a773de908be51f29a4a137
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Mar 5 21:46:37 2023 +0800

    Replace all strcat with strlcat
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 interpreters/minibasic/basic.c     |  2 +-
 netutils/rexec/rexec.c             |  4 ++--
 netutils/thttpd/cgi-src/redirect.c |  3 ++-
 netutils/thttpd/libhttpd.c         | 22 +++++++++++-----------
 netutils/xmlrpc/response.c         | 32 ++++++++++++--------------------
 nshlib/nsh_parse.c                 |  2 +-
 system/lzf/lzf_main.c              |  9 ++++-----
 system/taskset/taskset.c           |  7 +++----
 system/termcurses/tcurses_vt100.c  | 10 +++++-----
 system/trace/trace.c               |  6 +++---
 testing/fatutf8/fatutf8_main.c     |  2 +-
 testing/fstest/fstest_main.c       |  2 +-
 12 files changed, 46 insertions(+), 55 deletions(-)

diff --git a/interpreters/minibasic/basic.c b/interpreters/minibasic/basic.c
index 7edaa25bb..304b73b7f 100644
--- a/interpreters/minibasic/basic.c
+++ b/interpreters/minibasic/basic.c
@@ -4036,7 +4036,7 @@ static FAR char *mystrconcat(FAR const char *str, FAR const char *cat)
   if (answer)
     {
       strlcpy(answer, str, len);
-      strcat(answer, cat);
+      strlcat(answer, cat, len);
     }
 
   return answer;
diff --git a/netutils/rexec/rexec.c b/netutils/rexec/rexec.c
index 2f2358147..f5e031c53 100644
--- a/netutils/rexec/rexec.c
+++ b/netutils/rexec/rexec.c
@@ -165,8 +165,8 @@ int main(int argc, FAR char **argv)
   cmd[0] = '\0';
   for (i = optind; i < argc; i++)
     {
-      strcat(cmd, argv[i]);
-      strcat(cmd, " ");
+      strlcat(cmd, argv[i], sizeof(cmd));
+      strlcat(cmd, " ", sizeof(cmd));
     }
 
   arg.command = cmd;
diff --git a/netutils/thttpd/cgi-src/redirect.c b/netutils/thttpd/cgi-src/redirect.c
index 943ac17c4..afe21f0df 100644
--- a/netutils/thttpd/cgi-src/redirect.c
+++ b/netutils/thttpd/cgi-src/redirect.c
@@ -236,7 +236,8 @@ int main(int argc, char *argv[])
                     {
                       /* Got it; put together the full name. */
 
-                      strcat(g_url, script_name + (star - g_file));
+                      strlcat(g_url, script_name + (star - g_file),
+                              sizeof(g_url));
 
                       /* XXX Whack the script_name, too? */
 
diff --git a/netutils/thttpd/libhttpd.c b/netutils/thttpd/libhttpd.c
index f4ea6f7c3..070ba8f6b 100644
--- a/netutils/thttpd/libhttpd.c
+++ b/netutils/thttpd/libhttpd.c
@@ -924,10 +924,10 @@ static int httpd_tilde_map1(httpd_conn *hc)
 
   if (prefix[0] != '\0')
     {
-      strcat(hc->expnfilename, "/");
+      strlcat(hc->expnfilename, "/", hc->maxexpnfilename + 1);
     }
 
-  strcat(hc->expnfilename, temp);
+  strlcat(hc->expnfilename, temp, hc->maxexpnfilename + 1);
   return 1;
 }
 #endif /* CONFIG_THTTPD_TILDE_MAP1 */
@@ -975,8 +975,8 @@ static int httpd_tilde_map2(httpd_conn *hc)
   strlcpy(hc->altdir, pw->pw_dir, hc->maxaltdir + 1);
   if (postfix[0] != '\0')
     {
-      strcat(hc->altdir, "/");
-      strcat(hc->altdir, postfix);
+      strlcat(hc->altdir, "/", hc->maxaltdir + 1);
+      strlcat(hc->altdir, postfix, hc->maxaltdir + 1);
     }
 
   alt = expand_filename(hc->altdir, &rest, true);
@@ -1116,8 +1116,8 @@ static int vhost_map(httpd_conn *hc)
   httpd_realloc_str(&hc->expnfilename, &hc->maxexpnfilename,
                     strlen(hc->hostdir) + 1 + len);
   strlcpy(hc->expnfilename, hc->hostdir, hc->maxexpnfilename + 1);
-  strcat(hc->expnfilename, "/");
-  strcat(hc->expnfilename, tempfilename);
+  strlcat(hc->expnfilename, "/", hc->maxexpnfilename + 1);
+  strlcat(hc->expnfilename, tempfilename, hc->maxexpnfilename + 1);
   return 1;
 }
 #endif
@@ -2875,14 +2875,14 @@ int httpd_parse_request(httpd_conn *hc)
 
                   httpd_realloc_str(&hc->accept, &hc->maxaccept,
                                     strlen(hc->accept) + 2 + strlen(cp));
-                  strcat(hc->accept, ", ");
+                  strlcat(hc->accept, ", ", hc->maxaccepte + 1);
                 }
               else
                 {
                   httpd_realloc_str(&hc->accept, &hc->maxaccept, strlen(cp));
                 }
 
-              strcat(hc->accept, cp);
+              strlcat(hc->accept, cp, hc->maxaccepte + 1);
             }
           else if (strncasecmp(buf, "Accept-Encoding:", 16) == 0)
             {
@@ -2899,7 +2899,7 @@ int httpd_parse_request(httpd_conn *hc)
 
                   httpd_realloc_str(&hc->accepte, &hc->maxaccepte,
                                     strlen(hc->accepte) + 2 + strlen(cp));
-                  strcat(hc->accepte, ", ");
+                  strlcat(hc->accepte, ", ", hc->maxaccepte + 1);
                 }
               else
                 {
@@ -3296,7 +3296,7 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowp)
           indxlen = strlen(indexname);
           if (indxlen == 0 || indexname[indxlen - 1] != '/')
             {
-              strcat(indexname, "/");
+              strlcat(indexname, "/", maxindexname + 1);
             }
 
           if (strcmp(indexname, "./") == 0)
@@ -3304,7 +3304,7 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowp)
               indexname[0] = '\0';
             }
 
-          strcat(indexname, index_names[i]);
+          strlcat(indexname, index_names[i], maxindexname + 1);
           if (stat(indexname, &hc->sb) >= 0)
             {
               goto got_one;
diff --git a/netutils/xmlrpc/response.c b/netutils/xmlrpc/response.c
index a3f77b9ba..bfc99daa2 100644
--- a/netutils/xmlrpc/response.c
+++ b/netutils/xmlrpc/response.c
@@ -164,7 +164,6 @@ int xmlrpc_getstring(struct xmlrpc_s *xmlcall, char *arg)
 int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
 {
   va_list argp;
-  int ret = 0;
   int index = 0;
   int close = 0;
   int isstruct = 0;
@@ -187,12 +186,13 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
 
   if (xmlcall->error)
     {
-      strcat(&xmlcall->response[strlen(xmlcall->response)], "  <fault>\n");
+      strlcat(xmlcall->response, "  <fault>\n",
+              sizeof(xmlcall->response));
     }
   else
     {
-      strcat(&xmlcall->response[strlen(xmlcall->response)],
-             "  <params><param>\n");
+      strlcat(xmlcall->response, "  <params><param>\n",
+              sizeof(xmlcall->response));
     }
 
   va_start(argp, args);
@@ -268,26 +268,18 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
 
   if (xmlcall->error)
     {
-      strcat(&xmlcall->response[strlen(xmlcall->response)],
-             "  </fault>\r\n");
+      strlcat(xmlcall->response, "  </fault>\r\n",
+              sizeof(xmlcall->response));
     }
   else
     {
-      strcat(&xmlcall->response[strlen(xmlcall->response)],
-             "  </param></params>\r\n");
+      strlcat(xmlcall->response, "  </param></params>\r\n",
+              sizeof(xmlcall->response));
     }
 
-  if (ret == 0)
-    {
-      strcat(&xmlcall->response[strlen(xmlcall->response)],
-             "</methodResponse>\r\n");
-
-      xmlrpc_insertlength(xmlcall);
-    }
-  else
-    {
-      xmlcall->response[0] = 0;
-    }
+  strlcat(xmlcall->response, "</methodResponse>\r\n",
+          sizeof(xmlcall->response));
 
-  return ret;
+  xmlrpc_insertlength(xmlcall);
+  return 0;
 }
diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c
index cf8310bd7..4f1a3b015 100644
--- a/nshlib/nsh_parse.c
+++ b/nshlib/nsh_parse.c
@@ -1106,7 +1106,7 @@ static FAR char *nsh_strcat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
   else
     {
       argument[s1size] = '\0';  /* (In case s1 was NULL) */
-      strcat(argument, s2);
+      strlcat(argument, s2, allocsize);
     }
 
   return argument;
diff --git a/system/lzf/lzf_main.c b/system/lzf/lzf_main.c
index 60cb80ed0..f76935f9c 100644
--- a/system/lzf/lzf_main.c
+++ b/system/lzf/lzf_main.c
@@ -333,14 +333,14 @@ static int compose_name(FAR const char *fname, FAR char *oname, int namelen)
           return -1;
         }
 
-      strncpy(oname, fname, namelen);
+      strlcpy(oname, fname, namelen);
       p = strchr(oname, '.');
       if (p != NULL)
         {
           *p = '_';  /* _ for dot */
         }
 
-       strcat (oname, ".lzf");
+       strlcat(oname, ".lzf", namelen);
     }
   else
     {
@@ -372,13 +372,12 @@ static int compose_name(FAR const char *fname, FAR char *oname, int namelen)
 static int run_file(FAR const char *fname)
 {
   struct stat mystat;
-  char oname[PATH_MAX + 1];
+  char oname[PATH_MAX];
   int fd;
   int fd2;
   int ret;
 
-  memset(oname, 0, sizeof(oname));
-  if (compose_name(fname, oname, PATH_MAX + 1))
+  if (compose_name(fname, oname, sizeof(oname)))
     {
       return -1;
     }
diff --git a/system/taskset/taskset.c b/system/taskset/taskset.c
index 53f85375f..650150d98 100644
--- a/system/taskset/taskset.c
+++ b/system/taskset/taskset.c
@@ -88,8 +88,7 @@ int main(int argc, FAR char *argv[])
   int rc;
   int i;
 
-  memset(command, 0, sizeof(command));
-
+  command[0] = '\0';
   CPU_ZERO(&cpuset);
 
   /* Parse command line options */
@@ -156,8 +155,8 @@ int main(int argc, FAR char *argv[])
 
           for (i = 0; i < argc - 2; i++)
             {
-              strcat(command, argv[i + 2]);
-              strcat(command, " ");
+              strlcat(command, argv[i + 2], sizeof(command));
+              strlcat(command, " ", sizeof(command));
             }
 
           sched_setaffinity(gettid(), sizeof(cpu_set_t), &cpuset);
diff --git a/system/termcurses/tcurses_vt100.c b/system/termcurses/tcurses_vt100.c
index 447b973a6..62791527c 100644
--- a/system/termcurses/tcurses_vt100.c
+++ b/system/termcurses/tcurses_vt100.c
@@ -1091,23 +1091,23 @@ static int tcurses_vt100_setattributes(FAR struct termcurses_s *dev,
 
   if (attrib & TCURS_ATTRIB_BLINK)
     {
-      strcat(str, g_setblink);
+      strlcat(str, g_setblink, sizeof(str));
     }
   else
     {
-      strcat(str, g_setnoblink);
+      strlcat(str, g_setnoblink, sizeof(str));
     }
 
   if (attrib & TCURS_ATTRIB_UNDERLINE)
     {
-      strcat(str, g_setunderline);
+      strlcat(str, g_setunderline, sizeof(str));
     }
   else
     {
-      strcat(str, g_setnounderline);
+      strlcat(str, g_setnounderline, sizeof(str));
     }
 
-  strcat(str, "m");
+  strlcat(str, "m", sizeof(str));
 
   ret = write(fd, str, strlen(str));
 
diff --git a/system/trace/trace.c b/system/trace/trace.c
index 672f42e51..bdcb9c36f 100644
--- a/system/trace/trace.c
+++ b/system/trace/trace.c
@@ -261,11 +261,11 @@ static int trace_cmd_cmd(int index, int argc, FAR char **argv, int notectlfd)
       return ERROR;
     }
 
-  memset(command, 0, sizeof(command));
+  command[0] = '\0';
   while (index < argc)
     {
-      strcat(command, argv[index]);
-      strcat(command, " ");
+      strlcat(command, argv[index], sizeof(command));
+      strlcat(command, " ", sizeof(command));
       index++;
     }
 
diff --git a/testing/fatutf8/fatutf8_main.c b/testing/fatutf8/fatutf8_main.c
index 1ec56864f..cc42cae47 100644
--- a/testing/fatutf8/fatutf8_main.c
+++ b/testing/fatutf8/fatutf8_main.c
@@ -108,7 +108,7 @@ int main(int argc, FAR char *argv[])
 
   printf("\n");
 
-  strcat(path, FILE_NAME);
+  strlcat(path, FILE_NAME, sizeof(path));
 
   printf("open(%s)\n", path);
   fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0777);
diff --git a/testing/fstest/fstest_main.c b/testing/fstest/fstest_main.c
index 0c701309a..6f757aa3d 100644
--- a/testing/fstest/fstest_main.c
+++ b/testing/fstest/fstest_main.c
@@ -1021,7 +1021,7 @@ int main(int argc, FAR char *argv[])
 
   if (ctx->mountdir[strlen(ctx->mountdir)-1] != '/')
     {
-      strcat(ctx->mountdir, "/");
+      strlcat(ctx->mountdir, "/", sizeof(ctx->mountdir));
     }
 
   ctx->fileimage = calloc(ctx->max_file, 1);