# [PATCH] BUG: git push on an empty clone segfaults.

11 messages from 2009-04-20 to 2009-04-20. Participants: Matthieu Moy, Johannes Schindelin, Junio C Hamano, Jay Soffian, Petr Baudis.
Thread: https://gitlist.dev/t/18964

## Matthieu Moy, 2009-04-20 09:48

Subject: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <1240220930-24679-1-git-send-email-Matthieu.Moy@imag.fr>
URL: https://gitlist.dev/e/1240220930-24679-1-git-send-email-Matthieu.Moy%40imag.fr

```
Ideally, "git push" from an empty repository to another empty one
should be a no-op, or perhaps should error out cleanly. Currently, it
just segfaults.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 t/t5701-clone-local.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh
index 3559d17..7c6ef4c 100755
--- a/t/t5701-clone-local.sh
+++ b/t/t5701-clone-local.sh
@@ -132,4 +132,14 @@ test_expect_success 'clone empty repository' '
 	 test $actual = $expected)
 '
 
+test_expect_failure 'clone empty repository, and then push should not segfault.' '
+	cd "$D" &&
+	rm -fr empty/ empty-clone/ &&
+	mkdir empty &&
+	(cd empty && git init) &&
+	git clone empty empty-clone &&
+	cd empty-clone &&
+	test_must_fail git push
+'
+
 test_done
-- 
1.6.2.2.449.g92961.dirty

```

## Johannes Schindelin, 2009-04-20 10:20

Subject: [PATCH] Fix uninitialized memory in get_local_heads()
Message-ID: <alpine.DEB.1.00.0904201219240.6955@intel-tinevez-2-302>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904201219240.6955%40intel-tinevez-2-302
In-Reply-To: <1240220930-24679-1-git-send-email-Matthieu.Moy@imag.fr>

```

In 454e202(move duplicated get_local_heads() to remote.c), a static
local variable was moved into a function, but not initialized.

This resulted in a crash when trying to push from an empty repository.

Noticed by Matthieu Moy.

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

	On Mon, 20 Apr 2009, Matthieu Moy wrote:

	> Ideally, "git push" from an empty repository to another empty 
	> one should be a no-op, or perhaps should error out cleanly.
	> Currently, it just segfaults.

	Good catch.  This patch goes on top of yours.

	/me is running valgrind now.

 remote.c               |    2 +-
 t/t5701-clone-local.sh |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/remote.c b/remote.c
index a06761a..e4c89b8 100644
--- a/remote.c
+++ b/remote.c
@@ -1504,7 +1504,7 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
 
 struct ref *get_local_heads(void)
 {
-	struct ref *local_refs, **local_tail = &local_refs;
+	struct ref *local_refs = NULL, **local_tail = &local_refs;
 	for_each_ref(one_local_ref, &local_tail);
 	return local_refs;
 }
diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh
index 7c6ef4c..f26b511 100755
--- a/t/t5701-clone-local.sh
+++ b/t/t5701-clone-local.sh
@@ -132,7 +132,7 @@ test_expect_success 'clone empty repository' '
 	 test $actual = $expected)
 '
 
-test_expect_failure 'clone empty repository, and then push should not segfault.' '
+test_expect_success 'clone empty repository, and then push should not segfault.' '
 	cd "$D" &&
 	rm -fr empty/ empty-clone/ &&
 	mkdir empty &&
-- 
1.6.2.1.493.g67cf3

```

## Junio C Hamano, 2009-04-20 10:33

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <7vfxg3ipib.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vfxg3ipib.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1240220930-24679-1-git-send-email-Matthieu.Moy@imag.fr>

```
Matthieu Moy <Matthieu.Moy@imag.fr> writes:

> Ideally, "git push" from an empty repository to another empty one
> should be a no-op, or perhaps should error out cleanly. Currently, it
> just segfaults.

Didn't we see this fixed by Nguyễn with 55f0566 (get_local_heads(): do not
return random pointer if there is no head, 2009-04-17)?

```

## Johannes Schindelin, 2009-04-20 10:55

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <alpine.DEB.1.00.0904201252120.6955@intel-tinevez-2-302>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904201252120.6955%40intel-tinevez-2-302
In-Reply-To: <7vfxg3ipib.fsf@gitster.siamese.dyndns.org>

