Merge branch 'jn/static' into maint

The packaged static library has been useless for compression since
4.999.9beta-1 two and a half years ago.

* jn/static:
  Do not let liblzma.a from xzdec-build clobber the real thing
diff --git a/debian/changelog b/debian/changelog
index 779fdd8..39dffc0 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,10 +1,33 @@
 xz-utils (5.0.0-2.1) stable; urgency=low
 
+  * Fixes from upstream:
+    * liblzma:
+      - lzma_easy_buffer_encode() and lzma_stream_buffer_encode()
+        avoid writing Blocks with empty compressed data that xz and
+        liblzma versions before 5.0.2 cannot read.
+      - Plugs a memory leak in lzma_stream_encoder().
+      - lzma_index_init() returns NULL instead of segfaulting on
+        allocation failure.
+    * "xz -v -v --list" does not free() filter options unless the
+      filter options array has been initialized.  This prevents
+      reading and free()ing pointers from past the end of an on-stack
+      array when one of the listed files has an unmeaningful Block
+      header size.
+    * The exit status from “xzdiff foo.xz bar.xz” reflects whether
+      files differ.  Thanks to Peter Pallinger.  Closes: #635501.
+    * The Czech “xz --help” output uses a more correct term for files
+      with holes.  Thanks to Petr Hubený.  Closes: #605762.
+    * The Italian diagnostic for an invalid --format argument lost an
+      extra 'N'.
+  * debian/rules: "chmod +x tests/test_scripts.sh" for new xzdiff
+    tests.
+  * debian/symbols: Bump minimal versions for lzma_easy_buffer_encode
+    and lzma_stream_buffer_encode.
   * liblzma-dev: Install an appropriate library for static linking
     instead of the decompression-only version used to build xzdec.
-    Thanks to Anton Tolchanov for a report.  Closes: #673001.
+    Thanks to Anton Tolchanov.  Closes: #673001.
 
- -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 17 May 2012 20:25:00 -0500
+ -- Jonathan Nieder <jrnieder@gmail.com>  Thu, 24 May 2012 13:23:33 -0500
 
 xz-utils (5.0.0-2) unstable; urgency=low
 
