Change resolve_symlink() to take a strbuf rather than a string as parameter. This simplifies the code and removes an arbitrary pathname length restriction. It also means that lock_file's filename field no longer needs to be initialized to a large size. Signed-off-by: Michael Haggerty <mhagger@xxxxxxxxxxxx> --- lockfile.c | 53 +++++++++++++++++++---------------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/lockfile.c b/lockfile.c index 9413f7a..d78b6bf 100644 --- a/lockfile.c +++ b/lockfile.c @@ -119,53 +119,40 @@ static char *last_path_elm(char *p) #define MAXDEPTH 5 /* - * p = path that may be a symlink - * s = full size of p + * path contains a path that might be a symlink. * - * If p is a symlink, attempt to overwrite p with a path to the real - * file or directory (which may or may not exist), following a chain of - * symlinks if necessary. Otherwise, leave p unmodified. + * If path is a symlink, attempt to overwrite it with a path to the + * real file or directory (which may or may not exist), following a + * chain of symlinks if necessary. Otherwise, leave path unmodified. * - * This is a best-effort routine. If an error occurs, p will either be - * left unmodified or will name a different symlink in a symlink chain - * that started with p's initial contents. - * - * Always returns p. + * This is a best-effort routine. If an error occurs, path will + * either be left unmodified or will name a different symlink in a + * symlink chain that started with the original path. */ - -static char *resolve_symlink(char *p, size_t s) +static void resolve_symlink(struct strbuf *path) { int depth = MAXDEPTH; static struct strbuf link = STRBUF_INIT; while (depth--) { - if (strbuf_readlink(&link, p, strlen(p)) < 0) + if (strbuf_readlink(&link, path->buf, path->len) < 0) break; - if (is_absolute_path(link.buf)) { + if (is_absolute_path(link.buf)) /* absolute path simply replaces p */ - if (link.len < s) - strcpy(p, link.buf); - else { - warning("%s: symlink too long", p); - break; - } - } else { + strbuf_reset(path); + else { /* * 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.buf); - else { - warning("%s: symlink too long", p); - break; - } + char *r = last_path_elm(path->buf); + strbuf_setlen(path, r - path->buf); } + + strbuf_addbuf(path, &link); } strbuf_reset(&link); - return p; } /* Make sure errno contains a meaningful value on error */ @@ -185,16 +172,14 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) lk->active = 0; lk->owner = 0; lk->on_list = 1; - strbuf_init(&lk->filename, PATH_MAX); + strbuf_init(&lk->filename, 0); lk->next = lock_file_list; lock_file_list = lk; } strbuf_addstr(&lk->filename, path); - if (!(flags & LOCK_NODEREF)) { - resolve_symlink(lk->filename.buf, lk->filename.alloc); - strbuf_setlen(&lk->filename, strlen(lk->filename.buf)); - } + if (!(flags & LOCK_NODEREF)) + resolve_symlink(&lk->filename); strbuf_addstr(&lk->filename, LOCK_SUFFIX); lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666); if (lk->fd < 0) { -- 2.1.0 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html