Quellcode durchsuchen

In {Tcp,Udp}Socket::process, make protocol check an assertion.

We filter sockets by type upstream of the process() calls.
whitequark vor 7 Jahren
Ursprung
Commit
a15bc8105e
2 geänderte Dateien mit 3 neuen und 3 gelöschten Zeilen
  1. 2 2
      src/socket/tcp.rs
  2. 1 1
      src/socket/udp.rs

+ 2 - 2
src/socket/tcp.rs

@@ -634,9 +634,9 @@ impl<'a> TcpSocket<'a> {
     /// See [Socket::process](enum.Socket.html#method.process).
     pub fn process(&mut self, timestamp: u64, ip_repr: &IpRepr,
                    payload: &[u8]) -> Result<(), Error> {
-        if self.state == State::Closed { return Err(Error::Rejected) }
+        debug_assert!(ip_repr.protocol() == IpProtocol::Tcp);
 
-        if ip_repr.protocol() != IpProtocol::Tcp { return Err(Error::Rejected) }
+        if self.state == State::Closed { return Err(Error::Rejected) }
 
         let packet = TcpPacket::new_checked(&payload[..ip_repr.payload_len()])?;
         let repr = TcpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr())?;

+ 1 - 1
src/socket/udp.rs

@@ -153,7 +153,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     /// See [Socket::process](enum.Socket.html#method.process).
     pub fn process(&mut self, _timestamp: u64, ip_repr: &IpRepr,
                    payload: &[u8]) -> Result<(), Error> {
-        if ip_repr.protocol() != IpProtocol::Udp { return Err(Error::Rejected) }
+        debug_assert!(ip_repr.protocol() == IpProtocol::Udp);
 
         let packet = UdpPacket::new_checked(&payload[..ip_repr.payload_len()])?;
         let repr = UdpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr())?;