Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lang/perl/lib/Avro/BinaryDecoder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use warnings;
use Config;
use Encode();
use Error::Simple;
use Fcntl();
use Avro::Schema;

our $VERSION = '++MODULE_VERSION++';
Expand Down Expand Up @@ -124,7 +125,7 @@ sub skip_bytes {
my $class = shift;
my $reader = pop;
my $size = decode_long($class, undef, undef, $reader);
$reader->seek($size, 0);
$reader->seek($size, Fcntl->SEEK_CUR);
return;
}

Expand Down Expand Up @@ -350,7 +351,7 @@ sub decode_union {
sub skip_fixed {
my $class = shift;
my ($schema, $reader) = @_;
$reader->seek($schema->size, 0);
$reader->seek($schema->size, Fcntl->SEEK_CUR);
}

## 1.3.2 Fixed instances are encoded using the number of bytes declared in the
Expand Down
57 changes: 57 additions & 0 deletions lang/perl/t/03_bin_decode.t
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,63 @@ EOJ
} "Avro::Schema::Error::Mismatch", "no default value!";
}

## skipping a writer field must advance relative to the current position, so a
## field that follows a skipped bytes/string/fixed field still decodes correctly
{
my $w_schema = Avro::Schema->parse(<<EOJ);
{ "type": "record", "name": "skiptest",
"fields" : [
{"name": "skipped", "type": "bytes"},
{"name": "keep", "type": "long"} ]}
EOJ
my $r_schema = Avro::Schema->parse(<<EOJ);
{ "type": "record", "name": "skiptest",
"fields" : [
{"name": "keep", "type": "long"} ]}
EOJ
my $enc = '';
Avro::BinaryEncoder->encode(
schema => $w_schema,
data => { skipped => "hello", keep => 42 },
emit_cb => sub { $enc .= ${ $_[0] } },
);
open my $reader, '<', \$enc or die "Cannot open memory file: $!";
my $dec = Avro::BinaryDecoder->decode(
writer_schema => $w_schema,
reader_schema => $r_schema,
reader => $reader,
);
is $dec->{keep}, 42, "field after a skipped bytes field decodes correctly";

my $wf_schema = Avro::Schema->parse(<<EOJ);
{ "type": "record", "name": "skipfixed",
"fields" : [
{"name": "before", "type": "long"},
{"name": "skipped", "type": {"type":"fixed","name":"F","size":4}},
{"name": "keep", "type": "long"} ]}
EOJ
my $rf_schema = Avro::Schema->parse(<<EOJ);
{ "type": "record", "name": "skipfixed",
"fields" : [
{"name": "before", "type": "long"},
{"name": "keep", "type": "long"} ]}
EOJ
$enc = '';
Avro::BinaryEncoder->encode(
schema => $wf_schema,
data => { before => 9, skipped => "abcd", keep => 7 },
emit_cb => sub { $enc .= ${ $_[0] } },
);
open $reader, '<', \$enc or die "Cannot open memory file: $!";
$dec = Avro::BinaryDecoder->decode(
writer_schema => $wf_schema,
reader_schema => $rf_schema,
reader => $reader,
);
is $dec->{before}, 9, "field before a skipped fixed field decodes correctly";
is $dec->{keep}, 7, "field after a skipped fixed field decodes correctly";
}

## union resolution
{
my $w_schema = Avro::Schema->parse(<<EOP);
Expand Down