Skip to content

Commit

Permalink
resolve_symlink(): use a strbuf for internal scratch space
Browse files Browse the repository at this point in the history
Aside from shortening and simplifying the code, this removes another
place where the path name length is arbitrarily limited.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michael Haggerty authored and Junio C Hamano committed Oct 1, 2014
1 parent cf6950d commit 5025d84
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions lockfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,44 +126,35 @@ static char *last_path_elm(char *p)
static char *resolve_symlink(char *p, size_t s)
{
int depth = MAXDEPTH;
static struct strbuf link = STRBUF_INIT;

while (depth--) {
char link[PATH_MAX];
int link_len = readlink(p, link, sizeof(link));
if (link_len < 0) {
/* not a symlink anymore */
return p;
}
else if (link_len < sizeof(link))
/* readlink() never null-terminates */
link[link_len] = '\0';
else {
warning("%s: symlink too long", p);
return p;
}
if (strbuf_readlink(&link, p, strlen(p)) < 0)
break;

if (is_absolute_path(link)) {
if (is_absolute_path(link.buf)) {
/* absolute path simply replaces p */
if (link_len < s)
strcpy(p, link);
if (link.len < s)
strcpy(p, link.buf);
else {
warning("%s: symlink too long", p);
return p;
break;
}
} else {
/*
* link is a relative path, so I must replace the
* link is a relative path, so replace the
* last element of p with it.
*/
char *r = (char *)last_path_elm(p);
if (r - p + link_len < s)
strcpy(r, link);
if (r - p + link.len < s)
strcpy(r, link.buf);
else {
warning("%s: symlink too long", p);
return p;
break;
}
}
}
strbuf_reset(&link);
return p;
}

Expand Down

0 comments on commit 5025d84

Please sign in to comment.