From 9e6ed9a5ac3d9131c74616857e03ed423fd8d501 Mon Sep 17 00:00:00 2001 From: danielbotros Date: Tue, 31 Mar 2026 13:40:07 -0400 Subject: [PATCH 1/3] Use lossy utf8 conversion --- rtc-stun/src/error_code.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtc-stun/src/error_code.rs b/rtc-stun/src/error_code.rs index 21ba7269..a519e267 100644 --- a/rtc-stun/src/error_code.rs +++ b/rtc-stun/src/error_code.rs @@ -17,7 +17,7 @@ pub struct ErrorCodeAttribute { impl fmt::Display for ErrorCodeAttribute { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let reason = match String::from_utf8(self.reason.clone()) { + let reason = match String::from_utf8_lossy(self.reason.clone()) { Ok(reason) => reason, Err(_) => return Err(fmt::Error {}), }; From 82abcb6dc61c14eab9c0fac9509895304a0029af Mon Sep 17 00:00:00 2001 From: danielbotros Date: Tue, 31 Mar 2026 13:49:02 -0400 Subject: [PATCH 2/3] Remove result match --- rtc-stun/src/error_code.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rtc-stun/src/error_code.rs b/rtc-stun/src/error_code.rs index a519e267..491ec3de 100644 --- a/rtc-stun/src/error_code.rs +++ b/rtc-stun/src/error_code.rs @@ -17,12 +17,7 @@ pub struct ErrorCodeAttribute { impl fmt::Display for ErrorCodeAttribute { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let reason = match String::from_utf8_lossy(self.reason.clone()) { - Ok(reason) => reason, - Err(_) => return Err(fmt::Error {}), - }; - - write!(f, "{}: {}", self.code.0, reason) + write!(f, "{}: {}", self.code.0, String::from_utf8_lossy(&self.reason)) } } From 9bc81dc02806f6b171fe46d85e22986a25f11fdd Mon Sep 17 00:00:00 2001 From: danielbotros Date: Tue, 31 Mar 2026 15:40:59 -0400 Subject: [PATCH 3/3] Add regression test --- rtc-stun/src/error_code.rs | 3 +++ rtc-stun/src/error_code/error_code_test.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 rtc-stun/src/error_code/error_code_test.rs diff --git a/rtc-stun/src/error_code.rs b/rtc-stun/src/error_code.rs index 491ec3de..947c8083 100644 --- a/rtc-stun/src/error_code.rs +++ b/rtc-stun/src/error_code.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +mod error_code_test; + use crate::attributes::*; use crate::checks::*; use crate::message::*; diff --git a/rtc-stun/src/error_code/error_code_test.rs b/rtc-stun/src/error_code/error_code_test.rs new file mode 100644 index 00000000..4053b0f8 --- /dev/null +++ b/rtc-stun/src/error_code/error_code_test.rs @@ -0,0 +1,21 @@ +use super::*; + +#[test] +fn test_display_valid_utf8() { + let code = ErrorCodeAttribute { + code: ErrorCode(401), + reason: b"Unauthorized".to_vec(), + }; + assert_eq!(format!("{}", code), "401: Unauthorized"); +} + +#[test] +fn test_display_invalid_utf8_does_not_panic() { + let code = ErrorCodeAttribute { + code: ErrorCode(401), + reason: vec![0xc0, 0xaf], + }; + let result = format!("{}", code); + assert!(result.starts_with("401: ")); + assert!(result.contains('\u{FFFD}')); +}