From 8ab8b3c2a1e892824e1721aad77da64969ec99ac Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Tue, 26 Oct 2021 14:44:25 +0200 Subject: [PATCH] Fix negative array indices --- oscar64/ByteCodeGenerator.cpp | 18 +++++++++--------- oscar64/NativeCodeGenerator.cpp | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/oscar64/ByteCodeGenerator.cpp b/oscar64/ByteCodeGenerator.cpp index e0fb162..e5bb949 100644 --- a/oscar64/ByteCodeGenerator.cpp +++ b/oscar64/ByteCodeGenerator.cpp @@ -916,7 +916,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + 4 <= 256) + if (index >= 0 && index + 4 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -944,7 +944,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + 4 <= 256) + if (index >= 0 && index + 4 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -1113,7 +1113,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + 2 <= 256) + if (index >= 0 && index + 2 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -1141,7 +1141,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + 2 <= 256) + if (index >= 0 && index + 2 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -1579,7 +1579,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + InterTypeSize[ins->mSrc[0].mType] <= 256) + if (index >= 0 && index + InterTypeSize[ins->mSrc[0].mType] <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -1624,7 +1624,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI if (ins->mSrc[1].mMemory == IM_INDIRECT) { int index = ins->mSrc[1].mIntConst; - if (index + InterTypeSize[ins->mSrc[0].mType] <= 256) + if (index >= 0 && index + InterTypeSize[ins->mSrc[0].mType] <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; @@ -1736,7 +1736,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (ins->mSrc[0].mMemory == IM_INDIRECT) { int index = ins->mSrc[0].mIntConst; - if (index + 4 <= 256) + if (index >= 0 && index + 4 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[0].mTemp]; @@ -1823,7 +1823,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (ins->mSrc[0].mMemory == IM_INDIRECT) { int index = ins->mSrc[0].mIntConst; - if (index + 2 <= 256) + if (index >= 0 && index + 2 <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[0].mTemp]; @@ -2025,7 +2025,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (ins->mSrc[0].mMemory == IM_INDIRECT) { int index = ins->mSrc[0].mIntConst; - if (index + InterTypeSize[ins->mDst.mType] <= 256) + if (index >= 0 && index + InterTypeSize[ins->mDst.mType] <= 256) { ByteCodeInstruction lins(BC_ADDR_REG); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mSrc[0].mTemp]; diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index f92d516..886e7ab 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -2513,7 +2513,7 @@ void NativeCodeBasicBlock::LoadConstant(InterCodeProcedure* proc, const InterIns void NativeCodeBasicBlock::CheckFrameIndex(int& reg, int& index, int size) { - if (index + size > 256) + if (index < 0 || index + size > 256) { mIns.Push(NativeCodeInstruction(ASMIT_CLC, ASMIM_IMPLIED)); mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, reg));