resolve_symlink(): use a strbuf for internal scratch space

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>
This commit is contained in:
Michael Haggerty 2014-10-01 12:28:33 +02:00 committed by Junio C Hamano
parent cf6950d3bf
commit 5025d8450a

View File

@ -126,44 +126,35 @@ static char *last_path_elm(char *p)
static char *resolve_symlink(char *p, size_t s) static char *resolve_symlink(char *p, size_t s)
{ {
int depth = MAXDEPTH; int depth = MAXDEPTH;
static struct strbuf link = STRBUF_INIT;
while (depth--) { while (depth--) {
char link[PATH_MAX]; if (strbuf_readlink(&link, p, strlen(p)) < 0)
int link_len = readlink(p, link, sizeof(link)); break;
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 (is_absolute_path(link)) { if (is_absolute_path(link.buf)) {
/* absolute path simply replaces p */ /* absolute path simply replaces p */
if (link_len < s) if (link.len < s)
strcpy(p, link); strcpy(p, link.buf);
else { else {
warning("%s: symlink too long", p); warning("%s: symlink too long", p);
return p; break;
} }
} else { } 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. * last element of p with it.
*/ */
char *r = (char *)last_path_elm(p); char *r = (char *)last_path_elm(p);
if (r - p + link_len < s) if (r - p + link.len < s)
strcpy(r, link); strcpy(r, link.buf);
else { else {
warning("%s: symlink too long", p); warning("%s: symlink too long", p);
return p; break;
} }
} }
} }
strbuf_reset(&link);
return p; return p;
} }