Skip to content

Commit 3e47da6

Browse files
authored
Fix overflow in RTT calculation (webrtc-rs#350)
1 parent 56368e9 commit 3e47da6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

interceptor/src/stats/interceptor.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,11 @@ where
450450
for recp in &rr.reports {
451451
let e = acc.entry(recp.ssrc).or_default();
452452

453-
let rtt_ms = (recp.delay != 0)
454-
.then(|| calculate_rtt_ms(now, recp.delay, recp.last_sender_report));
453+
let rtt_ms = if recp.delay != 0 {
454+
calculate_rtt_ms(now, recp.delay, recp.last_sender_report)
455+
} else {
456+
None
457+
};
455458

456459
e.receiver_reports.push(ReceiverReportEntry {
457460
ext_seq_num: recp.last_sequence_number,
@@ -578,7 +581,7 @@ where
578581
let futures = sender_reports.into_iter().map(|sr| {
579582
let rtt_ms = match (sr.dlrr_last_rr, sr.dlrr_delay_rr, sr.sr_packets_sent) {
580583
(Some(last_rr), Some(delay_rr), Some(_)) if last_rr != 0 && delay_rr != 0 => {
581-
Some(calculate_rtt_ms(now, delay_rr, last_rr))
584+
calculate_rtt_ms(now, delay_rr, last_rr)
582585
}
583586
_ => None,
584587
};
@@ -790,7 +793,7 @@ impl RTPWriter for RTPWriteRecorder {
790793
/// - `now` the current middle 32 bits of an NTP timestamp for the current time.
791794
/// - `delay` the delay(`DLSR`) since last sender report expressed as fractions of a second in 32 bits.
792795
/// - `last_report` the middle 32 bits of an NTP timestamp for the most recent sender report(LSR) or Receiver Report(LRR).
793-
fn calculate_rtt_ms(now: u32, delay: u32, last_report: u32) -> f64 {
796+
fn calculate_rtt_ms(now: u32, delay: u32, last_report: u32) -> Option<f64> {
794797
// [10 Nov 1995 11:33:25.125 UTC] [10 Nov 1995 11:33:36.5 UTC]
795798
// n SR(n) A=b710:8000 (46864.500 s)
796799
// ---------------------------------------------------------------->
@@ -809,11 +812,11 @@ fn calculate_rtt_ms(now: u32, delay: u32, last_report: u32) -> f64 {
809812
// -------------------------------
810813
// delay 0x0006:2000 ( 6.125 s)
811814

812-
let rtt = now - delay - last_report;
815+
let rtt = now.checked_sub(delay)?.checked_sub(last_report)?;
813816
let rtt_seconds = rtt >> 16;
814817
let rtt_fraction = (rtt & (u16::MAX as u32)) as f64 / (u16::MAX as u32) as f64;
815818

816-
rtt_seconds as f64 * 1000.0 + (rtt_fraction as f64) * 1000.0
819+
Some(rtt_seconds as f64 * 1000.0 + (rtt_fraction as f64) * 1000.0)
817820
}
818821

819822
#[cfg(test)]

webrtc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Added support for insecure/deprecated signature verification algorithms, opt in via `SettingsEngine::allow_insecure_verification_algorithm` [#342](https://github.com/webrtc-rs/webrtc/pull/342).
66
* Make RTCRtpCodecCapability::payloader_for_codec public API [#349](https://github.com/webrtc-rs/webrtc/pull/349).
7+
* Fixed a panic in `calculate_rtt_ms` [#350](https://github.com/webrtc-rs/webrtc/pull/350).
78

89
## v0.6.0
910

0 commit comments

Comments
 (0)