From: Andreas Ericsson Date: Wed, 16 Nov 2005 00:42:15 GMT Subject: Re: [PATCH 1/3] C implementation of the 'git' program, take two. Message-ID: <437A8067.9050308@op5.se> In-Reply-To: Linus Torvalds wrote: > > 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. :) > > 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. > > 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? > 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