diff --git a/src/DebugToolDrivers/Microchip/Protocols/Edbg/Avr/EdbgAvr8Interface.cpp b/src/DebugToolDrivers/Microchip/Protocols/Edbg/Avr/EdbgAvr8Interface.cpp index 3bfbf274..f6266dae 100644 --- a/src/DebugToolDrivers/Microchip/Protocols/Edbg/Avr/EdbgAvr8Interface.cpp +++ b/src/DebugToolDrivers/Microchip/Protocols/Edbg/Avr/EdbgAvr8Interface.cpp @@ -605,6 +605,10 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr TargetMemorySize bytes, const std::set& excludedAddressRanges ) { + if (bytes == 0) { + return {}; + } + if (this->programmingModeEnabled && memorySegmentDescriptor.type == TargetMemorySegmentType::RAM) { throw Exception{"Cannot access RAM when programming mode is enabled"}; } @@ -742,6 +746,10 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr TargetMemoryAddress startAddress, Targets::TargetMemoryBufferSpan buffer ) { + if (buffer.empty()) { + return; + } + if (memorySegmentDescriptor.type == TargetMemorySegmentType::FLASH) { if (this->session.configVariant == Avr8ConfigVariant::XMEGA) { const auto bootSectionStartAddress = this->session.programBootSection.value().get().startAddress; @@ -1614,6 +1622,8 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr TargetMemorySize bytes, const std::set& excludedAddresses ) { + assert(bytes > 0); + if (type == Avr8MemoryType::FUSES && this->session.configVariant == Avr8ConfigVariant::DEBUG_WIRE) { throw Exception{"Cannot access AVR fuses via the debugWIRE interface"}; } @@ -1732,6 +1742,8 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr TargetMemoryAddress startAddress, Targets::TargetMemoryBufferSpan buffer ) { + assert(!buffer.empty()); + if (type == Avr8MemoryType::FUSES && this->session.configVariant == Avr8ConfigVariant::DEBUG_WIRE) { throw Exception{"Cannot access AVR fuses via the debugWIRE interface"}; } @@ -1770,17 +1782,20 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr alignedBuffer.begin() + (startAddress - alignedStartAddress) ); - const auto dataBack = this->readMemory( - readMemType, - startAddress + bytes, - alignedBytes - bytes - (startAddress - alignedStartAddress), - {} - ); - std::copy( - dataBack.begin(), - dataBack.end(), - alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes - ); + const auto backAlignmentBytes = alignedBytes - bytes - (startAddress - alignedStartAddress); + if (backAlignmentBytes > 0) { + const auto dataBack = this->readMemory( + readMemType, + startAddress + bytes, + backAlignmentBytes, + {} + ); + std::copy( + dataBack.begin(), + dataBack.end(), + alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes + ); + } return this->writeMemory(type, alignedStartAddress, alignedBuffer); } diff --git a/src/DebugToolDrivers/Protocols/RiscVDebug/DebugTranslator.cpp b/src/DebugToolDrivers/Protocols/RiscVDebug/DebugTranslator.cpp index 7330d073..143817bf 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebug/DebugTranslator.cpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebug/DebugTranslator.cpp @@ -529,18 +529,21 @@ namespace DebugToolDrivers::Protocols::RiscVDebug alignedBuffer.begin() + (startAddress - alignedStartAddress) ); - const auto dataBack = this->readMemory( - addressSpaceDescriptor, - memorySegmentDescriptor, - startAddress + bytes, - alignedBytes - bytes - (startAddress - alignedStartAddress), - {} - ); - std::copy( - dataBack.begin(), - dataBack.end(), - alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes - ); + const auto backAlignmentBytes = alignedBytes - bytes - (startAddress - alignedStartAddress); + if (backAlignmentBytes > 0) { + const auto dataBack = this->readMemory( + addressSpaceDescriptor, + memorySegmentDescriptor, + startAddress + bytes, + backAlignmentBytes, + {} + ); + std::copy( + dataBack.begin(), + dataBack.end(), + alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes + ); + } return this->writeMemory( addressSpaceDescriptor, @@ -958,6 +961,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebug Targets::TargetMemoryAddress startAddress, Targets::TargetMemorySize bytes ) { + assert(bytes > 0); assert(startAddress % DebugTranslator::WORD_BYTE_SIZE == 0); assert(bytes % DebugTranslator::WORD_BYTE_SIZE == 0); @@ -1006,6 +1010,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebug Targets::TargetMemoryBufferSpan buffer ) { using DebugModule::Registers::MemoryAccessControlField; + assert(!buffer.empty()); assert(startAddress % DebugTranslator::WORD_BYTE_SIZE == 0); assert(buffer.size() % DebugTranslator::WORD_BYTE_SIZE == 0); @@ -1049,6 +1054,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebug Targets::TargetMemoryAddress startAddress, Targets::TargetMemorySize bytes ) { + assert(bytes > 0); assert(startAddress % DebugTranslator::WORD_BYTE_SIZE == 0); assert(bytes % DebugTranslator::WORD_BYTE_SIZE == 0); @@ -1186,6 +1192,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebug Targets::TargetMemoryAddress startAddress, Targets::TargetMemoryBufferSpan buffer ) { + assert(!buffer.empty()); assert(startAddress % DebugTranslator::WORD_BYTE_SIZE == 0); assert(buffer.size() % DebugTranslator::WORD_BYTE_SIZE == 0);