Revision: 48500
          http://brlcad.svn.sourceforge.net/brlcad/?rev=48500&view=rev
Author:   starseeker
Date:     2012-01-12 15:22:26 +0000 (Thu, 12 Jan 2012)
Log Message:
-----------
Wrap realpath/GetFullPathName logic into a bu routine for re-use in other 
locations - move it out of brlcad_path.c

Modified Paths:
--------------
    brlcad/trunk/include/bu.h
    brlcad/trunk/src/libbu/brlcad_path.c
    brlcad/trunk/src/libbu/file.c

Modified: brlcad/trunk/include/bu.h
===================================================================
--- brlcad/trunk/include/bu.h   2012-01-12 15:22:06 UTC (rev 48499)
+++ brlcad/trunk/include/bu.h   2012-01-12 15:22:26 UTC (rev 48500)
@@ -2966,6 +2966,14 @@
  */
 BU_EXPORT extern size_t bu_file_glob(const char *pattern, char ***matches);
 
+/**
+ * Call canonicalization routines to both expand and validate
+ * a path name.  
+ *
+ * returns a pointer to the canonical path.  Caller must free
+ * the path.
+ */
+BU_EXPORT extern char * bu_file_path_canonicalize(const char *path);
 
 /** @file libbu/fnmatch.c
  *

Modified: brlcad/trunk/src/libbu/brlcad_path.c
===================================================================
--- brlcad/trunk/src/libbu/brlcad_path.c        2012-01-12 15:22:06 UTC (rev 
48499)
+++ brlcad/trunk/src/libbu/brlcad_path.c        2012-01-12 15:22:26 UTC (rev 
48500)
@@ -267,21 +267,8 @@
     /* run-time path identification */
     lhs = bu_argv0_full_path();
     if (lhs) {
-       char real_path[MAXPATHLEN] = {0};
        char *dirpath;
-#ifdef HAVE_REALPATH
-       dirpath = realpath(lhs, real_path);
-       if (!dirpath) {
-           /* if path lookup failed, resort to simple copy */
-           bu_strlcpy(real_path, lhs, (size_t)MAXPATHLEN);
-       }
-#else
-#  ifdef HAVE_GETFULLPATHNAME
-       GetFullPathName(lhs, MAXPATHLEN, real_path, NULL);
-#  else
-       bu_strlcpy(real_path, lhs, (size_t)MAXPATHLEN);
-#  endif
-#endif
+       char *real_path = bu_file_path_canonicalize(lhs);
        dirpath = bu_dirname(real_path);
        snprintf(real_path, MAXPATHLEN, "%s", dirpath);
        bu_free(dirpath, "free bu_dirname");
@@ -293,8 +280,10 @@
                bu_log("Found: Run-time path identification [%s]\n", result);
            }
            bu_vls_free(&searched);
+           bu_free(real_path, "free real_path");
            return result;
        }
+       bu_free(real_path, "free real_path");
     } else {
        snprintf(where, MAX_WHERE_SIZE, "\trun-time path identification 
[UNKNOWN]\n");
        bu_vls_strcat(&searched, where);

Modified: brlcad/trunk/src/libbu/file.c
===================================================================
--- brlcad/trunk/src/libbu/file.c       2012-01-12 15:22:06 UTC (rev 48499)
+++ brlcad/trunk/src/libbu/file.c       2012-01-12 15:22:26 UTC (rev 48500)
@@ -326,6 +326,30 @@
 }
 
 
+char *  
+bu_file_path_canonicalize(const char *path) 
+{
+  char *dirpath;
+  char *resolved_path = (char *)bu_calloc(MAXPATHLEN+1, sizeof(char), 
"resolved_path alloc");
+#ifdef HAVE_REALPATH
+  dirpath = realpath(path, resolved_path);
+  if (!dirpath) {
+    /* if path lookup failed, resort to simple copy */
+    bu_strlcpy(resolved_path, path, (size_t)MAXPATHLEN);
+  }
+#else
+  /* Best solution currently available for Windows
+   * See 
https://www.securecoding.cert.org/confluence/display/seccode/FIO02-C.+Canonicalize+path+names+originating+from+untrusted+sources
 */
+#  ifdef HAVE_GETFULLPATHNAME
+  GetFullPathName(lhs, MAXPATHLEN, resolved_path, NULL);
+#  else
+  /* Last resort - if NOTHING is defined, do a simple copy */
+  bu_strlcpy(resolved_path, lhs, (size_t)MAXPATHLEN);
+#  endif
+#endif
+  return resolved_path;
+}
+
 /*
  * Local Variables:
  * mode: C

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to