Skip to content

Commit

Permalink
Fix files with short paths crashing the emulator
Browse files Browse the repository at this point in the history
The file path being loaded is dynamically replaced
with an emulator-local copy stored in a SAVE
directory, which allows the original disk data to
remain unmodified.

In case the original file path is shorter than the
destination "save" path, the file name buffer was
undersized in some situations, which could cause a
crash on load.
  • Loading branch information
rzumer committed Jan 25, 2020
1 parent 0ffb82e commit be74c35
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions source/DiskImageHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,9 @@ ImageError_e CImageHelperBase::Open( LPCTSTR pszImageFilename,
!CopyFile(pszImageFilename, path, true))
return eIMAGE_ERROR_UNABLE_TO_OPEN;

ZeroMemory((void *) pszImageFilename, sizeof(pszImageFilename));
// Do not delete pszImageFilename off the heap, as
// it seems that its contents are reused elsewhere.
pszImageFilename = new CHAR[MAX_PATH];
strcpy((char *) pszImageFilename, path);

CloseHandle(pImageInfo->hFile);
Expand All @@ -1763,14 +1765,14 @@ ImageError_e CImageHelperBase::Open( LPCTSTR pszImageFilename,

// This omits some of the error detection done as part of CheckNormalFile(),
// and assumes that the save copy matches the original.
delete[] pImageInfo->pImageBuffer;
delete [] pImageInfo->pImageBuffer;
pImageInfo->pImageBuffer = new BYTE[pImageInfo->uImageSize];

DWORD dwBytesRead;
BOOL bRes = ReadFile(pImageInfo->hFile, pImageInfo->pImageBuffer, pImageInfo->uImageSize, &dwBytesRead, NULL);
if (!bRes || pImageInfo->uImageSize != dwBytesRead)
{
delete[] pImageInfo->pImageBuffer;
delete [] pImageInfo->pImageBuffer;
pImageInfo->pImageBuffer = NULL;
return eIMAGE_ERROR_BAD_SIZE;
}
Expand Down

0 comments on commit be74c35

Please sign in to comment.