-
Corrupted Stack...
Hello...
As soon as this function returns the Debug Window in VisualC++ reports this error:
"Run-Time Check Failure #2 - Stack around the variable 'fBuffer' was corrupted."
Code:
int cFile::GetType(LPCWSTR filename)
{
HANDLE hFile = NULL;
char fBuffer[3];
DWORD nread = 0;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == NULL)
return -1;
ZeroMemory(fBuffer, sizeof(char) * 4);
::ReadFile(hFile, fBuffer, 4, &nread, NULL);
if (nread < 4)
return -1;
if (fBuffer[0] == 82 && fBuffer[1] == 73 && fBuffer[2] == 70 && fBuffer[3] == 70)
return FILE_TYPE_AUDIO;
if (fBuffer[0] == 77 && fBuffer[1] == 84 && fBuffer[1] == 104 && fBuffer[1] == 100)
return FILE_TYPE_MIDI;
return 0;
}
Why??
-
Because you're calling ZeroMemory() on a buffer of only 3 chars and telling it to zero out 4.
BTW, this is the C section, and that's C++ code. ;)
-
ReadFile also uses 4 bytes buffer
You should close the file in this function ( you do not return handle for future use)
your use of "magic" numbers is also not too good
-
yes...you're right
I've tried declaring fBuffer[4] instead of fBuffer[3] and everything works fine...
Anyway shouldn't fBuffer[3] be 4 bytes long (of type char)?
Thanks
-
char array[SIZE]
has exactly SIZE bytes... what you ask is what you get.
Valid indexes are from 0 to SIZE-1
-
Ooops!
I was thinking that an array had indexes from 0 to size...Well thanks a lot!