git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Flag empty patches as errors

From
Linus Torvalds <torvalds@osdl.org>
Date
Oct 1, 2005, 06:25 UTC
Message-ID
<Pine.LNX.4.64.0509302320560.3378@g5.osdl.org>

A patch that contains no actual diff, and that doesn't change any meta-data is bad. It shouldn't be a patch at all, and git-apply shouldn't just accept it.

This caused a corrupted patch to be silently applied as an empty change in the kernel, because the corruption ended up making the patch look empty.

An example of such a patch is one that contains the patch header, but where the initial fragment header (the "@@ -nr,.." line) is missing, causing us to not parse any fragments.

The real "patch" program will also flag such patches as bad, with the message

	patch: **** Only garbage was found in the patch input.
and we should do likewise.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/apply.c b/apply.c
--- a/apply.c
+++ b/apply.c
@@ -723,6 +723,16 @@ static int parse_single_patch(char *line
 	return offset;
 }
 
+static inline int metadata_changes(struct patch *patch)
+{
+	return	patch->is_rename > 0 ||
+		patch->is_copy > 0 ||
+		patch->is_new > 0 ||
+		patch->is_delete ||
+		(patch->old_mode && patch->new_mode &&
+		 patch->old_mode != patch->new_mode);
+}
+
 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 {
 	int hdrsize, patchsize;
@@ -733,6 +743,9 @@ static int parse_chunk(char *buffer, uns
 
 	patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
 
+	if (!patchsize && !metadata_changes(patch))
+		die("patch with only garbage at line %d", linenr);
+
 	return offset + hdrsize + patchsize;
 }
 
Next: Junio C Hamano
Message 1 of 4 in “Flag empty patches as errors”
  1. Linus TorvaldsOct 1, 2005
  2. Junio C HamanoOct 1, 2005
  3. Packing on kernel.orgMartin Coxall, Oct 1, 2005
  4. H. Peter AnvinOct 1, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.