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: Linus Torvalds <torvalds@linux-foundation.org>, git@vger.kernel.org
Subject: Re: trim_common_tail bug?
Date: Sun, 16 Dec 2007 02:06:14 -0500	[thread overview]
Message-ID: <20071216070614.GA5072@sigill.intra.peff.net> (raw)
In-Reply-To: <20071215200202.GA3334@sigill.intra.peff.net>

On Sat, Dec 15, 2007 at 03:02:02PM -0500, Jeff King wrote:

> >   	for (i = 0, recovered = 0; recovered < trimmed && i <= ctx; i++) {
> > 		while (recovered < trimmed && ap[recovered++] != '\n')
> > 	        	; /* nothing */
> > 	}
> > 
> > then (warning: I haven't had my coffee yet)?
> 
> Yes, I think that is equivalent. My sleep-deprived brain keeps thinking
> there must be a more clear way of writing this whole loop, but it
> escapes me at the moment.

And this came to me in a dream. :) It fixes the bug, and I think it is a
bit simpler to see the termination conditions in a single loop. But
please double-check correctness, and that you agree that it is more
readable.

---
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 700def2..98b02ed 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -110,7 +110,7 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
 {
 	const int blk = 1024;
-	long trimmed = 0, recovered = 0, i;
+	long trimmed = 0, recovered = 0;
 	char *ap = a->ptr + a->size;
 	char *bp = b->ptr + b->size;
 	long smaller = (a->size < b->size) ? a->size : b->size;
@@ -121,10 +121,9 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
 		bp -= blk;
 	}
 
-	for (i = 0, recovered = 0; recovered < trimmed && i <= ctx; i++) {
-		while (recovered < trimmed && ap[recovered] != '\n')
-			recovered++;
-	}
+	while (recovered < trimmed && ctx)
+		if (ap[recovered++] == '\n')
+			ctx--;
 	a->size -= (trimmed - recovered);
 	b->size -= (trimmed - recovered);
 }

  reply	other threads:[~2007-12-16  7:06 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-15 11:16 trim_common_tail bug? Jeff King
2007-12-15 15:51 ` Jeff King
2007-12-15 17:49   ` Junio C Hamano
2007-12-15 20:02     ` Jeff King
2007-12-16  7:06       ` Jeff King [this message]
2007-12-16 19:43         ` Junio C Hamano
2007-12-16 21:16           ` Junio C Hamano
2007-12-16 21:21             ` Jeff King
2007-12-16 21:49               ` [PATCH] Re-re-re-fix common tail optimization Junio C Hamano
2007-12-16 22:15                 ` Jeff King
2007-12-16 22:23                   ` Junio C Hamano
2007-12-16 22:28                     ` Junio C Hamano
2007-12-16 22:29                     ` Jeff King
2007-12-17  8:42                       ` Wincent Colaiuta
2007-12-17 10:39                         ` Johannes Schindelin
2007-12-17 10:59                           ` Wincent Colaiuta
2007-12-17 11:57                             ` Johannes Schindelin
2007-12-17 12:08                               ` Wincent Colaiuta
2007-12-17 12:12                                 ` Jeff King
2007-12-17 12:20                                 ` Johannes Sixt
2007-12-17 12:51                                   ` Johannes Schindelin
2007-12-17 17:58                                     ` Junio C Hamano
2007-12-17 18:05                                       ` Johannes Schindelin
2007-12-19 14:18                 ` Charles Bailey
2007-12-19 14:27                   ` Jeff King
2007-12-19 14:37                     ` Charles Bailey
2007-12-20  0:21                       ` Junio C Hamano
2007-12-20  1:38                         ` Wincent Colaiuta
2007-12-20  9:23                         ` Charles Bailey
2007-12-20  9:40                           ` 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=20071216070614.GA5072@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=torvalds@linux-foundation.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 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).