Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: RegQueryValueExA/W is seemingly impossible to use correctly now #2030

Closed
monax3 opened this issue Sep 18, 2022 · 1 comment · Fixed by #2036 or #2039
Closed

Bug: RegQueryValueExA/W is seemingly impossible to use correctly now #2030

monax3 opened this issue Sep 18, 2022 · 1 comment · Fixed by #2036 or #2039
Labels
bug Something isn't working

Comments

@monax3
Copy link

monax3 commented Sep 18, 2022

Which crate is this about?

windows

Crate version

0.40.0

Summary

RegQueryValueExA has the following signature:

pub unsafe fn RegQueryValueExA<'a, P0, P1>(hkey: P0, lpvaluename: P1, lpreserved: &mut u32, lptype: ::core::option::Option<&mut REG_VALUE_TYPE>, lpdata: *mut u8, lpcbdata: ::core::option::Option<&mut u32>) -> super::super::Foundation::WIN32_ERROR
where
    P0: ::std::convert::Into<HKEY>,
    P1: ::std::convert::Into<::windows::core::PCSTR>,

The documentation states:

lpReserved
This parameter is reserved and must be NULL.

This means it can't be called as references should never be NULL. &mut *std::ptr::null() does work for me but is technically UB).

I'm sure there's other cases like this since a lot of functions have this kind of argument but I couldn't find an existing issue.

Toolchain version/configuration

Default host: x86_64-pc-windows-msvc
rustup home: C:\Users\Mona.rustup

installed toolchains

stable-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-msvc (default)

Reproducible example

use std::ptr::null_mut;
use windows::core::{s, w, HSTRING, PCSTR};
use windows::Win32::Foundation::E_INVALIDARG;
use windows::Win32::System::Registry::{RegOpenKeyExA,RegOpenKeyExW, RegQueryValueExA, RegQueryValueExW, HKEY, HKEY_LOCAL_MACHINE, KEY_READ, RegCloseKey};

fn repro_a() {
    const KEY: PCSTR = s!(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Setup");
    const VALUE: PCSTR = s!(r"BootDir");

    let mut hkey = HKEY::default();
    let mut len = 0;
    let mut zero = 0;
    unsafe {
        RegOpenKeyExA(HKEY_LOCAL_MACHINE, KEY,0, KEY_READ, &mut hkey).ok().unwrap();
        assert_eq!(RegQueryValueExA(hkey, VALUE, &mut zero, None, null_mut(), Some(&mut len)).ok(), Err(E_INVALIDARG.into()));
        assert!(RegQueryValueExA(hkey, VALUE, &mut *null_mut(), None, null_mut(), Some(&mut len)).ok().is_ok());
        RegCloseKey(hkey).ok().unwrap();
    }
}

fn repro_w() {
    const KEY: &HSTRING = w!(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Setup");
    const VALUE: &HSTRING = w!(r"BootDir");

    let mut hkey = HKEY::default();
    let mut len = 0;
    let mut zero = 0;
    unsafe {
        RegOpenKeyExW(HKEY_LOCAL_MACHINE, KEY, 0, KEY_READ, &mut hkey).ok().unwrap();
        assert_eq!(RegQueryValueExW(hkey, VALUE, &mut zero, None, null_mut(), Some(&mut len)).ok(), Err(E_INVALIDARG.into()));
        assert!(RegQueryValueExW(hkey, VALUE, &mut *null_mut(), None, null_mut(), Some(&mut len)).ok().is_ok());
        RegCloseKey(hkey).ok().unwrap();
    }
}

fn main() {
    repro_a();
    repro_w();
}

Crate manifest

[package]
name = "regqueryvaluerepro"
version = "0.1.0"
edition = "2021"

[dependencies]
windows = { version = "0.40.0", features = ["Win32_Foundation", "Win32_System_Registry"] }

Expected behavior

lpreserved should accept either std::ptr::null() or None

Actual behavior

lpreserved only accepts a valid reference which is an invalid argument

Additional comments

No response

@monax3 monax3 added the bug Something isn't working label Sep 18, 2022
@kennykerr
Copy link
Collaborator

Thanks for reporting! This has now been fixed (#2036). I'll leave this open until I further apply Option to reserved parameters so that you can simply pass None instead of having to type std::ptr::null().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants