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>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC 5025d8450a4c8bf9d22a202433072bee780b9b72
1 file changed +12 -21
lockfile.c
+12 -21
@@ -126,44 +126,35 @@ static char *last_path_elm(char *p)
126 static char *resolve_symlink(char *p, size_t s)
127 {
128 int depth = MAXDEPTH;
129 + static struct strbuf link = STRBUF_INIT;
130
131 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;
134
145 - if (is_absolute_path(link)) {
135 + if (is_absolute_path(link.buf)) {
136 /* absolute path simply replaces p */
147 - if (link_len < s)
148 - strcpy(p, link);
137 + if (link.len < s)
138 + strcpy(p, link.buf);
139 else {
140 warning("%s: symlink too long", p);
151 - return p;
141 + break;
142 }
143 } else {
144 /*
155 - * link is a relative path, so I must replace the
145 + * link is a relative path, so replace the
146 * last element of p with it.
147 */
148 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);
151 else {
152 warning("%s: symlink too long", p);
163 - return p;
153 + break;
154 }
155 }
156 }
157 + strbuf_reset(&link);
158 return p;
159 }
160