Discussion:
VS2015, WinHttp v5 - WinHttpQueryHeaders 1. call to get buffer size, 2. memory corruption after second call
(too old to reply)
m***@gmail.com
2017-05-09 15:25:00 UTC
Permalink
Hi,

I just started with WinHttp and I'm glad I succeeded with most tasks/obstacles.

One obstacle lasts - to get a "custom" header I used the following scheme to get the value:

1. use WinHttpQueryHeaders with WINHTTP_QUERY_CUSTOM and a nullptr as LPVOID parameter to get the needed size of the buffer.
2. I allocate memory (alloca or malloc) for the buffer
3. WinHttpQueryHeaders is called a second time with the buffer to retrieve the header value
(4. I free memory allocated with malloc)

Using alloca the memory corruption exception is raised by VS2015 leaving the function, while the heap corruption exception is raised by VS2015 as soon as the memory gets deallocated.

Here the source code example:

DWORD dwBufferSize = 0;

WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
nullptr, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX);

dwError = GetLastError();
if (dwError == ERROR_INSUFFICIENT_BUFFER)
{
LPWSTR pszBuffer = (LPWSTR)malloc(dwBufferSize / sizeof(WCHAR));

if (WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
(LPVOID)pszBuffer, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX) != FALSE)
{
// ... do something ...
}

free(pszBuffer);
}

The values I retrieve:

1. call: dwBufferSize = 12
2. call:
- dwBufferSize = 10
- pszBuffer = L"Valid"

I tested by allocating more and setting the memory to 0xFF to see, if something weird is going on, but I didn't get it!

Anychance to get help here?

Best regards,

Martin Lemburg
Martin Lemburg
2017-05-09 16:17:18 UTC
Permalink
Post by m***@gmail.com
Hi,
I just started with WinHttp and I'm glad I succeeded with most tasks/obstacles.
1. use WinHttpQueryHeaders with WINHTTP_QUERY_CUSTOM and a nullptr as LPVOID parameter to get the needed size of the buffer.
2. I allocate memory (alloca or malloc) for the buffer
3. WinHttpQueryHeaders is called a second time with the buffer to retrieve the header value
(4. I free memory allocated with malloc)
Using alloca the memory corruption exception is raised by VS2015 leaving the function, while the heap corruption exception is raised by VS2015 as soon as the memory gets deallocated.
DWORD dwBufferSize = 0;
WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
nullptr, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX);
dwError = GetLastError();
if (dwError == ERROR_INSUFFICIENT_BUFFER)
{
LPWSTR pszBuffer = (LPWSTR)malloc(dwBufferSize / sizeof(WCHAR));
if (WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
(LPVOID)pszBuffer, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX) != FALSE)
{
// ... do something ...
}
free(pszBuffer);
}
1. call: dwBufferSize = 12
- dwBufferSize = 10
- pszBuffer = L"Valid"
I tested by allocating more and setting the memory to 0xFF to see, if something weird is going on, but I didn't get it!
Anychance to get help here?
Best regards,
Martin Lemburg
I tried additionally to get the status text with WINHTTP_QUERY_STATUS_TEXT and tried the same way, than described above by first querying the needed buffer size, allocating the buffer, letting WinHttp filling the buffer.

Free'ing the buffer raises the heap corruption exception!

What am I doing wrong? What did I misunderstand?

Best regards,

Martin Lemburg

Loading...