# [PATCH 0/1] Avoid segmentation fault in git range-diff when diff.noprefix=true

11 messages from 2019-10-02 to 2019-10-03. Participants: Johannes Schindelin via GitGitGadget, Eric Sunshine, Michal Suchánek, Junio C Hamano, Johannes Schindelin.
Thread: https://gitlist.dev/t/51958

## Johannes Schindelin via GitGitGadget, 2019-10-02 18:05

Subject: [PATCH 0/1] Avoid segmentation fault in git range-diff when diff.noprefix=true
Message-ID: <pull.373.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.373.git.gitgitgadget%40gmail.com

```
This PR was inspired by this bug report: 
https://public-inbox.org/git/20191002141615.GB17916@kitsune.suse.cz/T/#me576615d7a151cf2ed46186c482fbd88f9959914

Johannes Schindelin (1):
  range-diff: internally force `diff.noprefix=false`

 range-diff.c          | 3 ++-
 t/t3206-range-diff.sh | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)


base-commit: bc12974a897308fd3254cf0cc90319078fe45eea
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-373%2Fdscho%2Frange-diff-requires-diff-prefix-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-373/dscho/range-diff-requires-diff-prefix-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/373
-- 
gitgitgadget

```

## Johannes Schindelin via GitGitGadget, 2019-10-02 18:05

Subject: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <1f84f92846bc14d21aa7339c8baa0f9bb710b17d.1570039511.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/1f84f92846bc14d21aa7339c8baa0f9bb710b17d.1570039511.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.373.git.gitgitgadget@gmail.com>

```
From: Johannes Schindelin <johannes.schindelin@gmx.de>

When parsing the diffs, `range-diff` expects to see the prefixes `a/`
and `b/` in the diff headers.

These prefixes can be forced off via the config setting
`diff.noprefix=true`. As `range-diff` is not prepared for that
situation, this will cause a segmentation fault.

Let's avoid that by forcing `diff.noprefix=false` just for that all to
`git log` that generates the diffs that `range-diff` wants to parse.

Noticed-by: Michal Suchánek <msuchanek@suse.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 range-diff.c          | 3 ++-
 t/t3206-range-diff.sh | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/range-diff.c b/range-diff.c
index ba1e9a4265..8cc348b4cb 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -50,7 +50,8 @@ static int read_patches(const char *range, struct string_list *list)
 	int offset, len;
 	size_t size;
 
-	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
+	argv_array_pushl(&cp.args, "-c", "diff.noprefix=false",
+			"log", "--no-color", "-p", "--no-merges",
 			"--reverse", "--date-order", "--decorate=no",
 			/*
 			 * Choose indicators that are not used anywhere
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 0120f769f1..64b66f2094 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -461,4 +461,8 @@ test_expect_success 'format-patch --range-diff as commentary' '
 	grep "> 1: .* new message" 0001-*
 '
 
+test_expect_success 'range-diff overrides diff.noprefix internally' '
+	git -c diff.noprefix=true range-diff HEAD^...
+'
+
 test_done
-- 
gitgitgadget

```

## Eric Sunshine, 2019-10-02 18:34

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <CAPig+cS1XHLC3Q_Ntq=-Ck8WzRR7FL1qwQuCfUPVFSTkEKK+LQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcS1XHLC3Q_Ntq%3D-Ck8WzRR7FL1qwQuCfUPVFSTkEKK%2BLQ%40mail.gmail.com
In-Reply-To: <1f84f92846bc14d21aa7339c8baa0f9bb710b17d.1570039511.git.gitgitgadget@gmail.com>

```
On Wed, Oct 2, 2019 at 2:05 PM Johannes Schindelin via GitGitGadget
<gitgitgadget@gmail.com> wrote:
> When parsing the diffs, `range-diff` expects to see the prefixes `a/`
> and `b/` in the diff headers.
>
> These prefixes can be forced off via the config setting
> `diff.noprefix=true`. As `range-diff` is not prepared for that
> situation, this will cause a segmentation fault.
>
> Let's avoid that by forcing `diff.noprefix=false` just for that all to

s/all/call/

> `git log` that generates the diffs that `range-diff` wants to parse.
>
> Noticed-by: Michal Suchánek <msuchanek@suse.de>

s/Noticed/Reported/ perhaps?

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

```

