Skip to content

Commit

Permalink
pgsql: parse only PDU when type is unknown
Browse files Browse the repository at this point in the history
A next PDU may already be in the slice to parse.
Do not skip its parsing, ie do not use rest, but take just
the length of the pdu
  • Loading branch information
catenacyber authored and victorjulien committed Feb 6, 2024
1 parent f52c033 commit 86de7cf
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions rust/src/pgsql/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::common::nom7::take_until_and_consume;
use nom7::branch::alt;
use nom7::bytes::streaming::{tag, take, take_until, take_until1};
use nom7::character::streaming::{alphanumeric1, char};
use nom7::combinator::{all_consuming, cond, eof, map_parser, opt, peek, rest, verify};
use nom7::combinator::{all_consuming, cond, eof, map_parser, opt, peek, verify};
use nom7::error::{make_error, ErrorKind};
use nom7::multi::{many1, many_m_n, many_till};
use nom7::number::streaming::{be_i16, be_i32};
Expand Down Expand Up @@ -1078,10 +1078,12 @@ pub fn pgsql_parse_response(i: &[u8]) -> IResult<&[u8], PgsqlBEMessage> {
b'A' => parse_notification_response(i)?,
b'D' => parse_consolidated_data_row(i)?,
_ => {
let (i, payload) = rest(i)?;
let (i, identifier) = be_u8(i)?;
let (i, length) = verify(be_u32, |&x| x > PGSQL_LENGTH_FIELD)(i)?;
let (i, payload) = take(length - PGSQL_LENGTH_FIELD)(i)?;
let unknown = PgsqlBEMessage::UnknownMessageType (RegularPacket{
identifier: pseudo_header.0,
length: pseudo_header.1,
identifier,
length,
payload: payload.to_vec(),
});
(i, unknown)
Expand Down Expand Up @@ -1918,7 +1920,7 @@ mod tests {
let res = PgsqlBEMessage::UnknownMessageType(RegularPacket {
identifier: b'`',
length: 54,
payload: bad_buf.to_vec(),
payload: bad_buf[5..].to_vec(),
});
assert_eq!(result, res);
assert!(remainder.is_empty());
Expand Down

0 comments on commit 86de7cf

Please sign in to comment.