# [PATCH] diff: don't presume empty file when corresponding object is missing

2 messages from 2010-10-22 to 2010-10-22. Participants: Nicolas Pitre, Uwe Kleine-König.
Thread: https://gitlist.dev/t/25514

## Nicolas Pitre, 2010-10-22 04:02

Subject: [PATCH] diff: don't presume empty file when corresponding object is missing
Message-ID: <alpine.LFD.2.00.1010212353550.2764@xanadu.home>
URL: https://gitlist.dev/e/alpine.LFD.2.00.1010212353550.2764%40xanadu.home

```
The low-level diff code will happily produce totally bogus diff output 
with a broken repository via format-patch and friends by treating missing
objects as empty files.  Let's prevent that from happening any longer.

Reported-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Nicolas Pitre <nico@fluxnic.net>
---

diff --git a/diff.c b/diff.c
index 4732b32..b2839f9 100644
--- a/diff.c
+++ b/diff.c
@@ -2386,10 +2386,14 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
 	}
 	else {
 		enum object_type type;
-		if (size_only)
+		if (size_only) {
 			type = sha1_object_info(s->sha1, &s->size);
-		else {
+			if (type < 0)
+				die("unable to read %s", sha1_to_hex(s->sha1));
+		} else {
 			s->data = read_sha1_file(s->sha1, &type, &s->size);
+			if (!s->data)
+				die("unable to read %s", sha1_to_hex(s->sha1));
 			s->should_free = 1;
 		}
 	}

```

## Uwe Kleine-König, 2010-10-22 07:04

Subject: Re: [PATCH] diff: don't presume empty file when corresponding object is missing
Message-ID: <20101022070446.GA19834@pengutronix.de>
URL: https://gitlist.dev/e/20101022070446.GA19834%40pengutronix.de
In-Reply-To: <alpine.LFD.2.00.1010212353550.2764@xanadu.home>

```
Hi Nicolas,

On Fri, Oct 22, 2010 at 12:02:27AM -0400, Nicolas Pitre wrote:
> The low-level diff code will happily produce totally bogus diff output 
> with a broken repository via format-patch and friends by treating missing
> objects as empty files.  Let's prevent that from happening any longer.
> 
> Reported-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Great, this fixes format-patch to fail in my tree as advertised.
Tested-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

> Signed-off-by: Nicolas Pitre <nico@fluxnic.net>

Thanks
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

```
