Skip to content

Commit 0b081b0

Browse files
authored
Handle unnamed winreg values in QueryValueEx (#9589)
Accept a required None value name, preserve UTF-16 strings, issue the CPython audit event, and return initial registry errors before retrying. Assisted-by: dot:frontier-intelligence
1 parent 0bdba72 commit 0b081b0

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

‎crates/host_env/src/winreg.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,25 @@ pub fn query_default_value(
422422

423423
pub fn query_value_bytes(
424424
hkey: Registry::HKEY,
425-
wide_name: &widestring::WideCStr,
425+
wide_name: Option<&widestring::WideCStr>,
426426
) -> Result<(Vec<u8>, u32), u32> {
427427
let mut buf_size: u32 = 0;
428428
let res = unsafe {
429429
query_value_ex(
430430
hkey,
431-
Some(wide_name),
431+
wide_name,
432432
core::ptr::null_mut(),
433433
core::ptr::null_mut(),
434434
&mut buf_size,
435435
)
436436
};
437-
if res == Foundation::ERROR_MORE_DATA || buf_size == 0 {
437+
if res == Foundation::ERROR_MORE_DATA {
438438
buf_size = 256;
439439
} else if res != 0 {
440440
return Err(res);
441+
} else if buf_size == 0 {
442+
// Keep a nonzero buffer so a value that grows between calls can retry.
443+
buf_size = 256;
441444
}
442445

443446
let mut ret_buf = vec![0u8; buf_size as usize];
@@ -448,7 +451,7 @@ pub fn query_value_bytes(
448451
let res = unsafe {
449452
query_value_ex(
450453
hkey,
451-
Some(wide_name),
454+
wide_name,
452455
&mut typ,
453456
ret_buf.as_mut_ptr(),
454457
&mut ret_size,

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,28 @@ mod winreg {
646646
}
647647

648648
#[pyfunction]
649-
fn QueryValueEx(key: HKEYArg, name: String, vm: &VirtualMachine) -> PyResult<PyRef<PyTuple>> {
649+
fn QueryValueEx(
650+
key: HKEYArg,
651+
name: Option<PyRef<PyStr>>,
652+
vm: &VirtualMachine,
653+
) -> PyResult<PyRef<PyTuple>> {
650654
let hkey = key.0;
651-
let wide_name = WideCString::from_str(name).map_err(|e| e.to_pyexception(vm))?;
652-
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, &wide_name)
655+
let wide_name = name
656+
.as_ref()
657+
.map(|name| {
658+
name.as_wtf8()
659+
.to_wide_cstring()
660+
.map_err(|_| nul_char_error(vm))
661+
})
662+
.transpose()?;
663+
vm.audit("winreg.QueryValue", || {
664+
let name: PyObjectRef = wide_name.as_ref().map_or_else(
665+
|| vm.ctx.none(),
666+
|name| vm.ctx.new_str(Wtf8Buf::from_wide(name.as_slice())).into(),
667+
);
668+
(hkey as isize, vm.ctx.none(), name)
669+
})?;
670+
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, wide_name.as_deref())
653671
.map_err(|err| os_error_from_windows_code(vm, err as i32))?;
654672
let obj = reg_to_py(vm, &ret_buf, typ)?;
655673
// Return tuple (value, type)

0 commit comments

Comments
 (0)