From abc631432d06c8b2d075247ff031a9ba5d46307f Mon Sep 17 00:00:00 2001
From: tomcw <tomcw@users.noreply.github.com>
Date: Mon, 15 Nov 2021 20:26:53 +0000
Subject: [PATCH] HDD: fix edge-case for old save-state m_buf_ptr

---
 source/Harddisk.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/source/Harddisk.cpp b/source/Harddisk.cpp
index a0eadba5..b2152b86 100644
--- a/source/Harddisk.cpp
+++ b/source/Harddisk.cpp
@@ -792,6 +792,8 @@ bool HarddiskInterfaceCard::LoadSnapshotHDDUnit(YamlLoadHelper& yamlLoadHelper,
 	Disk_Status_e diskStatusNext = (Disk_Status_e) yamlLoadHelper.LoadUint(SS_YAML_KEY_STATUS_NEXT);
 	Disk_Status_e diskStatusPrev = (Disk_Status_e) yamlLoadHelper.LoadUint(SS_YAML_KEY_STATUS_PREV);
 	m_hardDiskDrive[unit].m_buf_ptr = yamlLoadHelper.LoadUint(SS_YAML_KEY_BUF_PTR);
+	if (m_hardDiskDrive[unit].m_buf_ptr >= sizeof(m_hardDiskDrive[unit].m_buf))	// pre-v3 save-states would leave m_buf_ptr==0x200 after reading a block
+		m_hardDiskDrive[unit].m_buf_ptr = sizeof(m_hardDiskDrive[unit].m_buf) - 1;
 
 	if (!yamlLoadHelper.GetSubMap(SS_YAML_KEY_BUF))
 		throw hddUnitName + std::string(": Missing: ") + std::string(SS_YAML_KEY_BUF);