# [PATCH] Bad file descriptor on filtering empty files

2 messages from 2014-12-19 to 2014-12-22. Participants: William Throwe, Junio C Hamano.
Thread: https://gitlist.dev/t/38211

## William Throwe, 2014-12-19 22:41

Subject: [PATCH] Bad file descriptor on filtering empty files
Message-ID: <87sigbmfd5.fsf@cornell.edu>
URL: https://gitlist.dev/e/87sigbmfd5.fsf%40cornell.edu

```
In git 2.2.0 (also tested on 2.2.0.65.g9abc44b), if an external
smudge/clean filter is called on an empty file git reports something
like:
error: copy-fd: read returned Bad file descriptor
error: cannot feed the input to external filter cat
error: external filter cat failed

Test case:

mkdir bug
cd bug
git init
git config filter.cat.clean cat
git config filter.cat.smudge cat
echo '* filter=cat' >.gitattributes
touch a
git add a


This started in 9035d75a2be9d80d82676504d69553245017f6d4, which
introduced the possible call to copy_fd in code called from
apply_filter.  It appears that NULL as the src argument to apply_filter
is being used both as a sentinel value to indicate that the fd should be
used instead and also as a representation of the contents of an empty
file.  I suggest switching to using fd == -1 as the sentinel as shown in
the patch below.

Thanks,
Will


diff --git a/convert.c b/convert.c
index 9a5612e..0509ac1 100644
--- a/convert.c
+++ b/convert.c
@@ -355,7 +355,7 @@ static int filter_buffer_or_fd(int in, int out, void *data)
 
 	sigchain_push(SIGPIPE, SIG_IGN);
 
-	if (params->src) {
+	if (params->fd == -1) {
 		write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
 	} else {
 		write_err = copy_fd(params->fd, child_process.in);

```

## Junio C Hamano, 2014-12-22 18:26

Subject: Re: [PATCH] Bad file descriptor on filtering empty files
Message-ID: <xmqq7fxja6c8.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq7fxja6c8.fsf%40gitster.dls.corp.google.com
In-Reply-To: <87sigbmfd5.fsf@cornell.edu>

```
William Throwe <wtt6@cornell.edu> writes:

> In git 2.2.0 (also tested on 2.2.0.65.g9abc44b), if an external
> smudge/clean filter is called on an empty file git reports something
> like:
> error: copy-fd: read returned Bad file descriptor
> error: cannot feed the input to external filter cat
> error: external filter cat failed
>
> Test case:
>
> mkdir bug
> cd bug
> git init
> git config filter.cat.clean cat
> git config filter.cat.smudge cat
> echo '* filter=cat' >.gitattributes
> touch a
> git add a
>
>
> This started in 9035d75a2be9d80d82676504d69553245017f6d4, which
> introduced the possible call to copy_fd in code called from
> apply_filter.  It appears that NULL as the src argument to apply_filter
> is being used both as a sentinel value to indicate that the fd should be
> used instead and also as a representation of the contents of an empty
> file.  I suggest switching to using fd == -1 as the sentinel as shown in
> the patch below.
>
> Thanks,
> Will
>

William, thanks for raising this issue.

Steffen, comments?

> diff --git a/convert.c b/convert.c
> index 9a5612e..0509ac1 100644
> --- a/convert.c
> +++ b/convert.c
> @@ -355,7 +355,7 @@ static int filter_buffer_or_fd(int in, int out, void *data)
>  
>  	sigchain_push(SIGPIPE, SIG_IGN);
>  
> -	if (params->src) {
> +	if (params->fd == -1) {
>  		write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
>  	} else {
>  		write_err = copy_fd(params->fd, child_process.in);

```
