# [PATCH] fix hang in git fetch if pointed at a 0 length bundle

6 messages from 2012-01-03 to 2012-01-03. Participants: Brian Harring, Johannes Sixt, Nguyen Thai Ngoc Duy, Junio C Hamano.
Thread: https://gitlist.dev/t/29283

## Brian Harring, 2012-01-03 01:13

Subject: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <20120103011352.GA13825@localhost>
URL: https://gitlist.dev/e/20120103011352.GA13825%40localhost

```
git-repo if interupted at the exact wrong time will generate zero
length bundles- literal empty files.  git-repo is wrong here, but
git fetch shouldn't effectively spin loop if pointed at a zero
length bundle.

Signed-off-by: Brian Harring <ferringb@chromium.org>
---
 bundle.c          |    2 +-
 t/t5704-bundle.sh |   10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/bundle.c b/bundle.c
index 4742f27..ac9cc20 100644
--- a/bundle.c
+++ b/bundle.c
@@ -31,7 +31,7 @@ static int strbuf_readline_fd(struct strbuf *sb, int fd)
 	while (1) {
 		char ch;
 		ssize_t len = xread(fd, &ch, 1);
-		if (len < 0)
+		if (len <= 0)
 			return -1;
 		strbuf_addch(sb, ch);
 		if (ch == '\n')
diff --git a/t/t5704-bundle.sh b/t/t5704-bundle.sh
index 728ccd8..75ce5b8 100755
--- a/t/t5704-bundle.sh
+++ b/t/t5704-bundle.sh
@@ -53,4 +53,14 @@ test_expect_failure 'bundle --stdin <rev-list options>' '
 
 '
 
+test_expect_success 'die if bundle file is empty' '
+
+   echo -n > empty-bundle
+   timeout 5 git fetch empty-bundle
+   ret=$?
+   [ $ret == 128 ] && return 0
+   return $ret
+
+'
+
 test_done
-- 
1.7.8.2

```

## Johannes Sixt, 2012-01-03 08:35

Subject: Re: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <4F02BDEC.9060407@kdbg.org>
URL: https://gitlist.dev/e/4F02BDEC.9060407%40kdbg.org
In-Reply-To: <20120103011352.GA13825@localhost>

```
Am 03.01.2012 02:13, schrieb Brian Harring:
> git-repo if interupted at the exact wrong time will generate zero
> length bundles- literal empty files.  git-repo is wrong here, but
> git fetch shouldn't effectively spin loop if pointed at a zero
> length bundle.

Adding a test case is very much appreciated.

> +test_expect_success 'die if bundle file is empty' '

How about 'empty bundle file is rejected'?

> +
> +   echo -n > empty-bundle

'echo -n' is not portable; use simply

	>empty-bundle &&

(note the style: no blank after >). Also chain commands using &&.

> +   timeout 5 git fetch empty-bundle

Yes, there was an infinite loop. But we do not specifically protect our
git invocations in the test suite against this sort of failure. Just write

	test_must_fail git fetch empty-bundle

and end the test case here.

> +   ret=$?
> +   [ $ret == 128 ] && return 0
> +   return $ret
> +
> +'

Furthermore, indentation should be one tabstop, not blanks.

-- Hannes

```

## Nguyen Thai Ngoc Duy, 2012-01-03 12:07

Subject: Re: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <CACsJy8B5B2bx7WK7ViziseuWCPaMgcc-avwtsw2DybRme8Vgfg@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8B5B2bx7WK7ViziseuWCPaMgcc-avwtsw2DybRme8Vgfg%40mail.gmail.com
In-Reply-To: <20120103011352.GA13825@localhost>

