]> git.proxmox.com Git - libgit2.git/blobdiff - tests/clar/sandbox.h
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / clar / sandbox.h
index a44e29116b4edeb4c580155434f694c99a25bea0..0ba1479620ab351e8fc9750f97fd840ada543637 100644 (file)
@@ -1,4 +1,8 @@
-static char _clar_path[4096];
+#ifdef __APPLE__
+#include <sys/syslimits.h>
+#endif
+
+static char _clar_path[4096 + 1];
 
 static int
 is_valid_tmp_path(const char *path)
@@ -31,14 +35,24 @@ find_tmp_path(char *buffer, size_t length)
                        continue;
 
                if (is_valid_tmp_path(env)) {
-                       strncpy(buffer, env, length);
+#ifdef __APPLE__
+                       if (length >= PATH_MAX && realpath(env, buffer) != NULL)
+                               return 0;
+#endif
+                       strncpy(buffer, env, length - 1);
+                       buffer[length - 1] = '\0';
                        return 0;
                }
        }
 
        /* If the environment doesn't say anything, try to use /tmp */
        if (is_valid_tmp_path("/tmp")) {
-               strncpy(buffer, "/tmp", length);
+#ifdef __APPLE__
+               if (length >= PATH_MAX && realpath("/tmp", buffer) != NULL)
+                       return 0;
+#endif
+               strncpy(buffer, "/tmp", length - 1);
+               buffer[length - 1] = '\0';
                return 0;
        }
 
@@ -53,7 +67,8 @@ find_tmp_path(char *buffer, size_t length)
 
        /* This system doesn't like us, try to use the current directory */
        if (is_valid_tmp_path(".")) {
-               strncpy(buffer, ".", length);
+               strncpy(buffer, ".", length - 1);
+               buffer[length - 1] = '\0';
                return 0;
        }
 
@@ -65,14 +80,19 @@ static void clar_unsandbox(void)
        if (_clar_path[0] == '\0')
                return;
 
-       chdir("..");
+       cl_must_pass(chdir(".."));
 
        fs_rm(_clar_path);
 }
 
 static int build_sandbox_path(void)
 {
+#ifdef CLAR_TMPDIR
+       const char path_tail[] = CLAR_TMPDIR "_XXXXXX";
+#else
        const char path_tail[] = "clar_tmp_XXXXXX";
+#endif
+
        size_t len;
 
        if (find_tmp_path(_clar_path, sizeof(_clar_path)) < 0)