Skip to content

Commit

Permalink
Fix datagram buffer remaining size
Browse files Browse the repository at this point in the history
In the Java version, a datagram was incorrectly rejected if its size was
exactly the remaining buffer size (this was harmless).

In the Rust version, the datagram buffer considered it had enough space
if a packet was 1 byte too big (causing packet overwriting).

Refs #345 <#345>
  • Loading branch information
rom1v committed Dec 8, 2020
1 parent 6d2a4b9 commit 211db45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public boolean hasEnoughSpaceFor(int datagramLength) {
// there is at least the extra space for storing 1 packet
return true;
}
int remaining = tail - head - 1;
return HEADER_LENGTH + datagramLength < remaining;
int remaining = tail - head - 1; // 1 extra byte to distinguish empty vs full
return HEADER_LENGTH + datagramLength <= remaining;
}

public int capacity() {
Expand Down
5 changes: 3 additions & 2 deletions relay-rust/src/relay/datagram_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ impl DatagramBuffer {
// there is at least the extra space for storing 1 packet
return true;
}
let remaining = self.tail - self.head + 1;
HEADER_LENGTH + datagram_length < remaining
// 1 extra byte to distinguish empty vs full
let remaining = self.tail - self.head - 1;
HEADER_LENGTH + datagram_length <= remaining
}

pub fn write_to<S: DatagramSender>(&mut self, destination: &mut S) -> io::Result<()> {
Expand Down

0 comments on commit 211db45

Please sign in to comment.