All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: Re: [PATCH v3 08/18] rerere: explain the rerere I/O abstraction
Date: Fri, 24 Jul 2015 13:42:38 -0700	[thread overview]
Message-ID: <xmqqio99l22p.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1437171880-21590-9-git-send-email-gitster@pobox.com> (Junio C. Hamano's message of "Fri, 17 Jul 2015 15:24:30 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> +/*
> + * Write a conflict marker to io->output (if defined).
> + */
>  static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
>  {
>  	char buf[64];

This is unrelated to the topic of this step, but this function seems
to be very poorly put together with duct tape.

> static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
> {
> 	char buf[64];
> 
> 	while (size) {
> 		if (size < sizeof(buf) - 2) {
> 			memset(buf, ch, size);
> 			buf[size] = '\n';
> 			buf[size + 1] = '\0';
> 			size = 0;

The if() guarding this block has an off-by-one error in the benign
direction.  When size is 62, the marker with terminating LF and NUL
should still fit within the buf[], so it should use "<=", not "<",
to compare.

> 		} else {
> 			int sz = sizeof(buf) - 1;
> 			if (size <= sz)
> 				sz -= (sz - size) + 1;
> 			memset(buf, ch, sz);
> 			buf[sz] = '\0';
> 			size -= sz;

This is an even more awkward construct.  sz is what we have to work
with the substring that we cannot emit with a single call (because
buf[] is too small), so naturally I would expect it to be more like

	int to_emit = size;
        if (sz <= size)
        	to_emit = sz;
	memset(buf, ch, to_emit);
        buf[to_emit] = '\0';
	size -= to_emit;

which makes the "if (size <= sz)" in the code look very suspicious.

But rewriting it to the "more natural" version would make it buggy.
At a right boundary condition, i.e. size == 63, we may find that the
conflict marker is too long with LF and NUL to fit in buf[] and come
here, and then fill the full conflict marker with NUL just fine in
buf[], decrementing size to 0, emit that 63-byte long marker.  The
next iteration will exit the loop without emitting the LF.

The unnatural is what is saving us from such a bug.

	if (size <= sz)
		sz -= (sz - size) + 1;

It ensures that subtraction of sz (i.e. "to_emit") from size before
the next iteration will never brings size down to zero by reducing
sz by one too much, forcing another iteration, which will then have
size smaller than "sizeof(buf) - 2" and have us emit the LF.

Not buggy, but ugly.

> 		}
> 		rerere_io_putstr(buf, io);
> 	}
> }

  reply	other threads:[~2015-07-24 20:42 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01  6:04 [PATCH v2 00/13] "rerere" minor clean-up Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 01/13] rerere: fix an off-by-one non-bug Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 02/13] rerere: plug conflict ID leaks Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 03/13] rerere: lift PATH_MAX limitation Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 04/13] rerere: write out each record of MERGE_RR in one go Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 05/13] rerere: report autoupdated paths only after actually updating them Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 06/13] rerere: drop want_sp parameter from is_cmarker() Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 07/13] rerere: stop looping unnecessarily Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 08/13] rerere: explain the rerere I/O abstraction Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 09/13] rerere: explain MERGE_RR management helpers Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 10/13] rerere: explain the primary codepath Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 11/13] rerere: explain "rerere forget" codepath Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 12/13] rerere: explain the remainder Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 13/13] rerere: refactor "replay" part of do_plain_rerere() Junio C Hamano
2015-07-17 22:24 ` [PATCH v3 00/18] "rerere" preparatory clean-up Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 01/18] rerere: fix an off-by-one non-bug Junio C Hamano
2015-07-24 19:46     ` Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 02/18] rerere: plug conflict ID leaks Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 03/18] rerere: lift PATH_MAX limitation Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 04/18] rerere: write out each record of MERGE_RR in one go Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 05/18] rerere: report autoupdated paths only after actually updating them Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 06/18] rerere: drop want_sp parameter from is_cmarker() Junio C Hamano
2015-07-18  8:24     ` Philip Oakley
2015-07-18  8:47       ` Eric Sunshine
2015-07-17 22:24   ` [PATCH v3 07/18] rerere: stop looping unnecessarily Junio C Hamano
2015-07-24 20:06     ` Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 08/18] rerere: explain the rerere I/O abstraction Junio C Hamano
2015-07-24 20:42     ` Junio C Hamano [this message]
2015-07-17 22:24   ` [PATCH v3 09/18] rerere: explain MERGE_RR management helpers Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 10/18] rerere: explain the primary codepath Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 11/18] rerere: explain "rerere forget" codepath Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 12/18] rerere: explain the remainder Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 13/18] rerere: refactor "replay" part of do_plain_rerere() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 14/18] rerere: further de-dent do_plain_rerere() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 15/18] rerere: further clarify do_rerere_one_path() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 16/18] rerere: call conflict-ids IDs Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 17/18] rerere: use "struct rerere_id" instead of "char *" for conflict ID Junio C Hamano
2015-07-18  8:47     ` Eric Sunshine
2015-07-17 22:24   ` [PATCH v3 18/18] rerere: un-nest merge() further 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=xmqqio99l22p.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --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.