[PATCH] Flag empty patches as errors
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> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Sep 30, 2005 at 23:25 UTC
1fea629f794cda57cc161979dab903ec7460cc7c
1 file changed
+13
apply.c
+13
@@ -723,6 +723,16 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
723
return offset;
724
}
725
726
+static inline int metadata_changes(struct patch *patch)
727
+{
728
+ return patch->is_rename > 0 ||
729
+ patch->is_copy > 0 ||
730
+ patch->is_new > 0 ||
731
+ patch->is_delete ||
732
+ (patch->old_mode && patch->new_mode &&
733
+ patch->old_mode != patch->new_mode);
734
+}
735
+
736
static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
737
{
738
int hdrsize, patchsize;
@@ -733,6 +743,9 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
743
744
patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
745
746
+ if (!patchsize && !metadata_changes(patch))
747
+ die("patch with only garbage at line %d", linenr);
748
+
749
return offset + hdrsize + patchsize;
750
}
751