## Michal Suchánek, 2019-10-02 19:56

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <20191002195646.GF17916@kitsune.suse.cz>
URL: https://gitlist.dev/e/20191002195646.GF17916%40kitsune.suse.cz
In-Reply-To: <1f84f92846bc14d21aa7339c8baa0f9bb710b17d.1570039511.git.gitgitgadget@gmail.com>

```
On Wed, Oct 02, 2019 at 11:05:13AM -0700, Johannes Schindelin via GitGitGadget wrote:
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> When parsing the diffs, `range-diff` expects to see the prefixes `a/`
> and `b/` in the diff headers.
> 
> These prefixes can be forced off via the config setting
> `diff.noprefix=true`. As `range-diff` is not prepared for that
> situation, this will cause a segmentation fault.
> 
> Let's avoid that by forcing `diff.noprefix=false` just for that all to
> `git log` that generates the diffs that `range-diff` wants to parse.
> 
> Noticed-by: Michal Suchánek <msuchanek@suse.de>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Tested-by: Michal Suchánek <msuchanek@suse.de>

Thanks

Michal
> ---
>  range-diff.c          | 3 ++-
>  t/t3206-range-diff.sh | 4 ++++
>  2 files changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/range-diff.c b/range-diff.c
> index ba1e9a4265..8cc348b4cb 100644
> --- a/range-diff.c
> +++ b/range-diff.c
> @@ -50,7 +50,8 @@ static int read_patches(const char *range, struct string_list *list)
>  	int offset, len;
>  	size_t size;
>  
> -	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
> +	argv_array_pushl(&cp.args, "-c", "diff.noprefix=false",
> +			"log", "--no-color", "-p", "--no-merges",
>  			"--reverse", "--date-order", "--decorate=no",
>  			/*
>  			 * Choose indicators that are not used anywhere
> diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> index 0120f769f1..64b66f2094 100755
> --- a/t/t3206-range-diff.sh
> +++ b/t/t3206-range-diff.sh
> @@ -461,4 +461,8 @@ test_expect_success 'format-patch --range-diff as commentary' '
>  	grep "> 1: .* new message" 0001-*
>  '
>  
> +test_expect_success 'range-diff overrides diff.noprefix internally' '
> +	git -c diff.noprefix=true range-diff HEAD^...
> +'
> +
>  test_done
> -- 
> gitgitgadget

```

## Junio C Hamano, 2019-10-02 20:06

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <xmqq4l0qewu5.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq4l0qewu5.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <1f84f92846bc14d21aa7339c8baa0f9bb710b17d.1570039511.git.gitgitgadget@gmail.com>

```
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> When parsing the diffs, `range-diff` expects to see the prefixes `a/`
> and `b/` in the diff headers.

If so, passing src/dst prefix as command line option is a much
better solution, I think.  diff.noprefix may not stay to be (or it
may already not to be) the only thing how the prefix gets chosen.

> -	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
> +	argv_array_pushl(&cp.args, "-c", "diff.noprefix=false",
> +			"log", "--no-color", "-p", "--no-merges",
>  			"--reverse", "--date-order", "--decorate=no",
>  			/*
>  			 * Choose indicators that are not used anywhere
> diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> index 0120f769f1..64b66f2094 100755
> --- a/t/t3206-range-diff.sh
> +++ b/t/t3206-range-diff.sh
> @@ -461,4 +461,8 @@ test_expect_success 'format-patch --range-diff as commentary' '
>  	grep "> 1: .* new message" 0001-*
>  '
>  
> +test_expect_success 'range-diff overrides diff.noprefix internally' '
> +	git -c diff.noprefix=true range-diff HEAD^...
> +'
> +
>  test_done

```

## Johannes Schindelin, 2019-10-02 20:30

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <nycvar.QRO.7.76.6.1910022229340.46@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1910022229340.46%40tvgsbejvaqbjf.bet
In-Reply-To: <xmqq4l0qewu5.fsf@gitster-ct.c.googlers.com>

