Hey I am experimenting with Unicode String a bit and I found a really weird behaviour…prolly a bug!
I expected the Unicode Init Method to copy the Buffer over but what happens in the following example code is simply unbelieveable:
PWCHAR pBuffer = (PWCHAR)ExAllocatePool(NonPagedPool,50*sizeof(WCHAR)); // 50 WCHARs should be more than enough for this name + 0x00 0x00
LARGE_INTEGER p = KeQueryPerformanceCounter(NULL);
int random = p.LowPart ^ p.HighPart;
if(pBuffer==NULL){
// EXALLOCATE FAILED, GIVE BACK SOME BOGUS STRING!
RtlInitUnicodeString(myUnicode,L"\SystemRoot\BogusFile");
return;
}
RtlStringCchPrintfW(pBuffer,50,L"\SystemRoot\cc-%d.dcg.ttf",random);
RtlInitUnicodeString(myUnicode,pBuffer);
DbgPrint(“Test1: %wZ\n”,myUnicode);
ExFreePool(pBuffer);
DbgPrint(“Test2: %wZ\n”,myUnicode);
Alright the surprizing output is:
Test1: \SystemRoot\cc-75263875.dcg.ttf
Test2: ??ystemRoot\cc-75263875.dcg.ttf
So freeing the temporary buffer results in changing the first 2 chars in the Unicodestring itself.
Isnt that weird?