From 3c1a778dfd5bd4434aa6a15730af2e2b995c6ba5 Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Tue, 4 Aug 2009 13:34:52 -0700
Subject: [PATCH] maybe_backup_file: Clear previously-set after_backup_name

Without this, under various conditions, (including file_name being
a directory), maybe_backup_file would return true without having
done anything, nor having set after_backup_name. Then, if any
error was detected while extracting the member, (such as directory
already exists), the previously backed-up file would be restored,
potentially destroying user data.

This closes Debian bug #508199, with thanks to Eric Lammerts for
reporting it with a concise test case.
---
 debian/changelog | 6 ++++++
 src/misc.c       | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/debian/changelog b/debian/changelog
index 393a9e1..68578c6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+tar (1.22-3) UNRELEASED; urgency=low
+
+  * Avoid incorrectly restoring old backup file, closes: #508199
+
+ -- Carl Worth <cworth@cworth.org>  Tue, 04 Aug 2009 17:16:43 -0700
+
 tar (1.22-2) unstable; urgency=low
 
   * Add Carl Worth as an uploader.
diff --git a/src/misc.c b/src/misc.c
index 951449e..c400bcd 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -422,6 +422,10 @@ maybe_backup_file (const char *file_name, bool this_is_the_archive)
   if (this_is_the_archive && _remdev (file_name))
     return true;
 
+  /* Ensure that no previously backed-up file remains in case we
+   * return early. */
+  assign_string (&after_backup_name, 0);
+
   if (stat (file_name, &file_stat))
     {
       if (errno == ENOENT)
-- 
2.45.2