bug-gnu-utils
[Top][All Lists]
Advanced

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

Patch for GNU Patch: wrong line numbers reported for offsetted patches


From: ITO Tsuyoshi
Subject: Patch for GNU Patch: wrong line numbers reported for offsetted patches
Date: Fri, 12 Sep 2003 00:27:07 +0900 (JST)

(I am not on this list.  Please Cc to me if necessary.)

Hi,

GNU Patch 2.5.4 sometimes reports wrong line numbers when the line
numbers in the patch and the text are different (in other words, when
the patch has a non-zero offset).

Here is an example.  The files used in it are attached to this mail.

| $ gpatch --verbose < patch
| Hmm...  Looks like a unified diff to me...
| The text leading up to this was:
| --------------------------
| |--- text.old Thu Sep 11 23:26:25 2003
| |+++ text     Thu Sep 11 23:26:58 2003
| --------------------------
| Patching file text using Plan A...
| Hunk #1 succeeded at 1 (offset -30 lines).
| Hunk #2 succeeded at 39.
| Hunk #3 succeeded at 17 (offset -30 lines).
| Hunk #4 succeeded at 55.
| Hunk #5 succeeded at 33 (offset -30 lines).
| done

Note that the line numbers at which Hunks #2 and #4 are applied are
reported incorrectly.  (How can you apply a patch to a text at line
55, while the text is only 39 lines long and the patch only modifies
some lines without adding or removing any lines?)  The resulting
patched text is correct, that is, just what you might expect from the
original text and the patch.

A patch against GNU Patch 2.5.4 to correct this behavior is attached
(patch.c.patch).  Here is the output after patch.c.patch is applied.
There is no surprise this time.

| $ gpatch --verbose < patch
| Hmm...  Looks like a unified diff to me...
| The text leading up to this was:
| --------------------------
| |--- text.old Thu Sep 11 23:26:25 2003
| |+++ text     Thu Sep 11 23:26:58 2003
| --------------------------
| Patching file text using Plan A...
| Hunk #1 succeeded at 1 (offset -30 lines).
| Hunk #2 succeeded at 9 (offset -30 lines).
| Hunk #3 succeeded at 17 (offset -30 lines).
| Hunk #4 succeeded at 25 (offset -30 lines).
| Hunk #5 succeeded at 33 (offset -30 lines).
| done

I admit that in some rare cases, the Patch with patch.c.patch applied
gives a different result apart from the line numbers it shows.
(Technical note: this is because patch.c.patch changes the value of
the variable last_offset.)  However, in those rare cases, the
"correct" result is ambiguous from the beginning IMHO, and I guess
different Patch implementations give different results in such cases.
Besides, I think the behavior after patch.c.patch is applied is a
little more logical.

By the way, I am not sure if GNU Patch 2.5.4 (last modified in 1999)
is the newest version.  If this version is obsolete, please tell me
where to obtain the newest version.

Best regards,
Tsuyoshi

---   ITO Tsuyoshi  <address@hidden>   ---
--- Dept. of Computer Science, University of Tokyo. ---
--- patch.c.orig        Mon Aug 30 15:20:08 1999
+++ patch.c     Thu Sep 11 23:08:59 2003
@@ -913,8 +913,8 @@
            if (debug & 1)
              say ("Offset changing from %s to %s\n",
                   format_linenum (numbuf0, last_offset),
-                  format_linenum (numbuf1, offset));
-           last_offset = offset;
+                  format_linenum (numbuf1, last_offset + offset));
+           last_offset += offset;
            return first_guess+offset;
        }
        if (0 < offset && offset <= max_neg_offset
@@ -922,8 +922,8 @@
            if (debug & 1)
              say ("Offset changing from %s to %s\n",
                   format_linenum (numbuf0, last_offset),
-                  format_linenum (numbuf1, -offset));
-           last_offset = -offset;
+                  format_linenum (numbuf1, last_offset - offset));
+           last_offset -= offset;
            return first_guess-offset;
        }
     }
Hunk #1 starts here 1
Hunk #1 starts here 2
Hunk #1 starts here 3
This line in Hunk #1 is old
Hunk #1 ends here 1
Hunk #1 ends here 2
Hunk #1 ends here 3
This line between Hunks #1 and #2 is not related to the patch
Hunk #2 starts here 1
Hunk #2 starts here 2
Hunk #2 starts here 3
This line in Hunk #2 is old
Hunk #2 ends here 1
Hunk #2 ends here 2
Hunk #2 ends here 3
This line between Hunks #2 and #3 is not related to the patch
Hunk #3 starts here 1
Hunk #3 starts here 2
Hunk #3 starts here 3
This line in Hunk #3 is old
Hunk #3 ends here 1
Hunk #3 ends here 2
Hunk #3 ends here 3
This line between Hunks #3 and #4 is not related to the patch
Hunk #4 starts here 1
Hunk #4 starts here 2
Hunk #4 starts here 3
This line in Hunk #4 is old
Hunk #4 ends here 1
Hunk #4 ends here 2
Hunk #4 ends here 3
This line between Hunks #4 and #5 is not related to the patch
Hunk #5 starts here 1
Hunk #5 starts here 2
Hunk #5 starts here 3
This line in Hunk #5 is old
Hunk #5 ends here 1
Hunk #5 ends here 2
Hunk #5 ends here 3
--- text.old    Thu Sep 11 23:26:25 2003
+++ text        Thu Sep 11 23:26:58 2003
@@ -31,7 +31,7 @@
 Hunk #1 starts here 1
 Hunk #1 starts here 2
 Hunk #1 starts here 3
-This line in Hunk #1 is old
+This line in Hunk #1 is new
 Hunk #1 ends here 1
 Hunk #1 ends here 2
 Hunk #1 ends here 3
@@ -39,7 +39,7 @@
 Hunk #2 starts here 1
 Hunk #2 starts here 2
 Hunk #2 starts here 3
-This line in Hunk #2 is old
+This line in Hunk #2 is new
 Hunk #2 ends here 1
 Hunk #2 ends here 2
 Hunk #2 ends here 3
@@ -47,7 +47,7 @@
 Hunk #3 starts here 1
 Hunk #3 starts here 2
 Hunk #3 starts here 3
-This line in Hunk #3 is old
+This line in Hunk #3 is new
 Hunk #3 ends here 1
 Hunk #3 ends here 2
 Hunk #3 ends here 3
@@ -55,7 +55,7 @@
 Hunk #4 starts here 1
 Hunk #4 starts here 2
 Hunk #4 starts here 3
-This line in Hunk #4 is old
+This line in Hunk #4 is new
 Hunk #4 ends here 1
 Hunk #4 ends here 2
 Hunk #4 ends here 3
@@ -63,7 +63,7 @@
 Hunk #5 starts here 1
 Hunk #5 starts here 2
 Hunk #5 starts here 3
-This line in Hunk #5 is old
+This line in Hunk #5 is new
 Hunk #5 ends here 1
 Hunk #5 ends here 2
 Hunk #5 ends here 3

reply via email to

[Prev in Thread] Current Thread [Next in Thread]