# git stash apply segfaulting when called in subdir

7 messages from 2007-07-25 to 2007-07-26. Participants: Uwe Kleine-König, Junio C Hamano, Steven Walter, Johannes Schindelin, Jeff King.
Thread: https://gitlist.dev/t/9224

## Uwe Kleine-König, 2007-07-25 21:23

Subject: git stash apply segfaulting when called in subdir
Message-ID: <20070725212311.GA13325@cassiopeia>
URL: https://gitlist.dev/e/20070725212311.GA13325%40cassiopeia

```
Hello,

I did not do much testing and didn't look into the issue yet:

zeisberg@cassiopeia:/tmp$ mkdir repo; cd repo; git init
Initialized empty Git repository in .git/

zeisberg@cassiopeia:/tmp/repo$ mkdir dir; echo one > file; echo two > dir/file

zeisberg@cassiopeia:/tmp/repo$ git add file dir/file

zeisberg@cassiopeia:/tmp/repo$ git commit -m tralala
Created initial commit 265b7d7: tralala
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 dir/file
 create mode 100644 file

zeisberg@cassiopeia:/tmp/repo$ echo three >> file

zeisberg@cassiopeia:/tmp/repo$ git stash
Saved "WIP on master: 265b7d7... tralala"
HEAD is now at 265b7d7... tralala

zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
/home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree

Best regards
Uwe

-- 
Uwe Kleine-König

http://www.google.com/search?q=1+newton+in+kg*m+%2F+s%5E2

```

## Junio C Hamano, 2007-07-25 22:28

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <7v1wewdsoz.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v1wewdsoz.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20070725212311.GA13325@cassiopeia>

```
Uwe Kleine-König  <ukleinek@informatik.uni-freiburg.de> writes:

> zeisberg@cassiopeia:/tmp$ mkdir repo; cd repo; git init
> Initialized empty Git repository in .git/
>
> zeisberg@cassiopeia:/tmp/repo$ mkdir dir; echo one > file; echo two > dir/file
>
> zeisberg@cassiopeia:/tmp/repo$ git add file dir/file
>
> zeisberg@cassiopeia:/tmp/repo$ git commit -m tralala
> Created initial commit 265b7d7: tralala
>  2 files changed, 2 insertions(+), 0 deletions(-)
>  create mode 100644 dir/file
>  create mode 100644 file
>
> zeisberg@cassiopeia:/tmp/repo$ echo three >> file
>
> zeisberg@cassiopeia:/tmp/repo$ git stash
> Saved "WIP on master: 265b7d7... tralala"
> HEAD is now at 265b7d7... tralala
>
> zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
> error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
> /home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree

This probably is a merge-recursive bug, but in the meantime,
I think this should fix it.

---

 git-stash.sh |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/git-stash.sh b/git-stash.sh
index de13dd1..d9cd42d 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -6,6 +6,7 @@ USAGE='[ | list | show | apply | clear]'
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
 require_work_tree
+cd_to_toplevel
 
 TMP="$GIT_DIR/.git-stash.$$"
 trap 'rm -f "$TMP-*"' 0

```

## Steven Walter, 2007-07-25 22:32

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <20070725223244.GB21274@dervierte>
URL: https://gitlist.dev/e/20070725223244.GB21274%40dervierte
In-Reply-To: <20070725212311.GA13325@cassiopeia>

```
On Wed, Jul 25, 2007 at 11:23:11PM +0200, Uwe Kleine-König wrote:
> zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
> error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
> /home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree

I've been seeing this, as well.
-- 
-Steven Walter <stevenrwalter@gmail.com>
"A human being should be able to change a diaper, plan an invasion,
butcher a hog, conn a ship, design a building, write a sonnet, balance
accounts, build a wall, set a bone, comfort the dying, take orders,
give orders, cooperate, act alone, solve equations, analyze a new
problem, pitch manure, program a computer, cook a tasty meal, fight
efficiently, die gallantly. Specialization is for insects."
   -Robert Heinlein

```

## Johannes Schindelin, 2007-07-25 23:01

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <Pine.LNX.4.64.0707260001091.14781@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0707260001091.14781%40racer.site
In-Reply-To: <7v1wewdsoz.fsf@assigned-by-dhcp.cox.net>

