Imported Upstream version 3.4.11
[platform/upstream/gnutls.git] / src / libopts / load.c
index 71e57ec..5c82e12 100644 (file)
@@ -12,7 +12,7 @@
 /*
  *  This file is part of AutoOpts, a companion to AutoGen.
  *  AutoOpts is free software.
- *  AutoOpts is Copyright (C) 1992-2013 by Bruce Korb - all rights reserved
+ *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
  *
  *  AutoOpts is available under any one of two licenses.  The license
  *  in use must be one of these two and the choice is under the control
@@ -94,10 +94,10 @@ get_realpath(char * buf, size_t b_sz)
  * private:
  *
  * what:  translate and construct a path
- * arg:   + char*       + p_buf     + The result buffer +
- * arg:   + int         + b_sz      + The size of this buffer +
- * arg:   + char const* + fname     + The input name +
- * arg:   + char const* + prg_path  + The full path of the current program +
+ * arg:   + char *       + p_buf     + The result buffer +
+ * arg:   + int          + b_sz      + The size of this buffer +
+ * arg:   + char const * + fname     + The input name +
+ * arg:   + char const * + prg_path  + The full path of the current program +
  *
  * ret-type: bool
  * ret-desc: true if the name was handled, otherwise false.
@@ -207,6 +207,8 @@ add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
     char const *   path;
     char const *   pz;
     int     skip = 2;
+    size_t  fname_len;
+    size_t  dir_len;  //!< length of the directory portion of the path to the exe
 
     switch (fname[2]) {
     case DIRCH:
@@ -225,7 +227,7 @@ add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
     if (strchr(prg_path, DIRCH) != NULL)
         path = prg_path;
     else {
-        path = pathfind(getenv("PATH"), (char*)prg_path, "rx");
+        path = pathfind(getenv("PATH"), (char *)prg_path, "rx");
 
         if (path == NULL)
             return false;
@@ -240,17 +242,19 @@ add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
     if (pz == NULL)
         return false;
 
-    fname += skip;
+    fname    += skip;
+    fname_len = strlen(fname) + 1; // + NUL byte
+    dir_len   = (pz - path) + 1;   // + dir sep character
 
     /*
      *  Concatenate the file name to the end of the executable path.
      *  The result may be either a file or a directory.
      */
-    if ((unsigned)(pz - path) + 1 + strlen(fname) >= (unsigned)b_sz)
+    if (dir_len + fname_len > (unsigned)b_sz)
         return false;
 
-    memcpy(buf, path, (size_t)((pz - path)+1));
-    strcpy(buf + (pz - path) + 1, fname);
+    memcpy(buf, path, dir_len);
+    memcpy(buf + dir_len, fname, fname_len);
 
     /*
      *  If the "path" path was gotten from "pathfind()", then it was
@@ -289,10 +293,16 @@ add_env_val(char * buf, int buf_sz, char const * name)
     if (dir_part == NULL)
         return false;
 
-    if (strlen(dir_part) + 1 + strlen(name) >= (unsigned)buf_sz)
-        return false;
+    {
+        size_t dir_len = strlen(dir_part);
+        size_t nm_len  = strlen(name) + 1;
+        
+        if (dir_len + nm_len >= (unsigned)buf_sz)
+            return false;
+        memcpy(buf, dir_part, dir_len);
+        memcpy(buf + dir_len, name, nm_len);
+    }
 
-    sprintf(buf, "%s%s", dir_part, name);
     return true;
 }
 
@@ -307,7 +317,7 @@ add_env_val(char * buf, int buf_sz, char const * name)
 LOCAL void
 munge_str(char * txt, tOptionLoadMode mode)
 {
-    char * pzE;
+    char * end;
 
     if (mode == OPTION_LOAD_KEEP)
         return;
@@ -316,13 +326,13 @@ munge_str(char * txt, tOptionLoadMode mode)
         char * src = SPN_WHITESPACE_CHARS(txt+1);
         size_t l   = strlen(src) + 1;
         memmove(txt, src, l);
-        pzE = txt + l - 1;
+        end = txt + l - 1;
 
     } else
-        pzE = txt + strlen(txt);
+        end = txt + strlen(txt);
 
-    pzE  = SPN_WHITESPACE_BACK(txt, pzE);
-    *pzE = NUL;
+    end  = SPN_WHITESPACE_BACK(txt, end);
+    *end = NUL;
 
     if (mode == OPTION_LOAD_UNCOOKED)
         return;
@@ -333,7 +343,7 @@ munge_str(char * txt, tOptionLoadMode mode)
     case '\'': break;
     }
 
-    switch (pzE[-1]) {
+    switch (end[-1]) {
     default: return;
     case '"':
     case '\'': break;
@@ -544,8 +554,8 @@ load_opt_line(tOptions * opts, tOptState * opt_state, char * line,
  *
  * what:  process a string for an option name and value
  *
- * arg:   tOptions*,   opts,  program options descriptor
- * arg:   char const*, line,  NUL-terminated text
+ * arg:   tOptions *,   opts,  program options descriptor
+ * arg:   char const *, line,  NUL-terminated text
  *
  * doc:
  *