When an error is encountered in output_files(), the subsequent call to cleanup() calls back into output_files() resulting in an infinte recursion. This is trivially reproduced with a git-style patch (which utilizes output_file_later()) that tries to patch a nonexistent or unreadable file (see attached test case).
* src/patch.c: (output_files) clear the files_to_output list before iterating it, so that recursive calls won't iterate the same files. * tests/git-error: New test case. * tests/Makefile.am (TESTS): Add test case. --- src/patch.c | 12 ++++++++---- tests/Makefile.am | 1 + tests/git-error | 29 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/git-error diff --git a/src/patch.c b/src/patch.c index 81c7a02..e6fdd77 100644 --- a/src/patch.c +++ b/src/patch.c @@ -1939,8 +1939,12 @@ output_files (struct stat const *st) { gl_list_iterator_t iter; const void *elt; + gl_list_t files; - iter = gl_list_iterator (files_to_output); + files = 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; @@ -1958,8 +1962,8 @@ output_files (struct stat const *st) /* Free the list up to here. */ for (;;) { - const void *elt2 = gl_list_get_at (files_to_output, 0); - gl_list_remove_at (files_to_output, 0); + const void *elt2 = gl_list_get_at (files, 0); + gl_list_remove_at (files, 0); if (elt == elt2) break; } @@ -1968,7 +1972,7 @@ output_files (struct stat const *st) } } gl_list_iterator_free (&iter); - gl_list_clear (files_to_output); + gl_list_free (files); } /* Fatal exit with cleanup. */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 16f8693..5ce000a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -41,6 +41,7 @@ TESTS = \ filename-choice \ git-binary-diff \ git-cleanup \ + git-error \ garbage \ global-reject-files \ inname \ diff --git a/tests/git-error b/tests/git-error new file mode 100644 index 0000000..0071957 --- /dev/null +++ b/tests/git-error @@ -0,0 +1,29 @@ +# Copyright (C) 2018 Free Software Foundation, Inc. +# +# Copying and distribution of this file, with or without modification, +# in any medium, are permitted without royalty provided the copyright +# notice and this notice are preserved. + +. $srcdir/test-lib.sh + +require cat +use_local_patch +use_tmpdir + +cat > f.diff <<EOF +diff --git a/boo b/boo +--- /dev/fd/63 2018-02-27 16:32:54.861266246 +0100 ++++ /dev/fd/62 2018-02-27 16:32:54.861266246 +0100 +@@ -1 +1 @@ +-abc ++def + +EOF + +check 'patch .nonexistent < f.diff || echo "Status: $?"' <<EOF +patching file .nonexistent +Hunk #1 FAILED at 1. +1 out of 1 hunk FAILED -- saving rejects to file .nonexistent.rej +$PATCH: **** Can't reopen file .nonexistent : No such file or directory +Status: 2 +EOF -- 2.19.2