Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some temporary directory handling changes #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions clar/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <sys/syslimits.h>
#endif

static char _clar_path[4096 + 1];
#define CLAR_PATH_MAX 4096
static char _clar_path[CLAR_PATH_MAX];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we lose the 4097th byte here?


static int
is_valid_tmp_path(const char *path)
Expand Down Expand Up @@ -35,10 +36,9 @@ find_tmp_path(char *buffer, size_t length)
continue;

if (is_valid_tmp_path(env)) {
#ifdef __APPLE__
if (length >= PATH_MAX && realpath(env, buffer) != NULL)
return 0;
#endif
if (strlen(env) + 1 > CLAR_PATH_MAX)
return -1;

strncpy(buffer, env, length - 1);
buffer[length - 1] = '\0';
return 0;
Expand All @@ -47,10 +47,6 @@ find_tmp_path(char *buffer, size_t length)

/* If the environment doesn't say anything, try to use /tmp */
if (is_valid_tmp_path("/tmp")) {
#ifdef __APPLE__
if (length >= PATH_MAX && realpath("/tmp", buffer) != NULL)
return 0;
#endif
strncpy(buffer, "/tmp", length - 1);
buffer[length - 1] = '\0';
return 0;
Expand All @@ -75,6 +71,34 @@ find_tmp_path(char *buffer, size_t length)
return -1;
}

static int canonicalize_tmp_path(char *buffer)
{
#ifdef _WIN32
char tmp[CLAR_PATH_MAX];
DWORD ret;

ret = GetFullPathName(buffer, CLAR_PATH_MAX, tmp, NULL);

if (ret == 0 || ret > CLAR_PATH_MAX)
return -1;

ret = GetLongPathName(tmp, buffer, CLAR_PATH_MAX);

if (ret == 0 || ret > CLAR_PATH_MAX)
return -1;

return 0;
#else
char tmp[CLAR_PATH_MAX];

if (realpath(buffer, tmp) == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry a bit that realpath may not be available on all systems, as it is only specified by POSIX.1-2001 and thus not part of C90 itself. And unfortunately we have folks that compile Git on systems that predate POSIX.1-2001 itself.

I sometimes wish that some kind of resource existed that provides a matrix of operating system <-> availability of certain features.

return -1;

strcpy(buffer, tmp);
return 0;
#endif
}

static void clar_unsandbox(void)
{
if (_clar_path[0] == '\0')
Expand All @@ -95,7 +119,8 @@ static int build_sandbox_path(void)

size_t len;

if (find_tmp_path(_clar_path, sizeof(_clar_path)) < 0)
if (find_tmp_path(_clar_path, sizeof(_clar_path)) < 0 ||
canonicalize_tmp_path(_clar_path) < 0)
return -1;

len = strlen(_clar_path);
Expand Down