```
Hi,

On Mon, 20 Apr 2009, Junio C Hamano wrote:

> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
> 
> > Ideally, "git push" from an empty repository to another empty one
> > should be a no-op, or perhaps should error out cleanly. Currently, it
> > just segfaults.
> 
> Didn't we see this fixed by Nguyễn with 55f0566 (get_local_heads(): do not
> return random pointer if there is no head, 2009-04-17)?

I fetched from repo.or.cz and tested with 'master', and it was broken.  
Apparently git://repo.or.cz/git.git is lagging behind by 5 days.  Pasky?

Ciao,
Dscho

```

## Matthieu Moy, 2009-04-20 11:07

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <vpqbpqr1t3w.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqbpqr1t3w.fsf%40bauges.imag.fr
In-Reply-To: <7vfxg3ipib.fsf@gitster.siamese.dyndns.org>

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

> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
>
>> Ideally, "git push" from an empty repository to another empty one
>> should be a no-op, or perhaps should error out cleanly. Currently, it
>> just segfaults.
>
> Didn't we see this fixed by Nguyễn with 55f0566 (get_local_heads(): do not
> return random pointer if there is no head, 2009-04-17)?

Oops, right, I wasn't up to date. Still, the test-case is relevant,
I'll resend with test_expect_success.

-- 
Matthieu

```

## Matthieu Moy, 2009-04-20 11:09

Subject: [PATCH] clone: add test for push on an empty clone.
Message-ID: <1240225777-31334-1-git-send-email-Matthieu.Moy@imag.fr>
URL: https://gitlist.dev/e/1240225777-31334-1-git-send-email-Matthieu.Moy%40imag.fr
In-Reply-To: <7vfxg3ipib.fsf@gitster.siamese.dyndns.org>

```
Commit 55f0566 (get_local_heads(): do not return random pointer if
there is no head, 2009-04-17) fixed a segfault for git push, this
patch adds a test-case to avoid future regressions.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 t/t5701-clone-local.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh
index 3559d17..f26b511 100755
--- a/t/t5701-clone-local.sh
+++ b/t/t5701-clone-local.sh
@@ -132,4 +132,14 @@ test_expect_success 'clone empty repository' '
 	 test $actual = $expected)
 '
 
+test_expect_success 'clone empty repository, and then push should not segfault.' '
+	cd "$D" &&
+	rm -fr empty/ empty-clone/ &&
+	mkdir empty &&
+	(cd empty && git init) &&
+	git clone empty empty-clone &&
+	cd empty-clone &&
+	test_must_fail git push
+'
+
 test_done
-- 
1.6.2.2.449.g92961.dirty

```

## Junio C Hamano, 2009-04-20 11:13

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <7vhc0jh928.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vhc0jh928.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.DEB.1.00.0904201252120.6955@intel-tinevez-2-302>

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

> On Mon, 20 Apr 2009, Junio C Hamano wrote:
>
>> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
>> 
>> > Ideally, "git push" from an empty repository to another empty one
>> > should be a no-op, or perhaps should error out cleanly. Currently, it
>> > just segfaults.
>> 
>> Didn't we see this fixed by Nguyễn with 55f0566 (get_local_heads(): do not
>> return random pointer if there is no head, 2009-04-17)?
>
> I fetched from repo.or.cz and tested with 'master', and it was broken.  
> Apparently git://repo.or.cz/git.git is lagging behind by 5 days.  Pasky?

Unfortunately, that repository is outside of my direct control.

I push into the following four repositories:

	k.org's master repository
	git://repo.or.cz/alt-git.git/
        git://git.sourceforge.jp/gitroot/git-core/git.git
	git://git-core.git.sourceforge.net/gitroot/git-core

The k.org's master repository is mirrored (with small lag) to:

	git://git.kernel.org/pub/scm/git/git.git

I suspect that Pasky mirrors that mirror (with some lag) to:

        git://repo.or.cz/git.git

There may be some other mirrors of mirrors I do not know about.

I'd guess that people in the US are better off going to git.kernel.org,
Europeans to git://repo.or.cz/alt-git.git, and East Asians to
git.sourceforge.jp/.

I've already written about this elsewhere [*1*], but the sourceforge ones
are partial; they only have maint and master (and tags).


[Footnote]

*1* http://gitster.livejournal.com/31668.html

```

