The following test case will cause GP fault when run using the PSDK x64
compiler. The x86 library seems to handle this case just fine. The issue is
having a string without a null terminator that aligns itself onto the very
end of an allocates region such that strncpy() will run right off the end
looking for the terminating '\0'. This problem persists in Vista x64 RC2.
The test case does not verify that the next page is unallocated and you
could be unlucky and have the test case work if that were to happen.
I have not tried this with VS2005 x64 library.
The workaround is to use memcpy() since I know the exact length of the string.
#include <windows.h>
#include <stdio.h>
void main(void)
{
char *buffer;
char *ptr;
int len = 8;
char Str[30];
SYSTEM_INFO info;
register int ii;
// Figure out the page size
GetSystemInfo(&info);
// Allocate a chunk of memory
// Exactly one page in length
buffer = VirtualAlloc(0, info.dwPageSize, MEM_COMMIT, PAGE_READWRITE);
// Get a pointer to the last 8 bytes
ptr = buffer + info.dwPageSize - len;
for (ii=0;ii<len;ii++)
{
ptr[ii] = 'a' + ii;
}
// pass that to strncpy();
strncpy(Str, ptr, len);
Str[len] = '\0';
}