```
Hi Junio,

On Thu, 3 Oct 2019, Junio C Hamano wrote:

> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > When parsing the diffs, `range-diff` expects to see the prefixes `a/`
> > and `b/` in the diff headers.
>
> If so, passing src/dst prefix as command line option is a much better
> solution, I think.  diff.noprefix may not stay to be (or it may
> already not to be) the only thing how the prefix gets chosen.

Good point.

While at it, I invert the logic in v2: instead of forcing a prefix, I
now force no prefix (and reduce the strip level from 1 to 0 when parsing
the diff header).

Thanks,
Dscho

>
> > -	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
> > +	argv_array_pushl(&cp.args, "-c", "diff.noprefix=false",
> > +			"log", "--no-color", "-p", "--no-merges",
> >  			"--reverse", "--date-order", "--decorate=no",
> >  			/*
> >  			 * Choose indicators that are not used anywhere
> > diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> > index 0120f769f1..64b66f2094 100755
> > --- a/t/t3206-range-diff.sh
> > +++ b/t/t3206-range-diff.sh
> > @@ -461,4 +461,8 @@ test_expect_success 'format-patch --range-diff as commentary' '
> >  	grep "> 1: .* new message" 0001-*
> >  '
> >
> > +test_expect_success 'range-diff overrides diff.noprefix internally' '
> > +	git -c diff.noprefix=true range-diff HEAD^...
> > +'
> > +
> >  test_done
>

```

## Johannes Schindelin via GitGitGadget, 2019-10-02 21:10

Subject: [PATCH v2 0/1] Avoid segmentation fault in git range-diff when diff.noprefix=true
Message-ID: <pull.373.v2.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.373.v2.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.373.git.gitgitgadget@gmail.com>

```
This PR was inspired by this bug report: 
https://public-inbox.org/git/20191002141615.GB17916@kitsune.suse.cz/T/#me576615d7a151cf2ed46186c482fbd88f9959914

Changes since v1:

 * Use a command-line option instead of a command-line config setting.
 * Instead of forcing a prefix, force no prefix (and adjust the strip
   level).
 * Fix typo "all" -> "call" in the commit message (and adjust it to reflect
   the other changes since v1, too).

Johannes Schindelin (1):
  range-diff: internally force `diff.noprefix=true`

 range-diff.c          | 3 ++-
 t/t3206-range-diff.sh | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)


base-commit: bc12974a897308fd3254cf0cc90319078fe45eea
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-373%2Fdscho%2Frange-diff-requires-diff-prefix-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-373/dscho/range-diff-requires-diff-prefix-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/373

Range-diff vs v1:

 1:  1f84f92846 ! 1:  9993a607be range-diff: internally force `diff.noprefix=false`
     @@ -1,6 +1,6 @@
      Author: Johannes Schindelin <johannes.schindelin@gmx.de>
      
     -    range-diff: internally force `diff.noprefix=false`
     +    range-diff: internally force `diff.noprefix=true`
      
          When parsing the diffs, `range-diff` expects to see the prefixes `a/`
          and `b/` in the diff headers.
     @@ -9,25 +9,33 @@
          `diff.noprefix=true`. As `range-diff` is not prepared for that
          situation, this will cause a segmentation fault.
      
     -    Let's avoid that by forcing `diff.noprefix=false` just for that all to
     -    `git log` that generates the diffs that `range-diff` wants to parse.
     +    Let's avoid that by passing the `--no-prefix` option to the `git log`
     +    process that generates the diffs that `range-diff` wants to parse.
     +    And of course expect the output to have no prefixes, then.
      
     -    Noticed-by: Michal Suchánek <msuchanek@suse.de>
     +    Reported-by: Michal Suchánek <msuchanek@suse.de>
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
       diff --git a/range-diff.c b/range-diff.c
       --- a/range-diff.c
       +++ b/range-diff.c
      @@
     - 	int offset, len;
     - 	size_t size;
       
     --	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
     -+	argv_array_pushl(&cp.args, "-c", "diff.noprefix=false",
     -+			"log", "--no-color", "-p", "--no-merges",
     + 	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
       			"--reverse", "--date-order", "--decorate=no",
     ++			"--no-prefix",
       			/*
       			 * Choose indicators that are not used anywhere
     + 			 * else in diffs, but still look reasonable
     +@@
     + 			if (!util->diff_offset)
     + 				util->diff_offset = buf.len;
     + 			line[len - 1] = '\n';
     +-			len = parse_git_diff_header(&root, &linenr, 1, line,
     ++			len = parse_git_diff_header(&root, &linenr, 0, line,
     + 						    len, size, &patch);
     + 			if (len < 0)
     + 				die(_("could not parse git header '%.*s'"), (int)len, line);
      
       diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
       --- a/t/t3206-range-diff.sh

-- 
gitgitgadget

```

## Johannes Schindelin via GitGitGadget, 2019-10-02 21:10

