diff options
author | Jeff King <peff@peff.net> | 2015-09-24 17:08:35 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-10-05 11:08:06 -0700 |
commit | 00b6c178c3ab475098f7a0bc63b2df2da508020c (patch) | |
tree | eefc52933eb00b24ffb18ce489b05cc32913dd89 /diff-no-index.c | |
parent | f0766bf94ec7a398a5a524053ea5334883c16a2e (diff) | |
download | git-00b6c178c3ab475098f7a0bc63b2df2da508020c.tar.gz |
use strbuf_complete to conditionally append slash
When working with paths in strbufs, we frequently want to
ensure that a directory contains a trailing slash before
appending to it. We can shorten this code (and make the
intent more obvious) by calling strbuf_complete.
Most of these cases are trivially identical conversions, but
there are two things to note:
- in a few cases we did not check that the strbuf is
non-empty (which would lead to an out-of-bounds memory
access). These were generally not triggerable in
practice, either from earlier assertions, or typically
because we would have just fed the strbuf to opendir(),
which would choke on an empty path.
- in a few cases we indexed the buffer with "original_len"
or similar, rather than the current sb->len, and it is
not immediately obvious from the diff that they are the
same. In all of these cases, I manually verified that
the strbuf does not change between the assignment and
the strbuf_complete call.
This does not convert cases which look like:
if (sb->len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
as those are obviously semantically different. Some of these
cases arguably should be doing that, but that is out of
scope for this change, which aims purely for cleanup with no
behavior change (and at least it will make such sites easier
to find and examine in the future, as we can grep for
strbuf_complete).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'diff-no-index.c')
-rw-r--r-- | diff-no-index.c | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/diff-no-index.c b/diff-no-index.c index 0320605a84..8e0fd270b5 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -136,15 +136,13 @@ static int queue_diff(struct diff_options *o, if (name1) { strbuf_addstr(&buffer1, name1); - if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/') - strbuf_addch(&buffer1, '/'); + strbuf_complete(&buffer1, '/'); len1 = buffer1.len; } if (name2) { strbuf_addstr(&buffer2, name2); - if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/') - strbuf_addch(&buffer2, '/'); + strbuf_complete(&buffer2, '/'); len2 = buffer2.len; } |