git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] C implementation of the 'git' program, take two.

From
Andreas Ericsson <ae@op5.se>
Date
Nov 16, 2005, 00:42 UTC
Message-ID
<437A8067.9050308@op5.se>
In-Reply-To
<Pine.LNX.4.64.0511151603510.11232@g5.osdl.org>
Linus Torvalds wrote:
Show 15 quoted lines
> 
> On Wed, 16 Nov 2005, Andreas Ericsson wrote:
> 
>>+
>>+	/* allow relative paths, but run with exact */
>>+	if (chdir(exec_path)) {
>>+		printf("git: '%s': %s\n", exec_path, strerror(errno));
>>+		exit (1);
>>+	}
>>+
>>+	getcwd(git_command, sizeof(git_command));
>>+	chdir(wd);
> 
> 
> Argh. This is pretty horrible way to turn a path into an absolute one. 

It was your idea to begin with, actually, in the thread on how to do proper path validation in the git-daemon. :)

Show 6 quoted lines
> 
> 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);
?

Because that can fail too. Every solution is bad if you twist and turn it enough.

Show 7 quoted lines
> 
> The reason to avoid "chdir(relative) + chdir(back)" is that it totally 
> unnecessarily breaks under some extreme cases. For example, if the 
> exec_path is already absolute, and we just happen to be in a really deep 
> subdirectory, then the getcwd() could have failed due to the PATH_MAX 
> limitations.
> 
True. So, would something like
char *try_goddamn_hard_to_get_working_dir(char *rel_path)
{
	size_t plen = PATH_MAX;
	char *p = malloc(PATH_MAX);
	while(p && plen < 1 << 20 && !(p = getcwd(p, plen))) {
		plen += PATH_MAX;
		p = realloc(p, plen);
	}
	return p;
}
be considered safe from this particular point of view?
Show 6 quoted lines
> Also, depending on getcwd() will not work if any parent directory is 
> unreadable or non-executable (well, under Linux it will, as long as it's 
> executable, since getcwd() is actually a system call. Not in UNIX in 
> general, though). Again, that means that unless you _have_ to know what 
> the cwd is, you should try to avoid relying on it.
> 
True. I'll redo that part (tomorrow).
> 
> and the nicer thign to do is to just not try to be clever.
> 
I never try. It just comes to me naturally. ;)
-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
Previous: Linus TorvaldsNext: Linus Torvalds
Message 6 of 9 in “C implementation of the 'git' program, take two.”
  1. 1/3 C implementation of the 'git' program, take two.Andreas Ericsson, Nov 15, 2005
  2. Junio C HamanoNov 15, 2005
  3. Andreas EricssonNov 16, 2005
  4. Junio C HamanoNov 16, 2005
  5. Linus TorvaldsNov 16, 2005
  6. Andreas EricssonNov 16, 2005
  7. Linus TorvaldsNov 16, 2005
  8. Johannes SchindelinNov 16, 2005
  9. Alex RiesenNov 16, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.