summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@linbit.com>2012-04-24 12:37:26 +0200
committerAndreas Gruenbacher <agruen@linbit.com>2012-04-24 12:37:26 +0200
commit90d4e1f3d5fe04e3fb60ec21fb2a943cc90b0c75 (patch)
tree676207550656750f8a0237fe98b1f5ccdf258322
parent709688a8d9c7231d74c6c4f0f6679a1f912b0973 (diff)
downloadpatch-90d4e1f3d5fe04e3fb60ec21fb2a943cc90b0c75.tar.gz
Fix segfault in output_file_later()
Bug reported by Dmitry V. Levin <ldv@altlinux.org>. * src/patch.c (output_file_later): Fix case where the output file is identical with the input file (and to == NULL).
-rw-r--r--src/patch.c2
-rw-r--r--tests/create-delete21
2 files changed, 22 insertions, 1 deletions
diff --git a/src/patch.c b/src/patch.c
index 40987c1..e0e4f38 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -1730,7 +1730,7 @@ output_file_later (char const *from, int *from_needs_removal, const struct stat
file_to_output = xmalloc (sizeof *file_to_output);
file_to_output->from = xstrdup (from);
file_to_output->from_st = *from_st;
- file_to_output->to = xstrdup (to);
+ file_to_output->to = to ? xstrdup (to) : NULL;
file_to_output->mode = mode;
file_to_output->backup = backup;
gl_list_add_last (files_to_output, file_to_output);
diff --git a/tests/create-delete b/tests/create-delete
index c9d9ff9..9a6e1bb 100644
--- a/tests/create-delete
+++ b/tests/create-delete
@@ -168,3 +168,24 @@ EOF
check 'cat f' <<EOF
f
EOF
+#
+# ----------------------------------------------------------------
+
+# This test deletes the target file.
+
+echo data > target
+cat > p.diff <<EOF
+diff --git a/target b/target
+index 1..0
+EOF
+
+check 'patch -p1 -b < p.diff || echo status: $?' <<EOF
+patching file target
+File target is not empty after patch, as expected
+EOF
+
+ncheck 'test ! -e target'
+
+check 'cat target.orig' <<EOF
+data
+EOF