```
Hi,

On Wed, 25 Jul 2007, Junio C Hamano wrote:

> Uwe Kleine-König  <ukleinek@informatik.uni-freiburg.de> writes:
> 
> > zeisberg@cassiopeia:/tmp$ mkdir repo; cd repo; git init
> > Initialized empty Git repository in .git/
> >
> > zeisberg@cassiopeia:/tmp/repo$ mkdir dir; echo one > file; echo two > dir/file
> >
> > zeisberg@cassiopeia:/tmp/repo$ git add file dir/file
> >
> > zeisberg@cassiopeia:/tmp/repo$ git commit -m tralala
> > Created initial commit 265b7d7: tralala
> >  2 files changed, 2 insertions(+), 0 deletions(-)
> >  create mode 100644 dir/file
> >  create mode 100644 file
> >
> > zeisberg@cassiopeia:/tmp/repo$ echo three >> file
> >
> > zeisberg@cassiopeia:/tmp/repo$ git stash
> > Saved "WIP on master: 265b7d7... tralala"
> > HEAD is now at 265b7d7... tralala
> >
> > zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
> > error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
> > /home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree
> 
> This probably is a merge-recursive bug, but in the meantime,
> I think this should fix it.
> 
> ---
> 
>  git-stash.sh |    1 +
>  1 files changed, 1 insertions(+), 0 deletions(-)
> 
> diff --git a/git-stash.sh b/git-stash.sh
> index de13dd1..d9cd42d 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -6,6 +6,7 @@ USAGE='[ | list | show | apply | clear]'
>  SUBDIRECTORY_OK=Yes
>  . git-sh-setup
>  require_work_tree
> +cd_to_toplevel
>  
>  TMP="$GIT_DIR/.git-stash.$$"
>  trap 'rm -f "$TMP-*"' 0

It does... And here is a test case:

-- snipsnap --
[PATCH] Test "stash apply" in a subdirectory

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

---

 t/t3903-stash.sh |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 392ac1c..9e363fd 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -66,4 +66,18 @@ test_expect_success 'apply stashed changes (including index)' '
 	test 1 = $(git show HEAD:file)
 '
 
+test_expect_success 'apply in subdir' '
+	git --git-dir=new init &&
+	cd new &&
+	mkdir dir &&
+	echo one > file &&
+	echo two > dir/file &&
+	git add file dir/file &&
+	git commit -m first &&
+	echo three >> file &&
+	git stash &&
+	cd dir &&
+	git stash apply
+'
+
 test_done
```

## Jeff King, 2007-07-26 05:07

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <20070726050726.GC32617@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20070726050726.GC32617%40coredump.intra.peff.net
In-Reply-To: <7v1wewdsoz.fsf@assigned-by-dhcp.cox.net>

```
On Wed, Jul 25, 2007 at 03:28:12PM -0700, Junio C Hamano wrote:

> > zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
> > error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
> > /home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree
> 
> This probably is a merge-recursive bug, but in the meantime,
> I think this should fix it.

The merge-recursive bug is simply a failure to check for invalid input.
Patch to at least print and die rather than segfault is below.
Technically the error could be from a tag dereference, too, so maybe the
error message should be more exact?

-- >8 --
merge-recursive: don't segfault on missing input objects

This should generally never happen, but it's nicer to report
an error than to segfault.

Signed-off-by: Jeff King <peff@peff.net>
---
 merge-recursive.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/merge-recursive.c b/merge-recursive.c
index c8539ec..2764e61 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1692,6 +1692,8 @@ static struct commit *get_ref(const char *ref)
 	if (get_sha1(ref, sha1))
 		die("Could not resolve ref '%s'", ref);
 	object = deref_tag(parse_object(sha1), ref, strlen(ref));
+	if (!object)
+		die("Object does not exist: %s", ref);
 	if (object->type == OBJ_TREE)
 		return make_virtual_commit((struct tree*)object,
 			better_branch_name(ref));
-- 
1.5.3.rc3.818.gc1dc7-dirty

```

## Johannes Schindelin, 2007-07-26 05:40

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <Pine.LNX.4.64.0707260635060.14781@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0707260635060.14781%40racer.site
In-Reply-To: <20070726050726.GC32617@coredump.intra.peff.net>

```
Hi,

On Thu, 26 Jul 2007, Jeff King wrote:

> On Wed, Jul 25, 2007 at 03:28:12PM -0700, Junio C Hamano wrote:
> 
> > > zeisberg@cassiopeia:/tmp/repo$ cd dir; git stash apply
> > > error: missing object referenced by '696146c2a44d7fc4d5ae4a71589c4c0d84f59789'
> > > /home/zeisberg/usr/bin/git-stash: line 111: 13618 Segmentation fault      git-merge-recursive $b_tree -- $c_tree $w_tree
> > 
> > This probably is a merge-recursive bug, but in the meantime,
> > I think this should fix it.
> 
> The merge-recursive bug is simply a failure to check for invalid input.

No it is not.

The objects are there.  But the objects cannot be found, since 
merge-recursive cannot go to top level.

Ciao,
Dscho

```

## Jeff King, 2007-07-26 05:48

Subject: Re: git stash apply segfaulting when called in subdir
Message-ID: <20070726054800.GA21518@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20070726054800.GA21518%40coredump.intra.peff.net
In-Reply-To: <Pine.LNX.4.64.0707260635060.14781@racer.site>

```
On Thu, Jul 26, 2007 at 06:40:44AM +0100, Johannes Schindelin wrote:

> > The merge-recursive bug is simply a failure to check for invalid input.
> 
> No it is not.
> 
> The objects are there.  But the objects cannot be found, since 
> merge-recursive cannot go to top level.

Fair enough, I should have looked a little further. However, I still
wonder if it's better to report an error than to segfault.

-Peff

```
