tty: Truncate Edid strings to nul
Browse files at this commit78cda2e67fe8cee66ec1b64914031430aedd8733Parent: 924e21f1 changed file +41 -3
- src/backend/tty.rs +41 -3
src/backend/tty.rsmodified+41 -3
| @@ -652,7 +652,7 @@ impl Tty { | |||
| 652 | if non_desktop { | 652 | if non_desktop { |
| 653 | debug!("output is non desktop"); | 653 | debug!("output is non desktop"); |
| 654 | let description = EdidInfo::for_connector(&device.drm, connector.handle()) | 654 | let description = EdidInfo::for_connector(&device.drm, connector.handle()) |
| 655 | - .map(|info| info.model) | 655 | + .map(|info| truncate_to_nul(info.model)) |
| 656 | .unwrap_or_else(|| "Unknown".into()); | 656 | .unwrap_or_else(|| "Unknown".into()); |
| 657 | device.drm_lease_state.add_connector::<State>( | 657 | device.drm_lease_state.add_connector::<State>( |
| 658 | connector.handle(), | 658 | connector.handle(), |
| @@ -712,7 +712,12 @@ impl Tty { | |||
| 712 | let (physical_width, physical_height) = connector.size().unwrap_or((0, 0)); | 712 | let (physical_width, physical_height) = connector.size().unwrap_or((0, 0)); |
| 713 | 713 | ||
| 714 | let (make, model) = EdidInfo::for_connector(&device.drm, connector.handle()) | 714 | let (make, model) = EdidInfo::for_connector(&device.drm, connector.handle()) |
| 715 | - .map(|info| (info.manufacturer, info.model)) | 715 | + .map(|info| { |
| 716 | + ( | ||
| 717 | + truncate_to_nul(info.manufacturer), | ||
| 718 | + truncate_to_nul(info.model), | ||
| 719 | + ) | ||
| 720 | + }) | ||
| 716 | .unwrap_or_else(|| ("Unknown".into(), "Unknown".into())); | 721 | .unwrap_or_else(|| ("Unknown".into(), "Unknown".into())); |
| 717 | 722 | ||
| 718 | let output = Output::new( | 723 | let output = Output::new( |
| @@ -1239,7 +1244,12 @@ impl Tty { | |||
| 1239 | let physical_size = connector.size(); | 1244 | let physical_size = connector.size(); |
| 1240 | 1245 | ||
| 1241 | let (make, model) = EdidInfo::for_connector(&device.drm, connector.handle()) | 1246 | let (make, model) = EdidInfo::for_connector(&device.drm, connector.handle()) |
| 1242 | - .map(|info| (info.manufacturer, info.model)) | 1247 | + .map(|info| { |
| 1248 | + ( | ||
| 1249 | + truncate_to_nul(info.manufacturer), | ||
| 1250 | + truncate_to_nul(info.model), | ||
| 1251 | + ) | ||
| 1252 | + }) | ||
| 1243 | .unwrap_or_else(|| ("Unknown".into(), "Unknown".into())); | 1253 | .unwrap_or_else(|| ("Unknown".into(), "Unknown".into())); |
| 1244 | 1254 | ||
| 1245 | let modes = connector | 1255 | let modes = connector |
| @@ -1697,3 +1707,31 @@ fn pick_mode( | |||
| 1697 | 1707 | ||
| 1698 | mode.map(|m| (*m, fallback)) | 1708 | mode.map(|m| (*m, fallback)) |
| 1699 | } | 1709 | } |
| 1710 | + | ||
| 1711 | +fn truncate_to_nul(mut s: String) -> String { | ||
| 1712 | + if let Some(index) = s.find('\0') { | ||
| 1713 | + s.truncate(index); | ||
| 1714 | + } | ||
| 1715 | + s | ||
| 1716 | +} | ||
| 1717 | + | ||
| 1718 | +#[cfg(test)] | ||
| 1719 | +mod tests { | ||
| 1720 | + use super::*; | ||
| 1721 | + | ||
| 1722 | + #[track_caller] | ||
| 1723 | + fn check(input: &str, expected: &str) { | ||
| 1724 | + let input = String::from(input); | ||
| 1725 | + assert_eq!(truncate_to_nul(input), expected); | ||
| 1726 | + } | ||
| 1727 | + | ||
| 1728 | + #[test] | ||
| 1729 | + fn truncate_to_nul_works() { | ||
| 1730 | + check("", ""); | ||
| 1731 | + check("qwer", "qwer"); | ||
| 1732 | + check("abc\0def", "abc"); | ||
| 1733 | + check("\0as", ""); | ||
| 1734 | + check("a\0\0\0b", "a"); | ||
| 1735 | + check("bb😁\0cc", "bb😁"); | ||
| 1736 | + } | ||
| 1737 | +} | ||