diff --git a/debian/patches/cs-sparse-file b/debian/patches/cs-sparse-file
new file mode 100644
index 0000000..4611d6e
--- /dev/null
+++ b/debian/patches/cs-sparse-file
@@ -0,0 +1,43 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Tue, 7 Dec 2010 18:52:04 +0200
+Subject: Translations: Fix Czech translation of "sparse file".
+
+Thanks to Petr Hubený and Marek Černocký.
+---
+ po/cs.po |    6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/po/cs.po b/po/cs.po
+index 5bebf6e4..3ef3fcc9 100644
+--- a/po/cs.po
++++ b/po/cs.po
+@@ -7,7 +7,7 @@ msgstr ""
+ "Project-Id-Version: xz-utils\n"
+ "Report-Msgid-Bugs-To: lasse.collin@tukaani.org\n"
+ "POT-Creation-Date: 2010-10-23 17:48+0300\n"
+-"PO-Revision-Date: 2010-09-17 18:54+0200\n"
++"PO-Revision-Date: 2010-12-03 11:32+0100\n"
+ "Last-Translator: Marek Černocký <marek@manet.cz>\n"
+ "Language-Team: Czech <diskuze@lists.l10n.cz>\n"
+ "Language: cs\n"
+@@ -153,7 +153,7 @@ msgstr "%s: Selhalo zavření souboru: %s"
+ #: src/xz/file_io.c:762 src/xz/file_io.c:946
+ #, c-format
+ msgid "%s: Seeking failed when trying to create a sparse file: %s"
+-msgstr "%s: Selhalo nastavení pozice při pokusu o vytvoření záložního souboru: %s"
++msgstr "%s: Selhalo nastavení pozice při pokusu o vytvoření souboru řídké matice: %s"
+ 
+ #: src/xz/file_io.c:821
+ #, c-format
+@@ -517,7 +517,7 @@ msgid ""
+ "                      filenames must be terminated with the newline character\n"
+ "      --files0[=FILE] like --files but use the null character as terminator"
+ msgstr ""
+-"     --no-sparse       nevytvářet při dekomprimaci záložní soubory\n"
++"     --no-sparse       nevytvářet při dekomprimaci soubory řídkých matic\n"
+ " -S, --suffix=.PRIP    použít u komprimovaných souborů příponu „.PRIP“\n"
+ "     --files[=SOUBOR]  číst názvy souborů, které se mají zpracovat, ze SOUBORu;\n"
+ "                       pokud není SOUBOR zadán, čte se ze standardního vstupu;\n"
+-- 
+1.7.10.2
+
diff --git a/debian/patches/encoder-skip-empty-blocks b/debian/patches/encoder-skip-empty-blocks
new file mode 100644
index 0000000..ad86f6b
--- /dev/null
+++ b/debian/patches/encoder-skip-empty-blocks
@@ -0,0 +1,61 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Mon, 11 Apr 2011 13:59:50 +0300
+Subject: liblzma: Don't create an empty Block in lzma_stream_buffer_encode().
+
+Empty Block was created if the input buffer was empty.
+Empty Block wastes a few bytes of space, but more importantly
+it triggers a bug in XZ Utils 5.0.1 and older when trying
+to decompress such a file. 5.0.1 and older consider such
+files to be corrupt. I thought that no encoder creates empty
+Blocks when releasing 5.0.2 but I was wrong.
+---
+ src/liblzma/common/stream_buffer_encoder.c |   20 +++++++++++++-------
+ 1 file changed, 13 insertions(+), 7 deletions(-)
+
+diff --git a/src/liblzma/common/stream_buffer_encoder.c b/src/liblzma/common/stream_buffer_encoder.c
+index f727d854..0542c30a 100644
+--- a/src/liblzma/common/stream_buffer_encoder.c
++++ b/src/liblzma/common/stream_buffer_encoder.c
+@@ -81,26 +81,32 @@ lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
+ 
+ 	out_pos += LZMA_STREAM_HEADER_SIZE;
+ 
+-	// Block
++	// Encode a Block but only if there is at least one byte of input.
+ 	lzma_block block = {
+ 		.version = 0,
+ 		.check = check,
+ 		.filters = filters,
+ 	};
+ 
+-	return_if_error(lzma_block_buffer_encode(&block, allocator,
+-			in, in_size, out, &out_pos, out_size));
++	if (in_size > 0)
++		return_if_error(lzma_block_buffer_encode(&block, allocator,
++				in, in_size, out, &out_pos, out_size));
+ 
+ 	// Index
+ 	{
+-		// Create an Index with one Record.
++		// Create an Index. It will have one Record if there was
++		// at least one byte of input to encode. Otherwise the
++		// Index will be empty.
+ 		lzma_index *i = lzma_index_init(allocator);
+ 		if (i == NULL)
+ 			return LZMA_MEM_ERROR;
+ 
+-		lzma_ret ret = lzma_index_append(i, allocator,
+-				lzma_block_unpadded_size(&block),
+-				block.uncompressed_size);
++		lzma_ret ret = LZMA_OK;
++
++		if (in_size > 0)
++			ret = lzma_index_append(i, allocator,
++					lzma_block_unpadded_size(&block),
++					block.uncompressed_size);
+ 
+ 		// If adding the Record was successful, encode the Index
+ 		// and get its size which will be stored into Stream Footer.
+-- 
+1.7.10.2
+
diff --git a/debian/patches/index_init-NULL-dereference b/debian/patches/index_init-NULL-dereference
new file mode 100644
index 0000000..6517a6d
--- /dev/null
+++ b/debian/patches/index_init-NULL-dereference
@@ -0,0 +1,32 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Fri, 27 May 2011 22:09:49 +0300
+Subject: liblzma: Handle allocation failures correctly in lzma_index_init().
+
+Thanks to Jim Meyering.
+---
+ src/liblzma/common/index.c |    7 +++++--
+ 1 file changed, 5 insertions(+), 2 deletions(-)
+
+diff --git a/src/liblzma/common/index.c b/src/liblzma/common/index.c
+index ddb9d364..9af4bc19 100644
+--- a/src/liblzma/common/index.c
++++ b/src/liblzma/common/index.c
+@@ -398,10 +398,13 @@ extern LZMA_API(lzma_index *)
+ lzma_index_init(lzma_allocator *allocator)
+ {
+ 	lzma_index *i = index_init_plain(allocator);
++	if (i == NULL)
++		return NULL;
++
+ 	index_stream *s = index_stream_init(0, 0, 1, 0, allocator);
+-	if (i == NULL || s == NULL) {
+-		index_stream_end(s, allocator);
++	if (s == NULL) {
+ 		lzma_free(i, allocator);
++		return NULL;
+ 	}
+ 
+ 	index_tree_append(&i->streams, &s->node);
+-- 
+1.7.10.2
+
diff --git a/debian/patches/it-stray-N b/debian/patches/it-stray-N
new file mode 100644
index 0000000..a6c4a19
--- /dev/null
+++ b/debian/patches/it-stray-N
@@ -0,0 +1,48 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Sat, 28 May 2011 09:47:56 +0300
+Subject: Translations: Update Italian translation.
+
+Thanks to Milo Casagrande.
+---
+ po/it.po |    9 +++++----
+ 1 file changed, 5 insertions(+), 4 deletions(-)
+
+diff --git a/po/it.po b/po/it.po
+index 4f9ef0dd..4bffbe3c 100644
+--- a/po/it.po
++++ b/po/it.po
+@@ -2,13 +2,14 @@
+ # This file is in the public domain
+ # Gruppo traduzione italiano di Ubuntu-it <gruppo-traduzione@ubuntu-it.org>, 2009, 2010
+ # Lorenzo De Liso <blackz@ubuntu.com>, 2010.
+-# Milo Casagrande <milo@ubuntu.com>, 2009, 2010.
++# Milo Casagrande <milo@ubuntu.com>, 2009, 2010, 2011.
++#
+ msgid ""
+ msgstr ""
+ "Project-Id-Version: xz-utils\n"
+ "Report-Msgid-Bugs-To: lasse.collin@tukaani.org\n"
+ "POT-Creation-Date: 2010-10-23 17:48+0300\n"
+-"PO-Revision-Date: 2010-09-16 21:32+0200\n"
++"PO-Revision-Date: 2011-05-27 11:43+0200\n"
+ "Last-Translator: Milo Casagrande <milo@ubuntu.com>\n"
+ "Language-Team: Italian <tp@lists.linux.it>\n"
+ "Language: it\n"
+@@ -17,12 +18,12 @@ msgstr ""
+ "Content-Transfer-Encoding: 8bit\n"
+ "X-Launchpad-Export-Date: 2010-08-16 19:16+0000\n"
+ "X-Generator: Launchpad (build Unknown)\n"
+-"Plural-Forms: nplurals=2; plural=(n != 1);\n"
++"Plural-Forms: nplurals=2; plural=(n != 1)\n"
+ 
+ #: src/xz/args.c:333
+ #, c-format
+ msgid "%s: Unknown file format type"
+-msgstr "%s: tipo di formato del file sconosciutoN"
++msgstr "%s: tipo di formato del file sconosciuto"
+ 
+ #: src/xz/args.c:356 src/xz/args.c:364
+ #, c-format
+-- 
+1.7.10.2
+
diff --git a/debian/patches/series b/debian/patches/series
index 89ac206..958e893 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -1,3 +1,12 @@
 abi-reserved-fields
 abi-chunk-size-func
 abi-debian-soname
+cs-sparse-file
+stream_encoder-init-leak
+encoder-skip-empty-blocks
+index_init-NULL-dereference
+xz-lvv-invalid-free
+xz-lvv-invalid-free-test
+it-stray-N
+xzdiff-save-diff-status
+xzgrep-ignore-SIGPIPE
diff --git a/debian/patches/stream_encoder-init-leak b/debian/patches/stream_encoder-init-leak
new file mode 100644
index 0000000..9947182
--- /dev/null
+++ b/debian/patches/stream_encoder-init-leak
@@ -0,0 +1,34 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Sat, 2 Apr 2011 14:49:56 +0300
+Subject: liblzma: Fix a memory leak in stream_encoder.c.
+
+It leaks old filter options structures (hundred bytes or so)
+every time the lzma_stream is reinitialized. With the xz tool,
+this happens when compressing multiple files.
+---
+ src/liblzma/common/stream_encoder.c |    2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/liblzma/common/stream_encoder.c b/src/liblzma/common/stream_encoder.c
+index 48d91da7..97a7a23a 100644
+--- a/src/liblzma/common/stream_encoder.c
++++ b/src/liblzma/common/stream_encoder.c
+@@ -280,6 +280,7 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
+ 		next->end = &stream_encoder_end;
+ 		next->update = &stream_encoder_update;
+ 
++		next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
+ 		next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
+ 		next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
+ 		next->coder->index = NULL;
+@@ -289,7 +290,6 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
+ 	next->coder->sequence = SEQ_STREAM_HEADER;
+ 	next->coder->block_options.version = 0;
+ 	next->coder->block_options.check = check;
+-	next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
+ 
+ 	// Initialize the Index
+ 	lzma_index_end(next->coder->index, allocator);
+-- 
+1.7.10.2
+
diff --git a/debian/patches/xz-lvv-invalid-free b/debian/patches/xz-lvv-invalid-free
new file mode 100644
index 0000000..4959a52
--- /dev/null
+++ b/debian/patches/xz-lvv-invalid-free
@@ -0,0 +1,60 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Fri, 27 May 2011 22:25:44 +0300
+Subject: xz: Fix error handling in xz -lvv.
+
+It could do an invalid free() and read past the end
+of the uninitialized filters array.
+---
+ src/xz/list.c |   21 ++++++---------------
+ 1 file changed, 6 insertions(+), 15 deletions(-)
+
+diff --git a/src/xz/list.c b/src/xz/list.c
+index 1c93718b..98307eb2 100644
+--- a/src/xz/list.c
++++ b/src/xz/list.c
+@@ -382,14 +382,9 @@ parse_block_header(file_pair *pair, const lzma_index_iter *iter,
+ 	if (buf.u8[0] == 0)
+ 		goto data_error;
+ 
+-	lzma_block block;
+-	lzma_filter filters[LZMA_FILTERS_MAX + 1];
+-
+-	// Initialize the pointers so that they can be passed to free().
+-	for (size_t i = 0; i < ARRAY_SIZE(filters); ++i)
+-		filters[i].options = NULL;
+-
+ 	// Initialize the block structure and decode Block Header Size.
++	lzma_filter filters[LZMA_FILTERS_MAX + 1];
++	lzma_block block;
+ 	block.version = 0;
+ 	block.check = iter->stream.flags->check;
+ 	block.filters = filters;
+@@ -437,6 +432,10 @@ parse_block_header(file_pair *pair, const lzma_index_iter *iter,
+ 		break;
+ 
+ 	case LZMA_DATA_ERROR:
++		// Free the memory allocated by lzma_block_header_decode().
++		for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
++			free(filters[i].options);
++
+ 		goto data_error;
+ 
+ 	default:
+@@ -466,14 +465,6 @@ data_error:
+ 	// Show the error message.
+ 	message_error("%s: %s", pair->src_name,
+ 			message_strm(LZMA_DATA_ERROR));
+-
+-	// Free the memory allocated by lzma_block_header_decode().
+-	// This is truly needed only if we get here after a succcessful
+-	// call to lzma_block_header_decode() but it doesn't hurt to
+-	// always do it.
+-	for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+-		free(filters[i].options);
+-
+ 	return true;
+ }
+ 
+-- 
+1.7.10.2
+
diff --git a/debian/patches/xz-lvv-invalid-free-test b/debian/patches/xz-lvv-invalid-free-test
new file mode 100644
index 0000000..c68984b
--- /dev/null
+++ b/debian/patches/xz-lvv-invalid-free-test
@@ -0,0 +1,30 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Sat, 28 May 2011 08:46:04 +0300
+Subject: Tests: Add a test file for the bug in the previous commit.
+
+[jrnieder@gmail.com: leaving out bad-1-block_header-6.xz because
+ source format 3.0 (quilt) does not permit binary patches]
+---
+ tests/files/README                  |    4 ++++
+ tests/files/bad-1-block_header-6.xz |  Bin 0 -> 72 bytes
+ 2 files changed, 4 insertions(+)
+ create mode 100644 tests/files/bad-1-block_header-6.xz
+
+diff --git a/tests/files/README b/tests/files/README
+index 392ff768..108ce8fb 100644
+--- a/tests/files/README
++++ b/tests/files/README
+@@ -184,6 +184,10 @@
+ 
+     bad-1-block_header-5.xz has zero as Compressed Size in Block Header.
+ 
++    bad-1-block_header-6.xz has corrupt Block Header which may crash
++    xz -lvv in XZ Utils 5.0.3 and earlier. It was fixed in the commit
++    c0297445064951807803457dca1611b3c47e7f0f.
++
+     bad-2-index-1.xz has wrong Unpadded Sizes in Index.
+ 
+     bad-2-index-2.xz has wrong Uncompressed Sizes in Index.
+-- 
+1.7.10.2
+
diff --git a/debian/patches/xzdiff-save-diff-status b/debian/patches/xzdiff-save-diff-status
new file mode 100644
index 0000000..74048b1
--- /dev/null
+++ b/debian/patches/xzdiff-save-diff-status
@@ -0,0 +1,123 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Sun, 31 Jul 2011 11:01:47 +0300
+Subject: Fix exit status of "xzdiff foo.xz bar.xz".
+
+xzdiff was clobbering the exit status from diff in a case
+statement used to analyze the exit statuses from "xz" when
+its operands were two compressed files. Save and restore
+diff's exit status to fix this.
+
+The bug is inherited from zdiff in GNU gzip and was fixed
+there on 2009-10-09.
+
+Thanks to Jonathan Nieder for the patch and
+to Peter Pallinger for reporting the bug.
+---
+ src/scripts/xzdiff.in |    2 ++
+ tests/Makefile.am     |    4 +++-
+ tests/test_scripts.sh |   54 +++++++++++++++++++++++++++++++++++++++++++++++++
+ 3 files changed, 59 insertions(+), 1 deletion(-)
+ create mode 100755 tests/test_scripts.sh
+
+diff --git a/src/scripts/xzdiff.in b/src/scripts/xzdiff.in
+index 2d6e5da4..03428d1a 100644
+--- a/src/scripts/xzdiff.in
++++ b/src/scripts/xzdiff.in
+@@ -120,10 +120,12 @@ elif test $# -eq 2; then
+                 ( ($xz2 -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
+                 eval "$cmp" /dev/fd/5 - >&3) 5<&0
+             )
++            cmp_status=$?
+             case $xz_status in
+               *[1-9]*) xz_status=1;;
+               *) xz_status=0;;
+             esac
++            (exit $cmp_status)
+           else
+             F=`expr "/$2" : '.*/\(.*\)[-.][ablmtxz2]*$'` || F=$prog
+             tmp=
+diff --git a/tests/Makefile.am b/tests/Makefile.am
+index d0807ae4..0469264a 100644
+--- a/tests/Makefile.am
++++ b/tests/Makefile.am
+@@ -10,6 +10,7 @@ EXTRA_DIST = \
+ 	tests.h \
+ 	test_files.sh \
+ 	test_compress.sh \
++	test_scripts.sh \
+ 	bcj_test.c \
+ 	compress_prepared_bcj_sparc \
+ 	compress_prepared_bcj_x86
+@@ -42,7 +43,8 @@ TESTS = \
+ 	test_block_header \
+ 	test_index \
+ 	test_files.sh \
+-	test_compress.sh
++	test_compress.sh \
++	test_scripts.sh
+ 
+ clean-local:
+ 	-rm -f compress_generated_*
+diff --git a/tests/test_scripts.sh b/tests/test_scripts.sh
+new file mode 100755
+index 00000000..891fc76a
+--- /dev/null
++++ b/tests/test_scripts.sh
+@@ -0,0 +1,54 @@
++#!/bin/sh
++
++###############################################################################
++#
++# Author: Jonathan Nieder
++#
++# This file has been put into the public domain.
++# You can do whatever you want with this file.
++#
++###############################################################################
++
++# If scripts weren't built, this test is skipped.
++XZ=../src/xz/xz
++XZDIFF=../src/scripts/xzdiff
++test -x "$XZ" || XZ=
++test -r "$XZDIFF" || XZDIFF=
++if test -z "$XZ" || test -z "$XZDIFF"; then
++	(exit 77)
++	exit 77
++fi
++
++PATH=`pwd`/../src/xz:$PATH
++export PATH
++
++preimage=$srcdir/files/good-1-check-crc32.xz
++samepostimage=$srcdir/files/good-1-check-crc64.xz
++otherpostimage=$srcdir/files/good-1-lzma2-1.xz
++
++sh "$XZDIFF" "$preimage" "$samepostimage" >/dev/null
++status=$?
++if test "$status" != 0 ; then
++	echo "xzdiff with no changes exited with status $status != 0"
++	(exit 1)
++	exit 1
++fi
++
++sh "$XZDIFF" "$preimage" "$otherpostimage" >/dev/null
++status=$?
++if test "$status" != 1 ; then
++	echo "xzdiff with changes exited with status $status != 1"
++	(exit 1)
++	exit 1
++fi
++
++sh "$XZDIFF" "$preimage" "$srcdir/files/missing.xz" >/dev/null 2>&1
++status=$?
++if test "$status" != 2 ; then
++	echo "xzdiff with missing operand exited with status $status != 2"
++	(exit 1)
++	exit 1
++fi
++
++(exit 0)
++exit 0
+-- 
+1.7.10.2
+
diff --git a/debian/patches/xzgrep-ignore-SIGPIPE b/debian/patches/xzgrep-ignore-SIGPIPE
new file mode 100644
index 0000000..57086c8
--- /dev/null
+++ b/debian/patches/xzgrep-ignore-SIGPIPE
@@ -0,0 +1,36 @@
+From: Lasse Collin <lasse.collin@tukaani.org>
+Date: Wed, 22 Feb 2012 14:02:34 +0200
+Subject: Fix exit status of xzgrep when grepping binary files.
+
+When grepping binary files, grep may exit before it has
+read all the input. In this case, gzip -q returns 2 (eating
+SIGPIPE), but xz and bzip2 show SIGPIPE as the exit status
+(e.g. 141). This causes wrong exit status when grepping
+xz- or bzip2-compressed binary files.
+
+The fix checks for the special exit status that indicates SIGPIPE.
+It uses kill -l which should be supported everywhere since it
+is in both SUSv2 (1997) and POSIX.1-2008.
+
+Thanks to James Buren for the bug report.
+---
+ src/scripts/xzgrep.in |    3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in
+index bfa9fb53..cbc6b238 100644
+--- a/src/scripts/xzgrep.in
++++ b/src/scripts/xzgrep.in
+@@ -190,7 +190,8 @@ for i; do
+     fi >&3 5>&-
+   )
+   r=$?
+-  test "$xz_status" -eq 0 || test "$xz_status" -eq 2 || r=2
++  test "$xz_status" -eq 0 || test "$xz_status" -eq 2 \
++      || test "$(kill -l "$xz_status" 2> /dev/null)" = "PIPE" || r=2
+   test $res -lt $r && res=$r
+ done
+ exit $res
+-- 
+1.7.10.2
+
diff --git a/debian/rules b/debian/rules
index ec1e811..9612d54 100755
--- a/debian/rules
+++ b/debian/rules
@@ -6,6 +6,7 @@
 override_dh_auto_clean:
 	dh_auto_clean --builddirectory debian/xzdec-build
 	dh_auto_clean --builddirectory debian/normal-build
+	chmod -x tests/test_scripts.sh
 	sh debian/clean.sh
 
 override_dh_auto_configure:
@@ -31,6 +32,7 @@
 	fi
 
 override_dh_auto_test:
+	chmod +x tests/test_scripts.sh
 	$(MAKE) -C debian/normal-build check
 	$(MAKE) -C debian/xzdec-build/tests create_compress_files
 	$(MAKE) -C debian/xzdec-build/tests \
diff --git a/debian/source/include-binaries b/debian/source/include-binaries
new file mode 100644
index 0000000..d4f7e04
--- /dev/null
+++ b/debian/source/include-binaries
@@ -0,0 +1 @@
+tests/files/bad-1-block_header-6.xz
diff --git a/debian/symbols b/debian/symbols
index 989b065..82322ba 100644
--- a/debian/symbols
+++ b/debian/symbols
@@ -20,7 +20,7 @@
  lzma_code@Base 4.999.9beta
  lzma_crc32@Base 4.999.9beta
  lzma_crc64@Base 4.999.9beta
- lzma_easy_buffer_encode@Base 4.999.9beta
+ lzma_easy_buffer_encode@Base 5.0.0-2.1~
  lzma_easy_decoder_memusage@Base 4.999.9beta
  lzma_easy_encoder@Base 4.999.9beta
  lzma_easy_encoder_memusage@Base 4.999.9beta
@@ -81,7 +81,7 @@
  lzma_raw_encoder_memusage@Base 4.999.9beta
  lzma_stream_buffer_bound@Base 4.999.9beta
  lzma_stream_buffer_decode@Base 4.999.9beta
- lzma_stream_buffer_encode@Base 4.999.9beta
+ lzma_stream_buffer_encode@Base 5.0.0-2.1~
  lzma_stream_decoder@Base 4.999.9beta
  lzma_stream_encoder@Base 4.999.9beta
  lzma_stream_flags_compare@Base 4.999.9beta
diff --git a/po/cs.po b/po/cs.po
index 5bebf6e..3ef3fcc 100644
--- a/po/cs.po
+++ b/po/cs.po
@@ -7,7 +7,7 @@
 "Project-Id-Version: xz-utils\n"
 "Report-Msgid-Bugs-To: lasse.collin@tukaani.org\n"
 "POT-Creation-Date: 2010-10-23 17:48+0300\n"
-"PO-Revision-Date: 2010-09-17 18:54+0200\n"
+"PO-Revision-Date: 2010-12-03 11:32+0100\n"
 "Last-Translator: Marek Černocký <marek@manet.cz>\n"
 "Language-Team: Czech <diskuze@lists.l10n.cz>\n"
 "Language: cs\n"
@@ -153,7 +153,7 @@
 #: src/xz/file_io.c:762 src/xz/file_io.c:946
 #, c-format
 msgid "%s: Seeking failed when trying to create a sparse file: %s"
-msgstr "%s: Selhalo nastavení pozice při pokusu o vytvoření záložního souboru: %s"
+msgstr "%s: Selhalo nastavení pozice při pokusu o vytvoření souboru řídké matice: %s"
 
 #: src/xz/file_io.c:821
 #, c-format
@@ -517,7 +517,7 @@
 "                      filenames must be terminated with the newline character\n"
 "      --files0[=FILE] like --files but use the null character as terminator"
 msgstr ""
-"     --no-sparse       nevytvářet při dekomprimaci záložní soubory\n"
+"     --no-sparse       nevytvářet při dekomprimaci soubory řídkých matic\n"
 " -S, --suffix=.PRIP    použít u komprimovaných souborů příponu „.PRIP“\n"
 "     --files[=SOUBOR]  číst názvy souborů, které se mají zpracovat, ze SOUBORu;\n"
 "                       pokud není SOUBOR zadán, čte se ze standardního vstupu;\n"
diff --git a/po/it.po b/po/it.po
index 4f9ef0d..4bffbe3 100644
--- a/po/it.po
+++ b/po/it.po
@@ -2,13 +2,14 @@
 # This file is in the public domain
 # Gruppo traduzione italiano di Ubuntu-it <gruppo-traduzione@ubuntu-it.org>, 2009, 2010
 # Lorenzo De Liso <blackz@ubuntu.com>, 2010.
-# Milo Casagrande <milo@ubuntu.com>, 2009, 2010.
+# Milo Casagrande <milo@ubuntu.com>, 2009, 2010, 2011.
+#
 msgid ""
 msgstr ""
 "Project-Id-Version: xz-utils\n"
 "Report-Msgid-Bugs-To: lasse.collin@tukaani.org\n"
 "POT-Creation-Date: 2010-10-23 17:48+0300\n"
-"PO-Revision-Date: 2010-09-16 21:32+0200\n"
+"PO-Revision-Date: 2011-05-27 11:43+0200\n"
 "Last-Translator: Milo Casagrande <milo@ubuntu.com>\n"
 "Language-Team: Italian <tp@lists.linux.it>\n"
 "Language: it\n"
@@ -17,12 +18,12 @@
 "Content-Transfer-Encoding: 8bit\n"
 "X-Launchpad-Export-Date: 2010-08-16 19:16+0000\n"
 "X-Generator: Launchpad (build Unknown)\n"
-"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\n"
 
 #: src/xz/args.c:333
 #, c-format
 msgid "%s: Unknown file format type"
-msgstr "%s: tipo di formato del file sconosciutoN"
+msgstr "%s: tipo di formato del file sconosciuto"
 
 #: src/xz/args.c:356 src/xz/args.c:364
 #, c-format
diff --git a/src/liblzma/common/index.c b/src/liblzma/common/index.c
index ddb9d36..9af4bc1 100644
--- a/src/liblzma/common/index.c
+++ b/src/liblzma/common/index.c
@@ -398,10 +398,13 @@
 lzma_index_init(lzma_allocator *allocator)
 {
 	lzma_index *i = index_init_plain(allocator);
+	if (i == NULL)
+		return NULL;
+
 	index_stream *s = index_stream_init(0, 0, 1, 0, allocator);
-	if (i == NULL || s == NULL) {
-		index_stream_end(s, allocator);
+	if (s == NULL) {
 		lzma_free(i, allocator);
+		return NULL;
 	}
 
 	index_tree_append(&i->streams, &s->node);
diff --git a/src/liblzma/common/stream_buffer_encoder.c b/src/liblzma/common/stream_buffer_encoder.c
index f727d85..0542c30 100644
--- a/src/liblzma/common/stream_buffer_encoder.c
+++ b/src/liblzma/common/stream_buffer_encoder.c
@@ -81,26 +81,32 @@
 
 	out_pos += LZMA_STREAM_HEADER_SIZE;
 
-	// Block
+	// Encode a Block but only if there is at least one byte of input.
 	lzma_block block = {
 		.version = 0,
 		.check = check,
 		.filters = filters,
 	};
 
-	return_if_error(lzma_block_buffer_encode(&block, allocator,
-			in, in_size, out, &out_pos, out_size));
+	if (in_size > 0)
+		return_if_error(lzma_block_buffer_encode(&block, allocator,
+				in, in_size, out, &out_pos, out_size));
 
 	// Index
 	{
-		// Create an Index with one Record.
+		// Create an Index. It will have one Record if there was
+		// at least one byte of input to encode. Otherwise the
+		// Index will be empty.
 		lzma_index *i = lzma_index_init(allocator);
 		if (i == NULL)
 			return LZMA_MEM_ERROR;
 
-		lzma_ret ret = lzma_index_append(i, allocator,
-				lzma_block_unpadded_size(&block),
-				block.uncompressed_size);
+		lzma_ret ret = LZMA_OK;
+
+		if (in_size > 0)
+			ret = lzma_index_append(i, allocator,
+					lzma_block_unpadded_size(&block),
+					block.uncompressed_size);
 
 		// If adding the Record was successful, encode the Index
 		// and get its size which will be stored into Stream Footer.
diff --git a/src/liblzma/common/stream_encoder.c b/src/liblzma/common/stream_encoder.c
index 48d91da..97a7a23 100644
--- a/src/liblzma/common/stream_encoder.c
+++ b/src/liblzma/common/stream_encoder.c
@@ -280,6 +280,7 @@
 		next->end = &stream_encoder_end;
 		next->update = &stream_encoder_update;
 
+		next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
 		next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
 		next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
 		next->coder->index = NULL;
@@ -289,7 +290,6 @@
 	next->coder->sequence = SEQ_STREAM_HEADER;
 	next->coder->block_options.version = 0;
 	next->coder->block_options.check = check;
-	next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
 
 	// Initialize the Index
 	lzma_index_end(next->coder->index, allocator);
diff --git a/src/scripts/xzdiff.in b/src/scripts/xzdiff.in
index 2d6e5da..03428d1 100644
--- a/src/scripts/xzdiff.in
+++ b/src/scripts/xzdiff.in
@@ -120,10 +120,12 @@
                 ( ($xz2 -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
                 eval "$cmp" /dev/fd/5 - >&3) 5<&0
             )
+            cmp_status=$?
             case $xz_status in
               *[1-9]*) xz_status=1;;
               *) xz_status=0;;
             esac
+            (exit $cmp_status)
           else
             F=`expr "/$2" : '.*/\(.*\)[-.][ablmtxz2]*$'` || F=$prog
             tmp=
diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in
index bfa9fb5..cbc6b23 100644
--- a/src/scripts/xzgrep.in
+++ b/src/scripts/xzgrep.in
@@ -190,7 +190,8 @@
     fi >&3 5>&-
   )
   r=$?
-  test "$xz_status" -eq 0 || test "$xz_status" -eq 2 || r=2
+  test "$xz_status" -eq 0 || test "$xz_status" -eq 2 \
+      || test "$(kill -l "$xz_status" 2> /dev/null)" = "PIPE" || r=2
   test $res -lt $r && res=$r
 done
 exit $res
diff --git a/src/xz/list.c b/src/xz/list.c
index 1c93718..98307eb 100644
--- a/src/xz/list.c
+++ b/src/xz/list.c
@@ -382,14 +382,9 @@
 	if (buf.u8[0] == 0)
 		goto data_error;
 
-	lzma_block block;
-	lzma_filter filters[LZMA_FILTERS_MAX + 1];
-
-	// Initialize the pointers so that they can be passed to free().
-	for (size_t i = 0; i < ARRAY_SIZE(filters); ++i)
-		filters[i].options = NULL;
-
 	// Initialize the block structure and decode Block Header Size.
+	lzma_filter filters[LZMA_FILTERS_MAX + 1];
+	lzma_block block;
 	block.version = 0;
 	block.check = iter->stream.flags->check;
 	block.filters = filters;
@@ -437,6 +432,10 @@
 		break;
 
 	case LZMA_DATA_ERROR:
+		// Free the memory allocated by lzma_block_header_decode().
+		for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+			free(filters[i].options);
+
 		goto data_error;
 
 	default:
@@ -466,14 +465,6 @@
 	// Show the error message.
 	message_error("%s: %s", pair->src_name,
 			message_strm(LZMA_DATA_ERROR));
-
-	// Free the memory allocated by lzma_block_header_decode().
-	// This is truly needed only if we get here after a succcessful
-	// call to lzma_block_header_decode() but it doesn't hurt to
-	// always do it.
-	for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
-		free(filters[i].options);
-
 	return true;
 }
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d0807ae..0469264 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -10,6 +10,7 @@
 	tests.h \
 	test_files.sh \
 	test_compress.sh \
