Skip to content

Commit d045db9

Browse files
authored
Preserve surrogate code points in registry values (#9597)
* Preserve surrogate code points in registry values Decode REG_SZ, REG_EXPAND_SZ, and REG_MULTI_SZ values through WTF-8 so unpaired UTF-16 surrogates are represented in Python strings. Assisted-by: dot:frontier-intelligence * Make registry value conversion infallible Remove the obsolete Result wrapper after surrogate-preserving conversions eliminated the last error path, resolving Windows Clippy unnecessary_wraps. Assisted-by: OpenAI Codex:model version unavailable
1 parent a634d22 commit d045db9

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

‎crates/vm/src/stdlib/winreg.rs‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ mod winreg {
561561

562562
// Slice the data buffer to the actual size returned.
563563
let data_slice = &ret_data_buf[..current_data_size as usize];
564-
let py_data = reg_to_py(vm, data_slice, reg_type)?;
564+
let py_data = reg_to_py(vm, data_slice, reg_type);
565565

566566
// Return tuple (value_name, data, type)
567567
return Ok(vm
@@ -711,7 +711,7 @@ mod winreg {
711711
})?;
712712
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, wide_name.as_deref())
713713
.map_err(|err| os_error_from_windows_code(vm, err as i32))?;
714-
let obj = reg_to_py(vm, &ret_buf, typ)?;
714+
let obj = reg_to_py(vm, &ret_buf, typ);
715715
// Return tuple (value, type)
716716
Ok(vm.ctx.new_tuple(vec![obj, vm.ctx.new_int(typ).into()]))
717717
}
@@ -761,22 +761,22 @@ mod winreg {
761761
}
762762
}
763763

764-
fn reg_to_py(vm: &VirtualMachine, ret_data: &[u8], typ: u32) -> PyResult {
764+
fn reg_to_py(vm: &VirtualMachine, ret_data: &[u8], typ: u32) -> PyObjectRef {
765765
match typ {
766766
REG_DWORD => {
767767
// If there isn’t enough data, return 0.
768768
let val = ret_data
769769
.first_chunk::<4>()
770770
.copied()
771771
.map_or(0, u32::from_ne_bytes);
772-
Ok(vm.ctx.new_int(val).into())
772+
vm.ctx.new_int(val).into()
773773
}
774774
REG_QWORD => {
775775
let val = ret_data
776776
.first_chunk::<8>()
777777
.copied()
778778
.map_or(0, u64::from_ne_bytes);
779-
Ok(vm.ctx.new_int(val).into())
779+
vm.ctx.new_int(val).into()
780780
}
781781
REG_SZ | REG_EXPAND_SZ => {
782782
let u16_slice = host_winreg::bytes_as_wide_slice(ret_data);
@@ -785,13 +785,12 @@ mod winreg {
785785
.iter()
786786
.position(|&c| c == 0)
787787
.unwrap_or(u16_slice.len());
788-
let s = String::from_utf16(&u16_slice[..len])
789-
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
790-
Ok(vm.ctx.new_str(s).into())
788+
let s = Wtf8Buf::from_wide(&u16_slice[..len]);
789+
vm.ctx.new_str(s).into()
791790
}
792791
REG_MULTI_SZ => {
793792
if ret_data.is_empty() {
794-
Ok(vm.ctx.new_list(vec![]).into())
793+
vm.ctx.new_list(vec![]).into()
795794
} else {
796795
let u16_slice = host_winreg::bytes_as_wide_slice(ret_data);
797796
let u16_count = u16_slice.len();
@@ -807,27 +806,25 @@ mod winreg {
807806
let mut start = 0;
808807
for i in 0..len {
809808
if u16_slice[i] == 0 {
810-
let s = String::from_utf16(&u16_slice[start..i])
811-
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
809+
let s = Wtf8Buf::from_wide(&u16_slice[start..i]);
812810
strings.push(vm.ctx.new_str(s).into());
813811
start = i + 1;
814812
}
815813
}
816814
// Handle last string if not null-terminated
817815
if start < len {
818-
let s = String::from_utf16(&u16_slice[start..len])
819-
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
816+
let s = Wtf8Buf::from_wide(&u16_slice[start..len]);
820817
strings.push(vm.ctx.new_str(s).into());
821818
}
822-
Ok(vm.ctx.new_list(strings).into())
819+
vm.ctx.new_list(strings).into()
823820
}
824821
}
825822
// For REG_BINARY and any other unknown types, return a bytes object if data exists.
826823
_ => {
827824
if ret_data.is_empty() {
828-
Ok(vm.ctx.none())
825+
vm.ctx.none()
829826
} else {
830-
Ok(vm.ctx.new_bytes(ret_data.to_vec()).into())
827+
vm.ctx.new_bytes(ret_data.to_vec()).into()
831828
}
832829
}
833830
}

0 commit comments

Comments
 (0)