From f8ebd9da6ae0bf5f4a4f9bbfacb2c58385d5f1bb Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:41:23 +0100 Subject: [PATCH] winhttp_offline: an empty server name and a context option of the wrong size fail as on the host (WINHTTP-ARG-CHECKS) Two argument checks answered differently from the host's winhttp.dll (measured on Windows 11): - WinHttpConnect with an empty server name failed with ERROR_INVALID_PARAMETER; the host gives ERROR_WINHTTP_INVALID_URL (a null name stays ERROR_INVALID_PARAMETER on both). - WinHttpSetOption(WINHTTP_OPTION_CONTEXT_VALUE) accepted any length of at least a DWORD_PTR and read the first eight bytes; the host takes exactly a DWORD_PTR and refuses 16 bytes with ERROR_INSUFFICIENT_BUFFER as it does 4, keeping the context it had. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/winhttp_offline.cpp | 6 ++++-- runtime/test/winhttp_offline_test.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/runtime/shims/winhttp_offline.cpp b/runtime/shims/winhttp_offline.cpp index e8467f06c0..82b004110b 100644 --- a/runtime/shims/winhttp_offline.cpp +++ b/runtime/shims/winhttp_offline.cpp @@ -203,8 +203,10 @@ HINTERNET WINAPI open_connection(HINTERNET session, LPCWSTR server, INTERNET_POR return nullptr; } if (!server || !*server) { + // As the host's winhttp.dll answers (measured on Windows 11): no name is ERROR_INVALID_PARAMETER, an empty + // one ERROR_WINHTTP_INVALID_URL. release(s); - SetLastError(ERROR_INVALID_PARAMETER); + SetLastError(server ? ERROR_WINHTTP_INVALID_URL : ERROR_INVALID_PARAMETER); return nullptr; } return make_handle(Kind::Connect, s, s->async, server, port); // keeps the reference on the session @@ -342,7 +344,7 @@ BOOL WINAPI set_option(HINTERNET handle, DWORD option, LPVOID buffer, DWORD leng release(h); return fail(ERROR_INVALID_PARAMETER); } - if (length < sizeof(DWORD_PTR)) { + if (length != sizeof(DWORD_PTR)) { // the host takes exactly a DWORD_PTR: 4 and 16 bytes both fail (measured) release(h); return fail(ERROR_INSUFFICIENT_BUFFER); } diff --git a/runtime/test/winhttp_offline_test.cpp b/runtime/test/winhttp_offline_test.cpp index 00ba5326d8..7753fd3e90 100644 --- a/runtime/test/winhttp_offline_test.cpp +++ b/runtime/test/winhttp_offline_test.cpp @@ -342,6 +342,18 @@ int main() { check(set_option(request, WINHTTP_OPTION_CONTEXT_VALUE, &short_ctx, sizeof(short_ctx)) == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "a 4-byte WINHTTP_OPTION_CONTEXT_VALUE fails with ERROR_INSUFFICIENT_BUFFER"); + const DWORD_PTR long_ctx[2] = {0x5678, 0x9abc}; + SetLastError(0); + check(set_option(request, WINHTTP_OPTION_CONTEXT_VALUE, const_cast(long_ctx), sizeof(long_ctx)) == + FALSE && + GetLastError() == ERROR_INSUFFICIENT_BUFFER, + "a 16-byte WINHTTP_OPTION_CONTEXT_VALUE fails with ERROR_INSUFFICIENT_BUFFER too, as on the host"); + { + DWORD_PTR kept = 0; + DWORD kept_size = sizeof(kept); + check(query_option(request, WINHTTP_OPTION_CONTEXT_VALUE, &kept, &kept_size) != FALSE && kept == 0x1234, + "and the context set before stays"); + } SetLastError(0); check(set_option(nullptr, WINHTTP_OPTION_CONTEXT_VALUE, &value, sizeof(value)) == FALSE && GetLastError() == ERROR_WINHTTP_INCORRECT_HANDLE_TYPE, @@ -364,6 +376,9 @@ int main() { SetLastError(0); check(connect(session, nullptr, 80, 0) == nullptr && GetLastError() == ERROR_INVALID_PARAMETER, "a connection needs a server name"); + SetLastError(0); + check(connect(session, L"", 80, 0) == nullptr && GetLastError() == ERROR_WINHTTP_INVALID_URL, + "an empty server name fails with ERROR_WINHTTP_INVALID_URL, as on the host"); check(close(request) != FALSE && close(connection) != FALSE && close(session) != FALSE, "the handles close, children first");