Re: trim_common_tail bug?
- From
Jeff King <peff@peff.net>
- Date
- Dec 16, 2007, 07:06 UTC
- Message-ID
- <20071216070614.GA5072@sigill.intra.peff.net>
- In-Reply-To
- <20071215200202.GA3334@sigill.intra.peff.net>
On Sat, Dec 15, 2007 at 03:02:02PM -0500, Jeff King wrote:
Show 10 quoted lines
> > 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); }