Re: [PATCH 1/3] C implementation of the 'git' program, take two.
From: Linus Torvalds <torvalds@osdl.org>
Date: 2016-06-15 22:42:12
On Wed, 16 Nov 2005, Andreas Ericsson wrote:
It was your idea to begin with, actually, in the thread on how to do proper path validation in the git-daemon. :)
But that was because the important part wasn't to get an absolute path, but because the important part was to get the _canonical_ path. That was for security. Besides, I don't think we ever switched back. We just did a chdir() + a getcwd().
quoted
Why don't you just do if (exec_path[0] != '/') { .. prepend "cwd/" to exec_path ..You mean setenv("PATH", concat3(cwd, exec_path, old_path), 1);
No.
I mean exactly what I said.
I mean testing whether the exec_path[] is already absolute, and not
touching it at all if it is.
It's really as simple as something like
const char *absolute_path(const char *input)
{
int a, b;
char *buf;
char cwd[PATH_MAX];
if (*input == '/')
return input;
if (!getcwd(cwd, sizeof(cwd))
return NULL;
/* Do some trivial cleanup */
while (!strncmp(input, "./", 2)) {
input += 2;
while (*input == '/')
input++;
}
a = strlen(cwd);
b = strlen(input);
buf = malloc(a + b + 2);
if (!buf)
return NULL;
memcpy(buf, cwd, a);
buf[a] = '/';
memcpy(buf + a + 1, input, b);
buf[a + 1 + b] = 0;
return buf;
}
and there it is.
The magic rule being:
- if the path is already absolute, it's _good_. Don't play games with it.
- just append the dang thing with cwd. Don't play games (the above does
trivial simplification, which is unnecessary, but it's so simple that
hey, who cares? And it makes one common case a bit prettier)
Then, you just prepend it to the PATH, with a : in between (and if the
pathname has a ":" in it, tough, there's nothing we can do about it).
Linus