Skip to content

Commit 9e7a27c

Browse files
committed
Fix handling of Unicode serial numbers
Rust str are indexed by byte, so doing .take().count() is incorrect since Unicode values are often multiple bytes. This fixes it and adds tests for the edge cases.
1 parent 290ab2c commit 9e7a27c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/windows/enumerate.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ impl<'hwid> HwidMatches<'hwid> {
116116
let serial = if hwid_tail.starts_with('\\') || hwid_tail.starts_with('+') {
117117
hwid_tail.get(1..).and_then(|tail| {
118118
let index = tail
119-
.chars()
120-
.take_while(|&character| character.is_alphanumeric())
121-
.count();
119+
.char_indices()
120+
.find(|&(_, char)| !char.is_alphanumeric())
121+
.map(|(index, _)| index)
122+
.unwrap_or(tail.len());
122123
tail.get(..index)
123124
})
124125
} else {
@@ -570,4 +571,16 @@ fn test_parsing_usb_port_information() {
570571
assert_eq!(info.serial_number, Some("385435603432".to_string()));
571572
#[cfg(feature = "usbportinfo-interface")]
572573
assert_eq!(info.interface, None);
574+
575+
let unicode_serial = r"USB\VID_F055&PID_9802\3854356β03432&test";
576+
let info = parse_usb_port_info(unicode_serial, None).unwrap();
577+
assert_eq!(info.serial_number.as_deref(), Some("3854356β03432"));
578+
579+
let unicode_serial = r"USB\VID_F055&PID_9802\3854356β03432";
580+
let info = parse_usb_port_info(unicode_serial, None).unwrap();
581+
assert_eq!(info.serial_number.as_deref(), Some("3854356β03432"));
582+
583+
let unicode_serial = r"USB\VID_F055&PID_9802\3854356β";
584+
let info = parse_usb_port_info(unicode_serial, None).unwrap();
585+
assert_eq!(info.serial_number.as_deref(), Some("3854356β"));
573586
}

0 commit comments

Comments
 (0)