You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2020/06/16 11:47:49 UTC

[incubator-nuttx] branch master updated: mkstemp: Only look at the trailing Xs

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/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 66052f7  mkstemp: Only look at the trailing Xs
66052f7 is described below

commit 66052f7c4c68ab5dab9292ad04f35b07352c9e74
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Tue Jun 16 17:21:28 2020 +0900

    mkstemp: Only look at the trailing Xs
    
    As it's stated in the standards.
    
    The original code look at the first Xs. It can end up with
    an unexpected behavior, if a template contains multiple series of Xs.
    E.g. /tmp/XXXXXX/XXXXXX
---
 libs/libc/stdlib/lib_mkstemp.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/libs/libc/stdlib/lib_mkstemp.c b/libs/libc/stdlib/lib_mkstemp.c
index feb95d8..9fcc2df 100644
--- a/libs/libc/stdlib/lib_mkstemp.c
+++ b/libs/libc/stdlib/lib_mkstemp.c
@@ -198,29 +198,28 @@ int mkstemp(FAR char *path_template)
   uint8_t base62[MAX_XS];
   uint32_t retries;
   FAR char *xptr;
-  FAR char *ptr;
   int xlen;
   int fd;
   int i;
 
   /* Count the number of X's at the end of the template */
 
-  xptr = strchr(path_template, 'X');
-  if (!xptr)
+  xptr = &path_template[strlen(path_template)];
+  for (xlen = 0; xlen < MAX_XS && path_template < xptr && *(xptr - 1) == 'X';
+       xlen++, xptr--);
+
+  if (xlen == 0)
     {
       /* No Xs?  There should always really be 6 */
 
       return open(path_template, O_RDWR | O_CREAT | O_EXCL, 0666);
     }
 
-  /* There is at least one.. count all of them */
-
-  for (xlen = 0, ptr = xptr; xlen < MAX_XS && *ptr == 'X'; xlen++, ptr++);
-
   /* Ignore any X's after the sixth */
 
   if (xlen > MAX_XS)
     {
+      xptr += xlen - MAX_XS;
       xlen = MAX_XS;
     }