From 3369a3b49b0bc0a4205062e45623af297240c8c6 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 2 Jul 2025 13:48:36 +0900
Subject: [PATCH] Fix bug in archive streamer with LZ4 decompression

When decompressing some input data, the calculation for the initial
starting point and the initial size were incorrect, potentially leading
to failures when decompressing contents with LZ4.  These initialization
points are fixed in this commit, bringing the logic closer to what
exists for gzip and zstd.

The contents of the compressed data is clear (for example backups taken
with LZ4 can still be decompressed with a "lz4" command), only the
decompression part reading the input data was impacted by this issue.

This code path impacts pg_basebackup and pg_verifybackup, which can use
the LZ4 decompression routines with an archive streamer, or any tools
that try to use the archive streamers in src/fe_utils/.

The issue is easier to reproduce with files that have a low-compression
rate, like ones filled with random data, for a size of at least 512kB,
but this could happen with anything as long as it is stored in a data
folder.  Some tests are added based on this idea, with a file filled
with random bytes grabbed from the backend, written at the root of the
data folder.  This is proving good enough to reproduce the original
problem.

Author: Mikhail Gribkov <youzhick@gmail.com>
Discussion: https://postgr.es/m/CAMEv5_uQS1Hg6KCaEP2JkrTBbZ-nXQhxomWrhYQvbdzR-zy-wA@mail.gmail.com
Backpatch-through: 15
---
 src/bin/pg_verifybackup/t/008_untar.pl        | 22 +++++++++++++++++++
 src/bin/pg_verifybackup/t/010_client_untar.pl | 22 +++++++++++++++++++
 src/fe_utils/astreamer_lz4.c                  |  4 ++--
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_verifybackup/t/008_untar.pl b/src/bin/pg_verifybackup/t/008_untar.pl
index deed3ec247..bc3d6b352a 100644
--- a/src/bin/pg_verifybackup/t/008_untar.pl
+++ b/src/bin/pg_verifybackup/t/008_untar.pl
@@ -16,6 +16,22 @@ my $primary = PostgreSQL::Test::Cluster->new('primary');
 $primary->init(allows_streaming => 1);
 $primary->start;
 
+# Create file with some random data and an arbitrary size, useful to check
+# the solidity of the compression and decompression logic.  The size of the
+# file is chosen to be around 640kB.  This has proven to be large enough to
+# detect some issues related to LZ4, and low enough to not impact the runtime
+# of the test significantly.
+my $junk_data = $primary->safe_psql(
+	'postgres', qq(
+		SELECT string_agg(encode(sha256(i::bytea), 'hex'), '')
+		FROM generate_series(1, 10240) s(i);));
+my $data_dir = $primary->data_dir;
+my $junk_file = "$data_dir/junk";
+open my $jf, '>', $junk_file
+  or die "Could not create junk file: $!";
+print $jf $junk_data;
+close $jf;
+
 # Create a tablespace directory.
 my $source_ts_path = PostgreSQL::Test::Utils::tempdir_short();
 
@@ -52,6 +68,12 @@ my @test_configuration = (
 		'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
 		'enabled' => check_pg_config("#define USE_LZ4 1")
 	},
+	{
+		'compression_method' => 'lz4',
+		'backup_flags' => [ '--compress', 'server-lz4:5' ],
+		'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
+		'enabled' => check_pg_config("#define USE_LZ4 1")
+	},
 	{
 		'compression_method' => 'zstd',
 		'backup_flags' => [ '--compress', 'server-zstd' ],
diff --git a/src/bin/pg_verifybackup/t/010_client_untar.pl b/src/bin/pg_verifybackup/t/010_client_untar.pl
index d8d2b06c7e..b62faeb5ac 100644
--- a/src/bin/pg_verifybackup/t/010_client_untar.pl
+++ b/src/bin/pg_verifybackup/t/010_client_untar.pl
@@ -15,6 +15,22 @@ my $primary = PostgreSQL::Test::Cluster->new('primary');
 $primary->init(allows_streaming => 1);
 $primary->start;
 
+# Create file with some random data and an arbitrary size, useful to check
+# the solidity of the compression and decompression logic.  The size of the
+# file is chosen to be around 640kB.  This has proven to be large enough to
+# detect some issues related to LZ4, and low enough to not impact the runtime
+# of the test significantly.
+my $junk_data = $primary->safe_psql(
+	'postgres', qq(
+		SELECT string_agg(encode(sha256(i::bytea), 'hex'), '')
+		FROM generate_series(1, 10240) s(i);));
+my $data_dir = $primary->data_dir;
+my $junk_file = "$data_dir/junk";
+open my $jf, '>', $junk_file
+  or die "Could not create junk file: $!";
+print $jf $junk_data;
+close $jf;
+
 my $backup_path = $primary->backup_dir . '/client-backup';
 my $extract_path = $primary->backup_dir . '/extracted-backup';
 
@@ -37,6 +53,12 @@ my @test_configuration = (
 		'backup_archive' => 'base.tar.lz4',
 		'enabled' => check_pg_config("#define USE_LZ4 1")
 	},
+	{
+		'compression_method' => 'lz4',
+		'backup_flags' => [ '--compress', 'client-lz4:1' ],
+		'backup_archive' => 'base.tar.lz4',
+		'enabled' => check_pg_config("#define USE_LZ4 1")
+	},
 	{
 		'compression_method' => 'zstd',
 		'backup_flags' => [ '--compress', 'client-zstd:5' ],
diff --git a/src/fe_utils/astreamer_lz4.c b/src/fe_utils/astreamer_lz4.c
index 781aaf99f3..5f581d1de3 100644
--- a/src/fe_utils/astreamer_lz4.c
+++ b/src/fe_utils/astreamer_lz4.c
@@ -322,9 +322,9 @@ astreamer_lz4_decompressor_content(astreamer *streamer,
 
 	mystreamer = (astreamer_lz4_frame *) streamer;
 	next_in = (uint8 *) data;
-	next_out = (uint8 *) mystreamer->base.bbs_buffer.data;
+	next_out = (uint8 *) mystreamer->base.bbs_buffer.data + mystreamer->bytes_written;
 	avail_in = len;
-	avail_out = mystreamer->base.bbs_buffer.maxlen;
+	avail_out = mystreamer->base.bbs_buffer.maxlen - mystreamer->bytes_written;
 
 	while (avail_in > 0)
 	{
-- 
2.39.5

