git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 3/6] pretty: prepare notes message at a centralized place
Date: Thu, 18 Oct 2012 03:49:21 -0400	[thread overview]
Message-ID: <20121018074921.GC9999@sigill.intra.peff.net> (raw)
In-Reply-To: <1350539128-21577-4-git-send-email-gitster@pobox.com>

On Wed, Oct 17, 2012 at 10:45:25PM -0700, Junio C Hamano wrote:

> +	if (opt->show_notes) {
> +		int raw;
> +		struct strbuf notebuf = STRBUF_INIT;
> +
> +		raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
> +		format_display_notes(commit->object.sha1, &notebuf,
> +				     get_log_output_encoding(), raw);
> +		ctx.notes_message = notebuf.len
> +			? strbuf_detach(&notebuf, NULL)
> +			: xcalloc(1, 1);
> +	}

This last line seems like it is caused by a bug in the strbuf API.
Detaching an empty string will sometimes get you NULL and sometimes not.
For example, this:

  struct strbuf foo = STRBUF_INIT;
  strbuf_detach(&foo, NULL);

will return NULL. But this:

  struct strbuf foo = STRBUF_INIT;
  strbuf_addstr(&foo, "bar");
  strbuf_reset(&foo);
  strbuf_detach(&foo, NULL);

will get you a zero-length string. Which just seems insane to me. The
whole point of strbuf_detach is that you do not have to care about the
internal representation. It should probably always return a newly
allocated zero-length string.

Looking through a few uses of strbuf_detach, it looks like callers
assume they will always get a pointer from strbuf_detach, and we are
saved by implementation details. For example, sha1_file_to_archive might
have an empty file, but the fact that strbuf_attach always allocates a
byte means that the detach will never return NULL. Similarly,
argv_array_pushf would never want to turn an empty string into an
accidental NULL; it is saved by the fact that strbuf_vaddf will always
preemptively allocate 64 bytes.

It's possible that switching it would create bugs elsewhere (there are
over 100 uses of strbuf_detach, so maybe somebody really does want this
NULL behavior), but I tend to think it is just as likely to be fixing
undiscovered bugs.

-Peff

  reply	other threads:[~2012-10-18  7:49 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-18  5:45 [PATCH 0/6] Bring "format-patch --notes" closer to a real feature Junio C Hamano
2012-10-18  5:45 ` [PATCH 1/6] pretty: remove reencode_commit_message() Junio C Hamano
2012-10-18  5:45 ` [PATCH 2/6] format_note(): simplify API Junio C Hamano
2012-10-18  5:45 ` [PATCH 3/6] pretty: prepare notes message at a centralized place Junio C Hamano
2012-10-18  7:49   ` Jeff King [this message]
2012-10-18  9:17     ` Junio C Hamano
2012-10-18  9:18       ` Jeff King
2012-10-18 10:00     ` [PATCH] strbuf: always return a non-NULL value from strbuf_detach Jeff King
2012-10-18  5:45 ` [PATCH 4/6] pretty_print_commit(): do not append notes message Junio C Hamano
2012-10-18  5:45 ` [PATCH 5/6] format-patch: append --signature after notes Junio C Hamano
2012-10-18  5:45 ` [PATCH 6/6] format-patch --notes: show notes after three-dashes Junio C Hamano
2012-10-18 21:35   ` Philip Oakley
2012-10-18 22:08     ` Junio C Hamano
2012-10-19 20:06       ` Junio C Hamano
2012-10-21 21:33       ` Philip Oakley
2012-10-18 10:02 ` [PATCH 0/6] Bring "format-patch --notes" closer to a real feature Jeff King
2012-10-18 11:06 ` Nguyen Thai Ngoc Duy
2012-10-18 12:24   ` Michael J Gruber
2012-10-18 17:04   ` Junio C Hamano

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=20121018074921.GC9999@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).