## Jay Soffian, 2009-04-20 13:51

Subject: Re: [PATCH] Fix uninitialized memory in get_local_heads()
Message-ID: <76718490904200651t6cee0d73n8cbdea8099487654@mail.gmail.com>
URL: https://gitlist.dev/e/76718490904200651t6cee0d73n8cbdea8099487654%40mail.gmail.com
In-Reply-To: <alpine.DEB.1.00.0904201219240.6955@intel-tinevez-2-302>

```
On Mon, Apr 20, 2009 at 6:20 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
>
> In 454e202(move duplicated get_local_heads() to remote.c), a static
> local variable was moved into a function, but not initialized.
>
> This resulted in a crash when trying to push from an empty repository.
>
> Noticed by Matthieu Moy.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Doh. Thank you for the fix.

j.

```

## Petr Baudis, 2009-04-20 13:55

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <20090420135532.GC8940@machine.or.cz>
URL: https://gitlist.dev/e/20090420135532.GC8940%40machine.or.cz
In-Reply-To: <alpine.DEB.1.00.0904201252120.6955@intel-tinevez-2-302>

```
  Hi,

On Mon, Apr 20, 2009 at 12:55:10PM +0200, Johannes Schindelin wrote:
> I fetched from repo.or.cz and tested with 'master', and it was broken.  
> Apparently git://repo.or.cz/git.git is lagging behind by 5 days.  Pasky?

  yes, I noticed that the mirroring got hanging on some dead repository
earlier today and restarted it, but it might take a while until a full
mirror cycle refreshes everything.

  Sorry about this.

				Petr "Pasky" Baudis

```

## Johannes Schindelin, 2009-04-20 14:30

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <alpine.DEB.1.00.0904201629280.6771@intel-tinevez-2-302>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904201629280.6771%40intel-tinevez-2-302
In-Reply-To: <20090420135532.GC8940@machine.or.cz>

```
Hi,

On Mon, 20 Apr 2009, Petr Baudis wrote:

> On Mon, Apr 20, 2009 at 12:55:10PM +0200, Johannes Schindelin wrote:
> > I fetched from repo.or.cz and tested with 'master', and it was broken.  
> > Apparently git://repo.or.cz/git.git is lagging behind by 5 days.  
> > Pasky?
> 
>   yes, I noticed that the mirroring got hanging on some dead repository 
> earlier today and restarted it, but it might take a while until a full 
> mirror cycle refreshes everything.

I always wondered why it should make sense to have a mirror git.git, and 
in addition a repository alt-git.git that Junio pushes to.

Maybe it is time to fix that?

I mean, I am hardly a Git newbie, and even I got burnt.

Ciao,
Dscho

```

## Petr Baudis, 2009-04-20 20:46

Subject: Re: [PATCH] BUG: git push on an empty clone segfaults.
Message-ID: <20090420204604.GF8940@machine.or.cz>
URL: https://gitlist.dev/e/20090420204604.GF8940%40machine.or.cz
In-Reply-To: <alpine.DEB.1.00.0904201629280.6771@intel-tinevez-2-302>

```
On Mon, Apr 20, 2009 at 04:30:38PM +0200, Johannes Schindelin wrote:
> On Mon, 20 Apr 2009, Petr Baudis wrote:
> 
> > On Mon, Apr 20, 2009 at 12:55:10PM +0200, Johannes Schindelin wrote:
> > > I fetched from repo.or.cz and tested with 'master', and it was broken.  
> > > Apparently git://repo.or.cz/git.git is lagging behind by 5 days.  
> > > Pasky?
> > 
> >   yes, I noticed that the mirroring got hanging on some dead repository 
> > earlier today and restarted it, but it might take a while until a full 
> > mirror cycle refreshes everything.
> 
> I always wondered why it should make sense to have a mirror git.git, and 
> in addition a repository alt-git.git that Junio pushes to.
> 
> Maybe it is time to fix that?
> 
> I mean, I am hardly a Git newbie, and even I got burnt.

Sure, I can fix that, making alt-git.git a soft-link to git.git (or,
specifically, making refs/ and objects/ symlinks). Junio even might not
have to change his push scripts. ;-)

-- 
				Petr "Pasky" Baudis
The average, healthy, well-adjusted adult gets up at seven-thirty
in the morning feeling just terrible. -- Jean Kerr

```
