Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesbrink/7d6941b4c5fdaf449e796fe9924d852b to your computer and use it in GitHub Desktop.
Save jamesbrink/7d6941b4c5fdaf449e796fe9924d852b to your computer and use it in GitHub Desktop.
Resolves segfault in patch command
From b0a9156259c1ff9f1a0d42a86e08250e37248a0b Mon Sep 17 00:00:00 2001
From: James Brink <brink.james@gmail.com>
Date: Thu, 27 Dec 2018 04:50:56 -0700
Subject: [PATCH] Updated output_files function to prevent segfault.
I have updated the output_files function in patch.c to gracefully
handle the following scenario.
Given a git style patch like the following.
diff --git a/some_file b/some_file
index 7898192..6178079 100644
--- a/some_file
+++ b/some_file
@@ -1 +1 @@
-a
+b
and then running
patch non-existent.file my.patch
will result in a nasty recursion loop that eventually segfaults.
This patch simply addresses the segfault and nothing else, I suspect
some additional work and better error reporting/handling would be
needed.
here is a full example using docker to safely see this bug in action.
docker pull alpine:latest && docker run -i -t alpine:latest sh -c "apk --update add git patch;git config --global user.email 'you@example.com';git config --global user.name 'Your Name';git init;echo 'a' > some_file;git add some_file;git commit -m 'Initial commit';echo -e 'b' > some_file;git diff | patch some_file;"
---
src/patch.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/patch.c b/src/patch.c
index 81c7a02..c7b6357 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -1938,9 +1938,11 @@ static void
output_files (struct stat const *st)
{
gl_list_iterator_t iter;
+ gl_list_t files = files_to_output;
const void *elt;
- iter = gl_list_iterator (files_to_output);
+ init_files_to_output();
+ iter = gl_list_iterator (files);
while (gl_list_iterator_next (&iter, &elt, NULL))
{
const struct file_to_output *file_to_output = elt;
--
2.20.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment