[PATCH] Fix yet another subtle xdl_merge() bug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In very obscure cases, a merge can hit an unexpected code path (where the 
original code went as far as saying that this was a bug). This failing 
merge was noticed by Alexandre Juillard.

The problem is that the original file contains something like this:

-- snip --
two non-empty lines
before two empty lines


after two empty lines
-- snap --

and this snippet is reduced to _one_ empty line in _both_ new files. 
However, it is ambiguous as to which hunk takes the empty line: the first 
or the second one?

Indeed in Alexandre's example files, the xdiff algorithm attributes the 
empty line to the first hunk in one case, and to the second hunk in the 
other case.

(Trimming down the example files _changes_ that behaviour!)

Thus, the call to xdl_merge_cmp_lines() has no chance to realize that the 
change is actually identical in both new files. Therefore, 
xdl_refine_conflicts() finds an empty diff script, which was not expected 
there, because (the original author of xdl_merge() thought) 
xdl_merge_cmp_lines() would catch that case earlier.

Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx>
---

	Alexandre sent me this set of files where xdl_merge() returns 
	-1 instead of performing as expected. With his permission, I 
	attached them to this mail.

	On Thu, 28 Dec 2006, Alexandre Julliard wrote:
	
	> I'm still unable to complete the merge though, it dies a bit 
	> later with:
	> 
	> ...
	> Auto-merging dlls/ntdll/virtual.c
	> fatal: Failed to execute internal merge
	> Merge with strategy recursive failed.
	> 
	> Apparently xdl_merge() returns -1 for that one. I've attached 
	> the corresponding files.

	I tried for about 20 hours total to concoct a short test case for 
	this, still failing. I give up.

	Trying to find out what happens led me to an interesting hunt for 
	a bug which is not there. The code is even more elegant than I 
	thought: it took me 10 hours of staring into my laptop, scribbling 
	on my notepad and trying several examples, to find out why it 
	works.

	The code in question is where two conflicts overlap, i.e. the 
	original lines affected by both diff scripts overlap. The 
	interesting case is when the overlapping regions are 
	not identical.

	xdl_do_merge() enlarges the smaller region, and adds the same 
	amount of lines _to the corresponding region in the new file_.

	This seems wrong, as there can be modifications in that region, 
	too! Why does the code perform correctly, then?

	It is a feature/obscure behaviour of xdl_append_merge() that it 
	tries to combine conflicts. And if said problem occurs, it _will_ 
	modify the last conflict, but _only_ the end of the regions! Thus, 
	it does not matter if the first line passed to xdl_append_merge() 
	is actually wrong, as long as it is guaranteed to overlap the old 
	conflict (which is the case: if it would not overlap, there would 
	be no modification in that part of the original file!).

	The same holds for the last line: if it is wrong, it will get 
	fixed in a subsequent call to xdl_append_merge().

	If this sounds confused, it is because I thought too long and hard 
	about it, _and_ I just had a lovely poker night (my first one!). 
	But please, if you want to understand it, but my explanation is 
	not satisfactory, you may bug me for a better description, and I 
	will do it. Though it has to wait until 2nd of January.

	Happy new year!

 xdiff/xmerge.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c
index 294450b..b83b334 100644
--- a/xdiff/xmerge.c
+++ b/xdiff/xmerge.c
@@ -166,6 +166,8 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
 			size += xdl_recs_copy(xe2, m->i2 - m->i1 + i1,
 					m->i1 + m->chg2 - i1, 0,
 					dest ? dest + size : NULL);
+		else
+			continue;
 		i1 = m->i1 + m->chg1;
 	}
 	size += xdl_recs_copy(xe1, i1, xe1->xdf2.nrec - i1, 0,
@@ -213,9 +215,10 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
 			return -1;
 		}
 		if (!xscr) {
-			/* If this happens, it's a bug. */
+			/* If this happens, the changes are identical. */
 			xdl_free_env(&xe);
-			return -2;
+			m->mode = 4;
+			continue;
 		}
 		x = xscr;
 		m->i1 = xscr->i1 + i1;

Attachment: merge-file-error.tar.gz
Description: application/gunzip


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]