Skip to content

Commit 5025d84

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf6950d commit 5025d84

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

lockfile.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,44 +126,35 @@ static char *last_path_elm(char *p)
126126
static char *resolve_symlink(char *p, size_t s)
127127
{
128128
int depth = MAXDEPTH;
129+
static struct strbuf link = STRBUF_INIT;
129130

130131
while (depth--) {
131-
char link[PATH_MAX];
132-
int link_len = readlink(p, link, sizeof(link));
133-
if (link_len < 0) {
134-
/* not a symlink anymore */
135-
return p;
136-
}
137-
else if (link_len < sizeof(link))
138-
/* readlink() never null-terminates */
139-
link[link_len] = '\0';
140-
else {
141-
warning("%s: symlink too long", p);
142-
return p;
143-
}
132+
if (strbuf_readlink(&link, p, strlen(p)) < 0)
133+
break;
144134

145-
if (is_absolute_path(link)) {
135+
if (is_absolute_path(link.buf)) {
146136
/* absolute path simply replaces p */
147-
if (link_len < s)
148-
strcpy(p, link);
137+
if (link.len < s)
138+
strcpy(p, link.buf);
149139
else {
150140
warning("%s: symlink too long", p);
151-
return p;
141+
break;
152142
}
153143
} else {
154144
/*
155-
* link is a relative path, so I must replace the
145+
* link is a relative path, so replace the
156146
* last element of p with it.
157147
*/
158148
char *r = (char *)last_path_elm(p);
159-
if (r - p + link_len < s)
160-
strcpy(r, link);
149+
if (r - p + link.len < s)
150+
strcpy(r, link.buf);
161151
else {
162152
warning("%s: symlink too long", p);
163-
return p;
153+
break;
164154
}
165155
}
166156
}
157+
strbuf_reset(&link);
167158
return p;
168159
}
169160

0 commit comments

Comments
 (0)