Skip to content

Commit

Permalink
GCMemcard: Avoid undefined behavior in the static Format().
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Aug 24, 2020
1 parent 87135db commit 131eb91
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Source/Core/Core/HW/GCMemcard/GCMemcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,16 +1373,17 @@ bool GCMemcard::Format(u8* card_data, const CardFlashId& flash_id, u16 size_mbit
{
if (!card_data)
return false;
memset(card_data, 0xFF, BLOCK_SIZE * 3);
memset(card_data + BLOCK_SIZE * 3, 0, BLOCK_SIZE * 2);

*((Header*)card_data) =
Header(flash_id, size_mbits, shift_jis, rtc_bias, sram_language, format_time);
Header header(flash_id, size_mbits, shift_jis, rtc_bias, sram_language, format_time);
Directory dir;
BlockAlloc bat(size_mbits);

std::memcpy(&card_data[BLOCK_SIZE * 0], &header, BLOCK_SIZE);
std::memcpy(&card_data[BLOCK_SIZE * 1], &dir, BLOCK_SIZE);
std::memcpy(&card_data[BLOCK_SIZE * 2], &dir, BLOCK_SIZE);
std::memcpy(&card_data[BLOCK_SIZE * 3], &bat, BLOCK_SIZE);
std::memcpy(&card_data[BLOCK_SIZE * 4], &bat, BLOCK_SIZE);

*((Directory*)(card_data + BLOCK_SIZE)) = Directory();
*((Directory*)(card_data + BLOCK_SIZE * 2)) = Directory();
*((BlockAlloc*)(card_data + BLOCK_SIZE * 3)) = BlockAlloc(size_mbits);
*((BlockAlloc*)(card_data + BLOCK_SIZE * 4)) = BlockAlloc(size_mbits);
return true;
}

Expand Down

0 comments on commit 131eb91

Please sign in to comment.