Package: dpkg-dev
Followup-For: Bug #1099170
X-Debbugs-Cc: tj.iam...@proton.me

Apologies that the previous attached patch did NOT include the changes I
described. This one definitely does include dealing with partial hunk
preambles correctly.
>From e00ca8faafc3fbbbc125be94cdfc31a3b1cc5243 Mon Sep 17 00:00:00 2001
From: Tj <tj.iam...@proton.me>
Date: Sun, 2 Mar 2025 09:19:40 +0000
Subject: [PATCH] Source/Patch: fix parsing of patch header

analyze() failed to correctly parse a patch header that has a line that
matches two of the three hunk detection regular expressions because it
treats them in isolation rather than as a linked, ordered, series.

josch in IRC's #debian-mentors reported this error:

dpkg-source: error: expected ^--- in line 7 of diff 
'mesa-24.3.4.orig.JVu23i/debian/patches/mesa25/2079-radeonsi-fix-a-TCS-regression.patch'

The cause being the isolated "@@ -" in the patch header that was directly
taken from git-format-patch (here prefixed with "> " to avoid causing
the same error!):

> From a0579f75fb5aa6926f4acfdee3fa91f2666df559 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= <marek.ol...@amd.com>
> Date: Tue, 24 Dec 2024 15:00:39 -0500
> Subject: [PATCH 2079/3849] radeonsi: fix a TCS regression
>
> This change caused the regression:
> @@ -853,7 +853,7 @@ bool si_llvm_compile_shader(struct si_screen *sscreen, 
> struct ac_llvm_compiler *
...
>
> Reviewed-by: Qiang Yu <yuq...@gmail.com>
> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32780>
> ---
>  src/gallium/drivers/radeonsi/si_shader_llvm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/src/gallium/drivers/radeonsi/si_shader_llvm.c 
> b/src/gallium/drivers/radeonsi/si_shader_llvm.c
> index 620953e817f..bd0309744de 100644
> --- a/src/gallium/drivers/radeonsi/si_shader_llvm.c
> +++ b/src/gallium/drivers/radeonsi/si_shader_llvm.c
> @@ -840,12 +840,12 @@ bool si_llvm_compile_shader(struct si_screen *sscreen, 
> struct ac_llvm_compiler *
>        struct si_shader prev_shader = {};

Revise the parsing logic to treat the three hunk markers as ordered and
linked.

Signed-off-by: Tj <tj.iam...@proton.me>
---
 scripts/Dpkg/Source/Patch.pm | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/scripts/Dpkg/Source/Patch.pm b/scripts/Dpkg/Source/Patch.pm
index 57468fc4e..16a486589 100644
--- a/scripts/Dpkg/Source/Patch.pm
+++ b/scripts/Dpkg/Source/Patch.pm
@@ -422,6 +422,10 @@ sub analyze {
     my @patchorder;
     my $patch_header = '';
     my $diff_count = 0;
+    my @patchprefix = ( '^--- ', '^\+\+\+ ', '^@@ -' );
+    my $prefixindex = 0;
+    my $offset;
+    my $preamble = '';
 
     my $line = _getline($self);
 
@@ -433,11 +437,25 @@ sub analyze {
        # look for an Index: pseudo-header in the comments, because we would
        # not use it anyway, as we require both ---/+++ filename headers.
        while (1) {
-           if ($line =~ /^(?:--- |\+\+\+ |@@ -)/) {
-               last;
+           if ($line =~ $patchprefix[$prefixindex]) {
+               $prefixindex++;
+               $preamble .= "$line\n";
            } else {
+               $prefixindex = 0;
+               $patch_header .= "$preamble";
+               $preamble = '';
+           }
+           if ($prefixindex == 3) {
+               $prefixindex = 0;
+               seek($self, $offset, 0);
+               $line = _getline($self);
+               last;
+           } elsif ($prefixindex == 0) {
                $patch_header .= "$line\n";
            }
+           if ($prefixindex == 0) {
+               $offset = tell($self);
+           }
            $line = _getline($self);
            last HUNK if not defined $line;
        }
-- 
2.39.5

Reply via email to