```
On Tue, Jan 3, 2012 at 8:13 AM, Brian Harring <ferringb@gmail.com> wrote:
> @@ -31,7 +31,7 @@ static int strbuf_readline_fd(struct strbuf *sb, int fd)
>        while (1) {
>                char ch;
>                ssize_t len = xread(fd, &ch, 1);
> -               if (len < 0)
> +               if (len <= 0)
>                        return -1;
>                strbuf_addch(sb, ch);
>                if (ch == '\n')

I think it should return 0 when len == 0 because strictly speaking eof
is not a fault. It's not really important though because the two
callers in this file work fine either way.

FWIW I went through all xread call sites. All seem to handle return
value <= 0 correctly.
-- 
Duy

```

## Brian Harring, 2012-01-03 13:46

Subject: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <20120103134603.GA31034@localhost>
URL: https://gitlist.dev/e/20120103134603.GA31034%40localhost
In-Reply-To: <20120103011352.GA13825@localhost>

```
git-repo if interupted at the exact wrong time will generate zero
length bundles- literal empty files.  git-repo is wrong here, but
git fetch shouldn't effectively spin loop if pointed at a zero
length bundle.

Signed-off-by: Brian Harring <ferringb@chromium.org>
---
 bundle.c          |    4 ++--
 t/t5704-bundle.sh |    6 ++++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/bundle.c b/bundle.c
index 4742f27..b8acf3c 100644
--- a/bundle.c
+++ b/bundle.c
@@ -31,8 +31,8 @@ static int strbuf_readline_fd(struct strbuf *sb, int fd)
 	while (1) {
 		char ch;
 		ssize_t len = xread(fd, &ch, 1);
-		if (len < 0)
-			return -1;
+		if (len <= 0)
+			return len;
 		strbuf_addch(sb, ch);
 		if (ch == '\n')
 			break;
diff --git a/t/t5704-bundle.sh b/t/t5704-bundle.sh
index 728ccd8..4ae127d 100755
--- a/t/t5704-bundle.sh
+++ b/t/t5704-bundle.sh
@@ -53,4 +53,10 @@ test_expect_failure 'bundle --stdin <rev-list options>' '
 
 '
 
+test_expect_success 'empty bundle file is rejected' '
+
+    >empty-bundle && test_must_fail git fetch empty-bundle
+
+'
+
 test_done
-- 
1.7.8.2

```

## Junio C Hamano, 2012-01-03 20:11

Subject: Re: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <7vboqktukz.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vboqktukz.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CACsJy8B5B2bx7WK7ViziseuWCPaMgcc-avwtsw2DybRme8Vgfg@mail.gmail.com>

```
Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> On Tue, Jan 3, 2012 at 8:13 AM, Brian Harring <ferringb@gmail.com> wrote:
>> @@ -31,7 +31,7 @@ static int strbuf_readline_fd(struct strbuf *sb, int fd)
>>        while (1) {
>>                char ch;
>>                ssize_t len = xread(fd, &ch, 1);
>> -               if (len < 0)
>> +               if (len <= 0)
>>                        return -1;
>>                strbuf_addch(sb, ch);
>>                if (ch == '\n')
>
> I think it should return 0 when len == 0 because strictly speaking eof
> is not a fault.

Even if you do not strictly speak, end of file is a perfectly normal thing
to see, no?  IOW wouldn't the original patch actively _break_ callers that
read the whole file from the file descriptor to the end?

> FWIW I went through all xread call sites. All seem to handle return
> value <= 0 correctly.

Thanks.

```

## Junio C Hamano, 2012-01-03 20:13

Subject: Re: [PATCH] fix hang in git fetch if pointed at a 0 length bundle
Message-ID: <7v7h18tuhx.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7h18tuhx.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20120103134603.GA31034@localhost>

```
Brian Harring <ferringb@gmail.com> writes:

> git-repo if interupted at the exact wrong time will generate zero
> length bundles- literal empty files.  git-repo is wrong here, but
> git fetch shouldn't effectively spin loop if pointed at a zero
> length bundle.
>
> Signed-off-by: Brian Harring <ferringb@chromium.org>
> ---

Thanks.

Also thanks to all reviewers.

```
