From 0ee32250261eacaca13fc889f339cb1139756184 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Mon, 5 Jun 2017 20:45:55 +0100 Subject: [PATCH] Fix a bug in Disk_t: ZeroMemory() does not work for non POD. Disk_t is NOT a POD as it contains a std::string. Simply zeroing the memory is not a replacement for a constructor or for a .clear() method. This would cause access violation when the string strFilenameInZip is used (e.g. line 365). It is possible that in VS a std::string full of zeros is valid. This can be triggered inserting the same floppy in both drives. Signed-off-by: Andrea Odetti --- source/Disk.cpp | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/source/Disk.cpp b/source/Disk.cpp index 312b9785..de082a69 100644 --- a/source/Disk.cpp +++ b/source/Disk.cpp @@ -89,6 +89,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA DWORD writelight; int nibbles; // Init'd by ReadTrack() -> ImageReadTrack() + Disk_t() + { + clear(); + } + const Disk_t& operator= (const Disk_t& other) { memcpy(imagename, other.imagename, sizeof(imagename)); @@ -107,11 +112,29 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA nibbles = other.nibbles; return *this; } + + void clear() + { + ZeroMemory(imagename, sizeof(imagename)); + ZeroMemory(fullname, sizeof(imagename)); + strFilenameInZip.clear(); + imagehandle = NULL; + bWriteProtected = false; + track = 0; + trackimage = NULL; + phase = 0; + byte = 0; + trackimagedata = FALSE; + trackimagedirty = 0; + spinning = 0; + writelight = 0; + nibbles = 0; + } }; static WORD currdrive = 0; static BOOL diskaccessed = 0; -static Disk_t g_aFloppyDisk[NUM_DRIVES]; +static std::vector g_aFloppyDisk(NUM_DRIVES); static BYTE floppylatch = 0; static BOOL floppymotoron = 0; static BOOL floppyloadmode = 0; // for efficiency this is not used; it's extremely unlikely to affect emulation (nickw) @@ -551,7 +574,7 @@ void DiskInitialize(void) { int loop = NUM_DRIVES; while (loop--) - ZeroMemory(&g_aFloppyDisk[loop], sizeof(Disk_t)); + g_aFloppyDisk[loop].clear(); TCHAR imagefilename[MAX_PATH]; _tcscpy(imagefilename,g_sProgramDir); @@ -570,7 +593,7 @@ ImageError_e DiskInsert(const int iDrive, LPCTSTR pszImageFilename, const bool b { int track = fptr->track; int phase = fptr->phase; - ZeroMemory(fptr, sizeof(Disk_t)); + fptr->clear(); fptr->track = track; fptr->phase = phase; } @@ -1149,7 +1172,7 @@ int DiskSetSnapshot_v1(const SS_CARD_DISK2* const pSS) for(UINT i=0; i