Skip to content

Commit 51988e1

Browse files
author
Commitfest Bot
committed
[CF 5874] v2 - Fix decompression bug in astreamer_lz4
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5874 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAMEv5_u0BWA+tcczoKYdBXMtkwHYfgZLoe0GsH_8pqNM6bc2iQ@mail.gmail.com Author(s): Mikhail Gribkov
2 parents d81dcc8 + 8f913d4 commit 51988e1

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/bin/pg_verifybackup/t/008_untar.pl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
$primary->init(allows_streaming => 1);
1717
$primary->start;
1818

19+
# Create randomly filled file at the root of the data folder to check possible
20+
# decompression issues. There was a bug reported, that didn't show up with
21+
# a regular data folder contents. The bug itself was only detected for lz4
22+
# decompression with levels 0 and 1, and a minimal file size about 512k. It
23+
# doesn't seem worthy checking too wide variety of combinations, but at least
24+
# we'll add this file for all compression methods checks.
25+
# (https://www.postgresql.org/message-id/CAMEv5_uQS1Hg6KCaEP2JkrTBbZ-nXQhxomWrhYQvbdzR-zy-wA%40mail.gmail.com)
26+
my $junk_data = $primary->safe_psql(
27+
'postgres', qq(
28+
SELECT string_agg(encode(sha256(i::bytea), 'hex'), '')
29+
FROM generate_series(1, 10240) s(i);));
30+
my $data_dir = $primary->data_dir;
31+
my $junk_file = "$data_dir/junk";
32+
open my $jf, '>', $junk_file
33+
or die "Could not create junk file: $!";
34+
print $jf $junk_data;
35+
close $jf;
36+
1937
# Create a tablespace directory.
2038
my $source_ts_path = PostgreSQL::Test::Utils::tempdir_short();
2139

@@ -52,6 +70,12 @@
5270
'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
5371
'enabled' => check_pg_config("#define USE_LZ4 1")
5472
},
73+
{
74+
'compression_method' => 'lz4',
75+
'backup_flags' => [ '--compress', 'server-lz4:5' ],
76+
'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
77+
'enabled' => check_pg_config("#define USE_LZ4 1")
78+
},
5579
{
5680
'compression_method' => 'zstd',
5781
'backup_flags' => [ '--compress', 'server-zstd' ],
@@ -111,5 +135,4 @@
111135
rmtree($extract_path);
112136
}
113137
}
114-
115138
done_testing();

src/bin/pg_verifybackup/t/010_client_untar.pl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@
1515
$primary->init(allows_streaming => 1);
1616
$primary->start;
1717

18+
# Create randomly filled file at the root of the data folder to check possible
19+
# decompression issues. There was a bug reported, that didn't show up with
20+
# a regular data folder contents. The bug itself was only detected for lz4
21+
# decompression with levels 0 and 1, and a minimal file size about 512k. It
22+
# doesn't seem worthy checking too wide variety of combinations, but at least
23+
# we'll add this file for all compression methods checks.
24+
# (https://www.postgresql.org/message-id/CAMEv5_uQS1Hg6KCaEP2JkrTBbZ-nXQhxomWrhYQvbdzR-zy-wA%40mail.gmail.com)
25+
my $junk_data = $primary->safe_psql(
26+
'postgres', qq(
27+
SELECT string_agg(encode(sha256(i::bytea), 'hex'), '')
28+
FROM generate_series(1, 10240) s(i);));
29+
my $data_dir = $primary->data_dir;
30+
my $junk_file = "$data_dir/junk";
31+
open my $jf, '>', $junk_file
32+
or die "Could not create junk file: $!";
33+
print $jf $junk_data;
34+
close $jf;
35+
1836
my $backup_path = $primary->backup_dir . '/client-backup';
1937
my $extract_path = $primary->backup_dir . '/extracted-backup';
2038

@@ -37,6 +55,12 @@
3755
'backup_archive' => 'base.tar.lz4',
3856
'enabled' => check_pg_config("#define USE_LZ4 1")
3957
},
58+
{
59+
'compression_method' => 'lz4',
60+
'backup_flags' => [ '--compress', 'client-lz4:1' ],
61+
'backup_archive' => 'base.tar.lz4',
62+
'enabled' => check_pg_config("#define USE_LZ4 1")
63+
},
4064
{
4165
'compression_method' => 'zstd',
4266
'backup_flags' => [ '--compress', 'client-zstd:5' ],
@@ -125,5 +149,4 @@
125149
rmtree($backup_path);
126150
}
127151
}
128-
129152
done_testing();

src/fe_utils/astreamer_lz4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ astreamer_lz4_decompressor_content(astreamer *streamer,
322322

323323
mystreamer = (astreamer_lz4_frame *) streamer;
324324
next_in = (uint8 *) data;
325-
next_out = (uint8 *) mystreamer->base.bbs_buffer.data;
325+
next_out = (uint8 *) mystreamer->base.bbs_buffer.data + mystreamer->bytes_written;
326326
avail_in = len;
327-
avail_out = mystreamer->base.bbs_buffer.maxlen;
327+
avail_out = mystreamer->base.bbs_buffer.maxlen - mystreamer->bytes_written;
328328

329329
while (avail_in > 0)
330330
{

0 commit comments

Comments
 (0)