Git development
 help / color / mirror / Atom feed
* [PATCH] git-diff: fix combined diff
@ 2007-02-23  4:20 Johannes Schindelin
  2007-02-23  5:22 ` Junio C Hamano
  2007-02-23  5:34 ` Jeff King
  0 siblings, 2 replies; 5+ messages in thread
From: Johannes Schindelin @ 2007-02-23  4:20 UTC (permalink / raw)
  To: git, junkio


With "const unsigned (*parent)[20]", "parent + 1" is not the
same as "&parent[1]"...

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---

	I triggered this bug by "git diff a...b", where a and b
	have 4 merge bases. I am really too tired to add a test
	case, though...

 builtin-diff.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin-diff.c b/builtin-diff.c
index 9334589..846cafb 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -158,7 +158,8 @@ static int builtin_diff_combined(struct rev_info *revs,
 	parent = xmalloc(ents * sizeof(*parent));
 	/* Again, the revs are all reverse */
 	for (i = 0; i < ents; i++)
-		hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);
+		hashcpy((unsigned char*)&parent[i],
+				ent[ents - 1 - i].item->sha1);
 	diff_tree_combined(parent[0], parent + 1, ents - 1,
 			   revs->dense_combined_merges, revs);
 	return 0;
-- 
1.5.0.1.2218.g2de79

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] git-diff: fix combined diff
  2007-02-23  4:20 [PATCH] git-diff: fix combined diff Johannes Schindelin
@ 2007-02-23  5:22 ` Junio C Hamano
  2007-02-23  6:09   ` Junio C Hamano
  2007-02-23  5:34 ` Jeff King
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2007-02-23  5:22 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git

With this,

	$ git diff maint master next pu

starts working as planned ;-).

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] git-diff: fix combined diff
  2007-02-23  4:20 [PATCH] git-diff: fix combined diff Johannes Schindelin
  2007-02-23  5:22 ` Junio C Hamano
@ 2007-02-23  5:34 ` Jeff King
  2007-02-23 11:19   ` Johannes Schindelin
  1 sibling, 1 reply; 5+ messages in thread
From: Jeff King @ 2007-02-23  5:34 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, junkio

On Fri, Feb 23, 2007 at 05:20:32AM +0100, Johannes Schindelin wrote:

> With "const unsigned (*parent)[20]", "parent + 1" is not the
> same as "&parent[1]"...

Actually, they _are_ the same (the C standard definition of A[B] is *(A+B)).
The problem is the operator precedence of the cast:

> -		hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);

which translates to "cast parent to an unsigned char pointer, and then
add i * sizeof(unsigned char) to it".

Your fix works because [] binds more tightly, fixing the precedence
problem. You could also do this:
  hashcpy((unsigned char*)(parent + i), ...

-Peff

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] git-diff: fix combined diff
  2007-02-23  5:22 ` Junio C Hamano
@ 2007-02-23  6:09   ` Junio C Hamano
  0 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2007-02-23  6:09 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git

Junio C Hamano <junkio@cox.net> writes:

> With this,
>
> 	$ git diff maint master next pu
>
> starts working as planned ;-).

By the way, your commit log message does not seem to say
why it is a fix correctly.  Here is what I replaced it with.

-- >8 -- cut here -- >8 --
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Date: Fri, 23 Feb 2007 05:20:32 +0100
Subject: [PATCH] git-diff: fix combined diff

The code forgets that typecast binds tighter than addition, in
other words:

    (cast *)array + i  === ((cast *)array) + i

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 builtin-diff.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin-diff.c b/builtin-diff.c
index a659020..c387ebb 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -192,7 +192,8 @@ static int builtin_diff_combined(struct rev_info *revs,
 	parent = xmalloc(ents * sizeof(*parent));
 	/* Again, the revs are all reverse */
 	for (i = 0; i < ents; i++)
-		hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);
+		hashcpy((unsigned char *)(parent + i),
+			ent[ents - 1 - i].item->sha1);
 	diff_tree_combined(parent[0], parent + 1, ents - 1,
 			   revs->dense_combined_merges, revs);
 	return 0;
-- 
1.5.0.1.619.g04c5c

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] git-diff: fix combined diff
  2007-02-23  5:34 ` Jeff King
@ 2007-02-23 11:19   ` Johannes Schindelin
  0 siblings, 0 replies; 5+ messages in thread
From: Johannes Schindelin @ 2007-02-23 11:19 UTC (permalink / raw)
  To: Jeff King; +Cc: git, junkio

Hi,

On Fri, 23 Feb 2007, Jeff King wrote:

> On Fri, Feb 23, 2007 at 05:20:32AM +0100, Johannes Schindelin wrote:
> 
> > With "const unsigned (*parent)[20]", "parent + 1" is not the
> > same as "&parent[1]"...
> 
> Actually, they _are_ the same (the C standard definition of A[B] is *(A+B)).

Modulo binding precedence.

Thanks for clearing this up,
Dscho

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-02-23 11:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-23  4:20 [PATCH] git-diff: fix combined diff Johannes Schindelin
2007-02-23  5:22 ` Junio C Hamano
2007-02-23  6:09   ` Junio C Hamano
2007-02-23  5:34 ` Jeff King
2007-02-23 11:19   ` Johannes Schindelin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox