{"record":{"id":"99f6886e4593d97a","repo":"ramensoftware/windhawk","slug":"regqueryvalueex-data","errorCode":null,"errorMessage":"RegQueryValueEx (data)","messagePattern":"RegQueryValueEx \\(data\\)","errorType":"error_code","errorClass":"SettingsError","httpStatus":null,"severity":"error","filePath":"src/windhawk-core/windows/src/registry.rs","lineNumber":444,"sourceCode":"        }\n        let mut buf = vec![0u8; size as usize];\n        let mut data_size = size;\n        // SAFETY: buf has data_size bytes; out params valid.\n        let rc = unsafe {\n            RegQueryValueExW(\n                hkey,\n                name_w.as_ptr(),\n                std::ptr::null(),\n                &mut value_type,\n                buf.as_mut_ptr(),\n                &mut data_size,\n            )\n        };\n        if rc == ERROR_FILE_NOT_FOUND {\n            return Ok(None);\n        }\n        if rc != ERROR_SUCCESS {\n            return Err(self.err(\"get\", rc, \"RegQueryValueEx (data)\"));\n        }\n        buf.truncate(data_size as usize);\n        Ok(Some((value_type, buf)))\n    }\n\n    fn set_raw(&self, name: &str, value_type: u32, data: &[u8]) -> Result<(), SettingsError> {\n        self.check_name(\"set\", name)?;\n        let Some(hkey) = self.hkey() else {\n            return Err(self.err(\"set\", 0, \"set on a read-only/absent key\"));\n        };\n        let name_w = to_wide(name);\n        // A length the API's u32 cannot hold has no honest value to pass, and a\n        // clamped one would hand the call a length the buffer does not have.\n        let len = u32::try_from(data.len())\n            .map_err(|_| self.err(\"set\", 0, \"value is too large for the registry\"))?;\n        // SAFETY: name_w is NUL-terminated; data/len describe a valid buffer.\n        let rc =\n            unsafe { RegSetValueExW(hkey, name_w.as_ptr(), 0, value_type, data.as_ptr(), len) };","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/ramensoftware/windhawk/blob/61d99ed8e182e1af1b60109612b6763ad1b4b74e/src/windhawk-core/windows/src/registry.rs#L426-L462","documentation":"The second (sized) RegQueryValueExW call inside query_raw failed with an OS error other than ERROR_SUCCESS. The size query succeeded but reading the actual data buffer failed; the error carries the Win32 function name and the OS error code.","triggerScenarios":"Reading a value whose size changed or grew between the size probe and the data read (a race with another writer), an access-denied on the data read, or a handle invalidated mid-read.","commonSituations":"Another process concurrently rewriting the value; very large values where the size probe and read race; transient registry access issues under restrictive policies.","solutions":["Retry query_raw once; a transient size race typically resolves on the second read.","Check the wrapped OS code (ERROR_MORE_DATA implies the value grew — retry with a larger buffer).","Ensure the key handle is still valid and opened with KEY_QUERY_VALUE access.","Serialize writes/reads (avoid concurrent writers to the same value) if a race is suspected."],"exampleFix":"// before\nlet (t, data) = settings.query_raw(name)?; // can fail transiently\n// after\nlet mut tries = 0;\nlet (t, data) = loop {\n    match settings.query_raw(name) {\n        Ok(v) => break v,\n        Err(e) if tries < 2 => { tries += 1; continue; }\n        Err(e) => return Err(e),\n    }\n};","handlingStrategy":"retry","validationCode":"// no pre-call validation; failure is environmental or a size race","typeGuard":null,"tryCatchPattern":"let mut tries = 0;\nlet val = loop {\n    match settings.get_string(\"x\") {\n        Ok(v) => break v,\n        Err(e) if tries < 2 && e.message().contains(\"RegQueryValueEx\") => { tries += 1; continue; }\n        Err(e) => return Err(e),\n    }\n};","preventionTips":["Avoid concurrent writers to the same registry value.","Retry transient read failures with a short backoff.","Re-open the key if reads fail repeatedly (stale handle).","Keep values small so the size-probe/read pair is atomic in practice."],"tags":["registry","win32","race-condition","io"],"backgroundTag":"api-error-response","analyzedSha":"61d99ed8e182e1af1b60109612b6763ad1b4b74e","analyzedAt":"2026-09-12T14:02:41.115Z","contentChangedAt":"2026-09-12T14:02:41.115Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}