vici: Add error handling to message parsing in Perl bindings

This commit is contained in:
Tobias Brunner 2018-06-05 17:49:42 +02:00
parent d6aa6537e7
commit de4c3d2e76

View File

@ -70,23 +70,18 @@ sub parse {
until ( eof $fd ) until ( eof $fd )
{ {
read $fd, $data, 1; my $type = unpack('C', read_data($fd, 1));
my $type = unpack('C', $data);
if ( $type == SECTION_END ) if ( $type == SECTION_END )
{ {
return; return;
} }
read $fd, $data, 1; my $key = read_len_data($fd, 1);
my $length = unpack('C', $data);
read $fd, my $key, $length;
if ( $type == KEY_VALUE ) if ( $type == KEY_VALUE )
{ {
read $fd, $data, 2; my $value = read_len_data($fd, 2);
my $length = unpack('n', $data);
read $fd, my $value, $length;
$hash->{$key} = $value; $hash->{$key} = $value;
} }
elsif ( $type == SECTION_START ) elsif ( $type == SECTION_START )
@ -102,14 +97,11 @@ sub parse {
while ( !eof($fd) and $more ) while ( !eof($fd) and $more )
{ {
read $fd, $data, 1; my $type = unpack('C', read_data($fd, 1));
my $type = unpack('C', $data);
if ( $type == LIST_ITEM ) if ( $type == LIST_ITEM )
{ {
read $fd, $data, 2; my $value = read_len_data($fd, 2);
my $length = unpack('n', $data);
read $fd, my $value, $length;
push(@list, $value); push(@list, $value);
} }
elsif ( $type == LIST_END ) elsif ( $type == LIST_END )
@ -130,6 +122,26 @@ sub parse {
} }
} }
sub read_data {
my $fd = shift;
my $len = shift;
my $data;
my $res = read $fd, $data, $len;
unless (defined $res and $res == $len)
{
die "message parsing error: unable to read ", $len, " bytes\n";
}
return $data;
}
sub read_len_data {
my $fd = shift;
my $len = shift;
$len = unpack($len == 1 ? 'C' : 'n', read_data($fd, $len));
return read_data($fd, $len);
}
sub encode_hash { sub encode_hash {
my $hash = shift; my $hash = shift;