diff --git a/include/etl/mem_cast.h b/include/etl/mem_cast.h index 853aa18c..35980787 100644 --- a/include/etl/mem_cast.h +++ b/include/etl/mem_cast.h @@ -115,7 +115,7 @@ namespace etl { ETL_STATIC_ASSERT(Size >= Other_Size, "Other size is too large"); - memcpy(buffer, other.buffer, Size_); + memcpy(buffer, other.data(), Other_Size); } //*********************************** @@ -126,7 +126,7 @@ namespace etl { ETL_STATIC_ASSERT(Size >= Other_Size, "RHS size is too large"); - memcpy(buffer, rhs.buffer, Size_); + memcpy(buffer, rhs.data(), Other_Size); return *this; } diff --git a/test/test_mem_cast.cpp b/test/test_mem_cast.cpp index 85bdae8f..717fe1d9 100644 --- a/test/test_mem_cast.cpp +++ b/test/test_mem_cast.cpp @@ -152,6 +152,24 @@ namespace CHECK_EQUAL(3, memCast.ref().a[2]); } + //************************************************************************* + // Cross-size copy/assignment must copy only the source's bytes and must + // not read past the end of the (smaller) source buffer. + TEST(test_mem_cast_copy_and_assign_from_smaller) + { + etl::mem_cast smaller; + smaller.assign(0x12345678UL); + + // Cross-size copy construct (destination larger than source). + etl::mem_cast larger_copy(smaller); + CHECK_EQUAL(0x12345678UL, larger_copy.ref()); + + // Cross-size assignment (destination larger than source). + etl::mem_cast larger_assign; + larger_assign = smaller; + CHECK_EQUAL(0x12345678UL, larger_assign.ref()); + } + //************************************************************************* TEST(test_mem_cast_assign_type_at_dynamic_offset) {