Commit e4e2e338 authored by Damjan Jovanovic's avatar Damjan Jovanovic Committed by Alexandre Julliard

wininet: Return the required buffer size from InternetGetLastResponseInfo() when it's too small.

parent 2f2a5d5c
......@@ -1092,10 +1092,17 @@ BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
}
if (lpwite)
{
if (lpszBuffer == NULL || *lpdwBufferLength < strlen(lpwite->response))
{
*lpdwBufferLength = strlen(lpwite->response);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
*lpdwError = lpwite->dwError;
if (lpwite->dwError)
if (lpwite->dwError && *lpdwBufferLength)
{
memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
lpszBuffer[*lpdwBufferLength - 1] = 0;
*lpdwBufferLength = strlen(lpszBuffer);
}
else
......@@ -1134,8 +1141,15 @@ BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
}
if (lpwite)
{
int required_size = MultiByteToWideChar(CP_ACP, 0, lpwite->response, -1, NULL, 0) - 1;
if (lpszBuffer == NULL || *lpdwBufferLength < required_size)
{
*lpdwBufferLength = required_size;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
*lpdwError = lpwite->dwError;
if (lpwite->dwError)
if (lpwite->dwError && *lpdwBufferLength)
*lpdwBufferLength = MultiByteToWideChar(CP_ACP, 0, lpwite->response, -1, lpszBuffer, *lpdwBufferLength);
else
*lpdwBufferLength = 0;
......
......@@ -738,7 +738,8 @@ static void test_command(HINTERNET hFtp)
for (i = 0; i < ARRAY_SIZE(command_test); i++)
{
DWORD size;
DWORD size, orig_size;
char *buffer;
SetLastError(0xdeadbeef);
ret = pFtpCommandA(hFtp, FALSE, FTP_TRANSFER_TYPE_ASCII, command_test[i].cmd, 0, NULL);
......@@ -747,10 +748,35 @@ static void test_command(HINTERNET hFtp)
ok(ret == command_test[i].ret, "%d: expected FtpCommandA to %s\n", i, command_test[i].ret ? "succeed" : "fail");
ok(error == command_test[i].error, "%d: expected error %u, got %u\n", i, command_test[i].error, error);
size = 0;
ret = InternetGetLastResponseInfoA(&error, NULL, NULL);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "%d: ret %d, lasterr %d\n", i, ret, GetLastError());
ret = InternetGetLastResponseInfoA(NULL, NULL, &size);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "%d: ret %d, lasterr %d\n", i, ret, GetLastError());
/* Zero size */
size = 0;
ret = InternetGetLastResponseInfoA(&error, NULL, &size);
ok((ret && size == 0) || (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER), "%d: got ret %d, size %d, lasterr %d\n", i, ret, size, GetLastError());
/* Positive size, NULL buffer */
size++;
ret = InternetGetLastResponseInfoA(&error, NULL, &size);
ok((ret && size == 0) || (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER), "%d: got ret %d, size %u, lasterr %d\n", i, ret, size, GetLastError());
/* When buffer is 1 char too short, it succeeds but trims the string: */
orig_size = size;
buffer = HeapAlloc(GetProcessHeap(), 0, size);
ok(buffer != NULL, "%d: no memory\n", i);
ret = InternetGetLastResponseInfoA(&error, buffer, &size);
ok(ret, "%d: got ret %d\n", i, ret);
ok(orig_size == 0 ? size == 0 : size == orig_size - 1, "%d: got orig_size %d, size %d\n", i, orig_size, size);
ok(size == 0 || strlen(buffer) == size, "%d: size %d, buffer size %d\n", i, size, size ? strlen(buffer) : 0);
HeapFree(GetProcessHeap(), 0, buffer);
/* Long enough buffer */
buffer = HeapAlloc(GetProcessHeap(), 0, ++size);
ok(buffer != NULL, "%d: no memory\n", i);
ret = InternetGetLastResponseInfoA(&error, buffer, &size);
ok(ret, "%d: got ret %d\n", i, ret);
ok(size == 0 || strlen(buffer) == size, "%d: size %d, buffer size %d\n", i, size, size ? strlen(buffer) : 0);
HeapFree(GetProcessHeap(), 0, buffer);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment