tag: use strbuf to format tag header

We format the tag header into a fixed 1024-byte buffer. But since the tag-name and tagger ident can be arbitrarily large, we may unceremoniously die with "tag header too big". Let's just use a strbuf instead. Note that it looks at first glance like we can just format this directly into the "buf" strbuf where it will ultimately go. But that buffer may already contain the tag message, and we have no easy way to prepend formatted data to a strbuf (we can only splice in an already-generated buffer). This isn't a performance-critical path, so going through an extra buffer isn't a big deal. Signed-off-by: Jeff King <peff@peff.net>

Jeff King committed Mar 28, 2017 at 15:46 UTC b0ceab98d72a4861a8cea061487169fb5468b736
1 file changed +12 -15
builtin/tag.c
+12 -15
@@ -231,26 +231,22 @@ static void create_tag(const unsigned char *object, const char *tag,
231 unsigned char *prev, unsigned char *result)
232 {
233 enum object_type type;
234 - char header_buf[1024];
235 - int header_len;
234 + struct strbuf header = STRBUF_INIT;
235 char *path = NULL;
236
237 type = sha1_object_info(object, NULL);
238 if (type <= OBJ_NONE)
239 die(_("bad object type."));
240
242 - header_len = snprintf(header_buf, sizeof(header_buf),
243 - "object %s\n"
244 - "type %s\n"
245 - "tag %s\n"
246 - "tagger %s\n\n",
247 - sha1_to_hex(object),
248 - typename(type),
249 - tag,
250 - git_committer_info(IDENT_STRICT));
251 -
252 - if (header_len > sizeof(header_buf) - 1)
253 - die(_("tag header too big."));
241 + strbuf_addf(&header,
242 + "object %s\n"
243 + "type %s\n"
244 + "tag %s\n"
245 + "tagger %s\n\n",
246 + sha1_to_hex(object),
247 + typename(type),
248 + tag,
249 + git_committer_info(IDENT_STRICT));
250
251 if (!opt->message_given) {
252 int fd;
@@ -288,7 +284,8 @@ static void create_tag(const unsigned char *object, const char *tag,
284 if (!opt->message_given && !buf->len)
285 die(_("no tag message?"));
286
291 - strbuf_insert(buf, 0, header_buf, header_len);
287 + strbuf_insert(buf, 0, header.buf, header.len);
288 + strbuf_release(&header);
289
290 if (build_tag_object(buf, opt->sign, result) < 0) {
291 if (path)