From: Tobias Brunner Date: Tue, 5 Jun 2018 15:49:42 +0000 (+0200) Subject: vici: Add error handling to message parsing in Perl bindings X-Git-Tag: 5.7.0rc1~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de4c3d2e76798aae2080708eb4ce3456b8edef7c;p=thirdparty%2Fstrongswan.git vici: Add error handling to message parsing in Perl bindings --- diff --git a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm index b777e25170..d0700fa972 100644 --- a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm +++ b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm @@ -70,23 +70,18 @@ sub parse { until ( eof $fd ) { - read $fd, $data, 1; - my $type = unpack('C', $data); + my $type = unpack('C', read_data($fd, 1)); if ( $type == SECTION_END ) { return; } - read $fd, $data, 1; - my $length = unpack('C', $data); - read $fd, my $key, $length; + my $key = read_len_data($fd, 1); if ( $type == KEY_VALUE ) { - read $fd, $data, 2; - my $length = unpack('n', $data); - read $fd, my $value, $length; + my $value = read_len_data($fd, 2); $hash->{$key} = $value; } elsif ( $type == SECTION_START ) @@ -102,14 +97,11 @@ sub parse { while ( !eof($fd) and $more ) { - read $fd, $data, 1; - my $type = unpack('C', $data); + my $type = unpack('C', read_data($fd, 1)); if ( $type == LIST_ITEM ) { - read $fd, $data, 2; - my $length = unpack('n', $data); - read $fd, my $value, $length; + my $value = read_len_data($fd, 2); push(@list, $value); } 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 { my $hash = shift;