+	test_scripts.sh \
 	bcj_test.c \
 	compress_prepared_bcj_sparc \
 	compress_prepared_bcj_x86
@@ -42,7 +43,8 @@
 	test_block_header \
 	test_index \
 	test_files.sh \
-	test_compress.sh
+	test_compress.sh \
+	test_scripts.sh
 
 clean-local:
 	-rm -f compress_generated_*
diff --git a/tests/files/README b/tests/files/README
index 392ff76..108ce8f 100644
--- a/tests/files/README
+++ b/tests/files/README
@@ -184,6 +184,10 @@
 
     bad-1-block_header-5.xz has zero as Compressed Size in Block Header.
 
+    bad-1-block_header-6.xz has corrupt Block Header which may crash
+    xz -lvv in XZ Utils 5.0.3 and earlier. It was fixed in the commit
+    c0297445064951807803457dca1611b3c47e7f0f.
+
     bad-2-index-1.xz has wrong Unpadded Sizes in Index.
 
     bad-2-index-2.xz has wrong Uncompressed Sizes in Index.
diff --git a/tests/files/bad-1-block_header-6.xz b/tests/files/bad-1-block_header-6.xz
new file mode 100644
index 0000000..ecf0ff0
--- /dev/null
+++ b/tests/files/bad-1-block_header-6.xz
Binary files differ
diff --git a/tests/test_scripts.sh b/tests/test_scripts.sh
new file mode 100755
index 0000000..891fc76
--- /dev/null
+++ b/tests/test_scripts.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+###############################################################################
+#
+# Author: Jonathan Nieder
+#
+# This file has been put into the public domain.
+# You can do whatever you want with this file.
+#
+###############################################################################
+
+# If scripts weren't built, this test is skipped.
+XZ=../src/xz/xz
+XZDIFF=../src/scripts/xzdiff
+test -x "$XZ" || XZ=
+test -r "$XZDIFF" || XZDIFF=
+if test -z "$XZ" || test -z "$XZDIFF"; then
+	(exit 77)
+	exit 77
+fi
+
+PATH=`pwd`/../src/xz:$PATH
+export PATH
+
+preimage=$srcdir/files/good-1-check-crc32.xz
+samepostimage=$srcdir/files/good-1-check-crc64.xz
+otherpostimage=$srcdir/files/good-1-lzma2-1.xz
+
+sh "$XZDIFF" "$preimage" "$samepostimage" >/dev/null
+status=$?
+if test "$status" != 0 ; then
+	echo "xzdiff with no changes exited with status $status != 0"
+	(exit 1)
+	exit 1
+fi
+
+sh "$XZDIFF" "$preimage" "$otherpostimage" >/dev/null
+status=$?
+if test "$status" != 1 ; then
+	echo "xzdiff with changes exited with status $status != 1"
+	(exit 1)
+	exit 1
+fi
+
+sh "$XZDIFF" "$preimage" "$srcdir/files/missing.xz" >/dev/null 2>&1
+status=$?
+if test "$status" != 2 ; then
+	echo "xzdiff with missing operand exited with status $status != 2"
+	(exit 1)
+	exit 1
+fi
+
+(exit 0)
+exit 0