Subject: [PATCH v2 1/1] range-diff: internally force `diff.noprefix=true`
Message-ID: <9993a607beaafc66f72e1be3f158074cd121b4bb.1570050645.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/9993a607beaafc66f72e1be3f158074cd121b4bb.1570050645.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.373.v2.git.gitgitgadget@gmail.com>

```
From: Johannes Schindelin <johannes.schindelin@gmx.de>

When parsing the diffs, `range-diff` expects to see the prefixes `a/`
and `b/` in the diff headers.

These prefixes can be forced off via the config setting
`diff.noprefix=true`. As `range-diff` is not prepared for that
situation, this will cause a segmentation fault.

Let's avoid that by passing the `--no-prefix` option to the `git log`
process that generates the diffs that `range-diff` wants to parse.
And of course expect the output to have no prefixes, then.

Reported-by: Michal Suchánek <msuchanek@suse.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 range-diff.c          | 3 ++-
 t/t3206-range-diff.sh | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/range-diff.c b/range-diff.c
index ba1e9a4265..f2fc1e02c2 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -52,6 +52,7 @@ static int read_patches(const char *range, struct string_list *list)
 
 	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
 			"--reverse", "--date-order", "--decorate=no",
+			"--no-prefix",
 			/*
 			 * Choose indicators that are not used anywhere
 			 * else in diffs, but still look reasonable
@@ -111,7 +112,7 @@ static int read_patches(const char *range, struct string_list *list)
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
 			line[len - 1] = '\n';
-			len = parse_git_diff_header(&root, &linenr, 1, line,
+			len = parse_git_diff_header(&root, &linenr, 0, line,
 						    len, size, &patch);
 			if (len < 0)
 				die(_("could not parse git header '%.*s'"), (int)len, line);
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 0120f769f1..64b66f2094 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -461,4 +461,8 @@ test_expect_success 'format-patch --range-diff as commentary' '
 	grep "> 1: .* new message" 0001-*
 '
 
+test_expect_success 'range-diff overrides diff.noprefix internally' '
+	git -c diff.noprefix=true range-diff HEAD^...
+'
+
 test_done
-- 
gitgitgadget

```

## Junio C Hamano, 2019-10-03 00:41

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <xmqqv9t6d5iy.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqv9t6d5iy.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <xmqq4l0qewu5.fsf@gitster-ct.c.googlers.com>

```
Junio C Hamano <gitster@pobox.com> writes:

> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
>> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>>
>> When parsing the diffs, `range-diff` expects to see the prefixes `a/`
>> and `b/` in the diff headers.
>
> If so, passing src/dst prefix as command line option is a much
> better solution, I think.  diff.noprefix may not stay to be (or it
> may already not to be) the only thing how the prefix gets chosen.

That is, "--src-prefix=a/ --dst-prefix=b/", so that configuration
variables yet to be invented for setting prefixes different from a/
and b/ can also be overridden, not just diff.noprefix (ancient Git
used l/ and k/ as src and dst prefix, IIRC).



```

## Junio C Hamano, 2019-10-03 00:43

Subject: Re: [PATCH 1/1] range-diff: internally force `diff.noprefix=false`
Message-ID: <xmqqr23ud5ff.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqr23ud5ff.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <nycvar.QRO.7.76.6.1910022229340.46@tvgsbejvaqbjf.bet>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> While at it, I invert the logic in v2: instead of forcing a prefix, I
> now force no prefix (and reduce the strip level from 1 to 0 when parsing
> the diff header).

Ah, that approach would also work and may be conceptually cleaner
than requiring a/ and b/ prefix and forcing them.

```

## Junio C Hamano, 2019-10-03 02:11

Subject: Re: [PATCH v2 1/1] range-diff: internally force `diff.noprefix=true`
Message-ID: <xmqq1rvud1dm.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq1rvud1dm.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <9993a607beaafc66f72e1be3f158074cd121b4bb.1570050645.git.gitgitgadget@gmail.com>

```
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> When parsing the diffs, `range-diff` expects to see the prefixes `a/`
> and `b/` in the diff headers.
>
> These prefixes can be forced off via the config setting
> `diff.noprefix=true`. As `range-diff` is not prepared for that
> situation, this will cause a segmentation fault.
>
> Let's avoid that by passing the `--no-prefix` option to the `git log`
> process that generates the diffs that `range-diff` wants to parse.
> And of course expect the output to have no prefixes, then.

Looks good.  Thanks.

```
