From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: "René Scharfe" <l.s.r@web.de>
Subject: [PATCH 07/18] link_alt_odb_entry: handle normalize_path errors
Date: Mon, 3 Oct 2016 16:34:17 -0400 [thread overview]
Message-ID: <20161003203417.izcgwt4yz3yspdnm@sigill.intra.peff.net> (raw)
In-Reply-To: <20161003203321.rj5jepviwo57uhqw@sigill.intra.peff.net>
When we add a new alternate to the list, we try to normalize
out any redundant "..", etc. However, we do not look at the
return value of normalize_path_copy(), and will happily
continue with a path that could not be normalized. Worse,
the normalizing process is done in-place, so we are left
with whatever half-finished working state the normalizing
function was in.
Fortunately, this cannot cause us to read past the end of
our buffer, as that working state will always leave the
NUL from the original path in place. And we do tend to
notice problems when we check is_directory() on the path.
But you can see the nonsense that we feed to is_directory
with an entry like:
this/../../is/../../way/../../too/../../deep/../../to/../../resolve
in your objects/info/alternates, which yields:
error: object directory
/to/e/deep/too/way//ects/this/../../is/../../way/../../too/../../deep/../../to/../../resolve
does not exist; check .git/objects/info/alternates.
We can easily fix this just by checking the return value.
But that makes it hard to generate a good error message,
since we're normalizing in-place and our input value has
been overwritten by cruft.
Instead, let's provide a strbuf helper that does an in-place
normalize, but restores the original contents on error. This
uses a second buffer under the hood, which is slightly less
efficient, but this is not a performance-critical code path.
The strbuf helper can also properly set the "len" parameter
of the strbuf before returning. Just doing:
normalize_path_copy(buf.buf, buf.buf);
will shorten the string, but leave buf.len at the original
length. That may be confusing to later code which uses the
strbuf.
Signed-off-by: Jeff King <peff@peff.net>
---
sha1_file.c | 11 +++++++++--
strbuf.c | 20 ++++++++++++++++++++
strbuf.h | 8 ++++++++
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index b9c1fa3..68571bd 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -263,7 +263,12 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
}
strbuf_addstr(&pathbuf, entry);
- normalize_path_copy(pathbuf.buf, pathbuf.buf);
+ if (strbuf_normalize_path(&pathbuf) < 0) {
+ error("unable to normalize alternate object path: %s",
+ pathbuf.buf);
+ strbuf_release(&pathbuf);
+ return -1;
+ }
pfxlen = strlen(pathbuf.buf);
@@ -335,7 +340,9 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
}
strbuf_add_absolute_path(&objdirbuf, get_object_directory());
- normalize_path_copy(objdirbuf.buf, objdirbuf.buf);
+ if (strbuf_normalize_path(&objdirbuf) < 0)
+ die("unable to normalize object directory: %s",
+ objdirbuf.buf);
alt_copy = xmemdupz(alt, len);
string_list_split_in_place(&entries, alt_copy, sep, -1);
diff --git a/strbuf.c b/strbuf.c
index b839be4..8fec657 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -870,3 +870,23 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
strbuf_setlen(sb, j);
}
+
+int strbuf_normalize_path(struct strbuf *src)
+{
+ struct strbuf dst = STRBUF_INIT;
+
+ strbuf_grow(&dst, src->len);
+ if (normalize_path_copy(dst.buf, src->buf) < 0) {
+ strbuf_release(&dst);
+ return -1;
+ }
+
+ /*
+ * normalize_path does not tell us the new length, so we have to
+ * compute it by looking for the new NUL it placed
+ */
+ strbuf_setlen(&dst, strlen(dst.buf));
+ strbuf_swap(src, &dst);
+ strbuf_release(&dst);
+ return 0;
+}
diff --git a/strbuf.h b/strbuf.h
index ba8d5f1..2262b12 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -443,6 +443,14 @@ extern int strbuf_getcwd(struct strbuf *sb);
*/
extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Normalize in-place the path contained in the strbuf. See
+ * normalize_path_copy() for details. If an error occurs, the contents of "sb"
+ * are left untouched, and -1 is returned.
+ */
+extern int strbuf_normalize_path(struct strbuf *sb);
+
/**
* Strip whitespace from a buffer. The second parameter controls if
* comments are considered contents to be removed or not.
--
2.10.0.618.g82cc264
next prev parent reply other threads:[~2016-10-03 20:34 UTC|newest]
Thread overview: 84+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-03 20:33 [PATCH 0/18] alternate object database cleanups Jeff King
2016-10-03 20:33 ` [PATCH 01/18] t5613: drop reachable_via function Jeff King
2016-10-04 5:48 ` Jacob Keller
2016-10-04 13:43 ` Jeff King
2016-10-03 20:33 ` [PATCH 02/18] t5613: drop test_valid_repo function Jeff King
2016-10-04 5:50 ` Jacob Keller
2016-10-03 20:34 ` [PATCH 03/18] t5613: use test_must_fail Jeff King
2016-10-04 5:51 ` Jacob Keller
2016-10-03 20:34 ` [PATCH 04/18] t5613: whitespace/style cleanups Jeff King
2016-10-04 5:52 ` Jacob Keller
2016-10-04 13:47 ` Jeff King
2016-10-04 20:41 ` Jacob Keller
2016-10-03 20:34 ` [PATCH 05/18] t5613: do not chdir in main process Jeff King
2016-10-04 5:54 ` Jacob Keller
2016-10-04 21:00 ` Junio C Hamano
2016-10-03 20:34 ` [PATCH 06/18] t5613: clarify "too deep" recursion tests Jeff King
2016-10-04 5:57 ` Jacob Keller
2016-10-04 13:48 ` Jeff King
2016-10-04 20:44 ` Jacob Keller
2016-10-04 20:49 ` Jeff King
2016-10-04 20:52 ` Jacob Keller
2016-10-04 20:55 ` Jeff King
2016-10-04 20:58 ` Stefan Beller
2016-10-04 21:00 ` Jeff King
2016-10-05 13:58 ` Jakub Narębski
2016-10-05 14:40 ` Jeff King
2016-10-05 16:14 ` Junio C Hamano
2016-10-05 16:47 ` Jacob Keller
2016-10-04 21:43 ` Jacob Keller
2016-10-04 21:49 ` Jeff King
2016-10-04 21:50 ` Jacob Keller
2016-10-03 20:34 ` Jeff King [this message]
2016-10-04 6:01 ` [PATCH 07/18] link_alt_odb_entry: handle normalize_path errors Jacob Keller
2016-10-04 21:08 ` Junio C Hamano
2016-10-05 18:47 ` René Scharfe
2016-10-05 19:04 ` Jeff King
2016-11-07 23:42 ` Bryan Turner
2016-11-08 0:30 ` Jeff King
2016-11-08 1:12 ` Bryan Turner
2016-11-08 5:33 ` Jeff King
2016-11-08 19:27 ` Bryan Turner
2016-10-03 20:34 ` [PATCH 08/18] link_alt_odb_entry: refactor string handling Jeff King
2016-10-04 6:05 ` Jacob Keller
2016-10-04 13:53 ` Jeff King
2016-10-04 20:46 ` Jacob Keller
2016-10-04 21:18 ` Junio C Hamano
2016-10-03 20:35 ` [PATCH 09/18] alternates: provide helper for adding to alternates list Jeff King
2016-10-04 6:07 ` Jacob Keller
2016-10-03 20:35 ` [PATCH 10/18] alternates: provide helper for allocating alternate Jeff King
2016-10-04 6:09 ` Jacob Keller
2016-10-03 20:35 ` [PATCH 11/18] alternates: encapsulate alt->base munging Jeff King
2016-10-03 20:35 ` [PATCH 12/18] alternates: use a separate scratch space Jeff King
2016-10-04 6:12 ` Jacob Keller
2016-10-04 21:29 ` Junio C Hamano
2016-10-04 21:32 ` Jeff King
2016-10-04 21:49 ` Junio C Hamano
2016-10-04 21:51 ` Jeff King
2016-10-03 20:35 ` [PATCH 13/18] fill_sha1_file: write "boring" characters Jeff King
2016-10-04 6:13 ` Jacob Keller
2016-10-04 21:46 ` Junio C Hamano
2016-10-04 21:48 ` Jeff King
2016-10-04 21:49 ` Jacob Keller
2016-10-05 19:35 ` Junio C Hamano
2016-10-03 20:36 ` [PATCH 14/18] alternates: store scratch buffer as strbuf Jeff King
2016-10-03 20:36 ` [PATCH 15/18] fill_sha1_file: write into a strbuf Jeff King
2016-10-04 6:44 ` Jacob Keller
2016-10-03 20:36 ` [PATCH 16/18] count-objects: report alternates via verbose mode Jeff King
2016-10-04 6:46 ` Jacob Keller
2016-10-04 13:56 ` Jeff King
2016-10-05 14:23 ` Jakub Narębski
2016-10-05 18:47 ` René Scharfe
2016-10-03 20:36 ` [PATCH 17/18] sha1_file: always allow relative paths to alternates Jeff King
2016-10-04 6:50 ` Jacob Keller
2016-10-04 14:00 ` Jeff King
2016-10-03 20:36 ` [PATCH 18/18] alternates: use fspathcmp to detect duplicates Jeff King
2016-10-04 6:51 ` Jacob Keller
2016-10-04 14:10 ` Jeff King
2016-10-04 21:42 ` Junio C Hamano
2016-10-05 2:34 ` Aaron Schrab
2016-10-05 3:54 ` Jeff King
2016-10-04 5:47 ` [PATCH 0/18] alternate object database cleanups Jacob Keller
2016-10-04 13:41 ` Jeff King
2016-10-04 20:40 ` Jacob Keller
2016-10-05 18:47 ` René Scharfe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161003203417.izcgwt4yz3yspdnm@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=l.s.r@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).