All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 4/5] shorten_unambiguous_ref: use xsnprintf
Date: Fri, 18 May 2018 18:58:20 -0700	[thread overview]
Message-ID: <20180519015819.GD32492@sigill.intra.peff.net> (raw)
In-Reply-To: <20180519015444.GA12080@sigill.intra.peff.net>

We convert the ref_rev_parse_rules array into scanf formats
on the fly, and use snprintf() to write into each string. We
should have enough memory to hold everything because of the
earlier total_len computation. Let's use xsnprintf() to
give runtime confirmation that this is the case, and to make
it easy for people auditing the code to know there's no
truncation bug.

Signed-off-by: Jeff King <peff@peff.net>
---
 refs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index 64aadd14c9..73c446628f 100644
--- a/refs.c
+++ b/refs.c
@@ -1147,8 +1147,8 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
 		for (i = 0; i < nr_rules; i++) {
 			assert(offset < total_len);
 			scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
-			offset += snprintf(scanf_fmts[i], total_len - offset,
-					   ref_rev_parse_rules[i], 2, "%s") + 1;
+			offset += xsnprintf(scanf_fmts[i], total_len - offset,
+					    ref_rev_parse_rules[i], 2, "%s") + 1;
 		}
 	}
 
-- 
2.17.0.1052.g7d69f75dbf


  parent reply	other threads:[~2018-05-19  1:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-19  1:54 [PATCH 0/5] snprintf truncation fixes Jeff King
2018-05-19  1:56 ` [PATCH 1/5] http: use strbufs instead of fixed buffers Jeff King
2018-05-21 18:11   ` Stefan Beller
2018-05-21 19:41     ` Jeff King
2018-05-21 20:57       ` Stefan Beller
2018-05-19  1:57 ` [PATCH 2/5] log_write_email_headers: use strbufs Jeff King
2018-05-19  1:57 ` [PATCH 3/5] query_fsmonitor: use xsnprintf for formatting integers Jeff King
2018-05-19  8:27   ` René Scharfe
2018-05-20 17:08     ` Jeff King
2018-05-21  0:58     ` Junio C Hamano
2018-05-21 12:36     ` Ben Peart
2018-05-19  1:58 ` Jeff King [this message]
2018-05-19  1:58 ` [PATCH 5/5] fmt_with_err: add a comment that truncation is OK Jeff King

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=20180519015819.GD32492@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.