Skip to content

Commit

Permalink
fixed UB due to overlapped dst & src in memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Dec 12, 2024
1 parent a2af963 commit 1f74417
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
22 changes: 11 additions & 11 deletions HephCommon/HeaderFiles/Buffers/BufferBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ namespace Heph
}
else if (rhs > 0)
{
(void)std::memcpy(this->pData, this->pData + rhs, this->SizeAsByte() - BufferBase::SizeAsByte(rhs));
(void)std::memmove(this->pData, this->pData + rhs, this->SizeAsByte() - BufferBase::SizeAsByte(rhs));
BufferBase::Initialize(this->pData + this->size - rhs, this->pData + this->size);
}

Expand Down Expand Up @@ -265,7 +265,7 @@ namespace Heph
}
else if (rhs > 0)
{
(void)std::memcpy(this->pData + rhs, this->pData, this->SizeAsByte() - BufferBase::SizeAsByte(rhs));
(void)std::memmove(this->pData + rhs, this->pData, this->SizeAsByte() - BufferBase::SizeAsByte(rhs));
BufferBase::Initialize(this->pData, this->pData + rhs);
}
return *(Tself*)this;
Expand Down Expand Up @@ -645,12 +645,12 @@ namespace Heph

if (pThisData == pRhsData)
{
(void)std::memcpy(((uint8_t*)pResultData) + rhsSize_byte, pResultData, thisSize_byte);
(void)std::memcpy(pResultData, ((uint8_t*)pResultData) + rhsSize_byte, rhsSize_byte);
(void)std::memmove(((uint8_t*)pResultData) + rhsSize_byte, pResultData, thisSize_byte);
(void)std::memmove(pResultData, ((uint8_t*)pResultData) + rhsSize_byte, rhsSize_byte);
}
else
{
(void)std::memcpy(((uint8_t*)pResultData) + rhsSize_byte, pThisData, thisSize_byte);
(void)std::memmove(((uint8_t*)pResultData) + rhsSize_byte, pResultData, thisSize_byte);
(void)std::memcpy(pResultData, pRhsData, rhsSize_byte);
}

Expand All @@ -676,7 +676,7 @@ namespace Heph

if (pThisData == pRhsData)
{
(void)std::memcpy(((uint8_t*)pResultData) + thisSize_byte, pResultData, rhsSize_byte);
(void)std::memmove(((uint8_t*)pResultData) + thisSize_byte, pResultData, rhsSize_byte);
}
else
{
Expand Down Expand Up @@ -717,20 +717,20 @@ namespace Heph

if (pThisData == pRhsData)
{
(void)std::memcpy(((uint8_t*)pResultData) + index_byte + rhsSize_byte, ((uint8_t*)pResultData) + index_byte, thisSize_byte - index_byte);
(void)std::memmove(((uint8_t*)pResultData) + index_byte + rhsSize_byte, ((uint8_t*)pResultData) + index_byte, thisSize_byte - index_byte);
if (rhsSize_byte > index_byte)
{
(void)std::memcpy(((uint8_t*)pResultData) + index_byte, pResultData, index_byte);
(void)std::memcpy(((uint8_t*)pResultData) + 2 * index_byte, ((uint8_t*)pResultData) + index_byte + rhsSize_byte, rhsSize_byte - index_byte);
(void)std::memmove(((uint8_t*)pResultData) + index_byte, pResultData, index_byte);
(void)std::memmove(((uint8_t*)pResultData) + 2 * index_byte, ((uint8_t*)pResultData) + index_byte + rhsSize_byte, rhsSize_byte - index_byte);
}
else
{
(void)std::memcpy(((uint8_t*)pResultData) + index_byte, pResultData, rhsSize_byte);
(void)std::memmove(((uint8_t*)pResultData) + index_byte, pResultData, rhsSize_byte);
}
}
else
{
(void)std::memcpy(((uint8_t*)pResultData) + index_byte + rhsSize_byte, ((uint8_t*)pResultData) + index_byte, thisSize_byte - index_byte);
(void)std::memmove(((uint8_t*)pResultData) + index_byte + rhsSize_byte, ((uint8_t*)pResultData) + index_byte, thisSize_byte - index_byte);
(void)std::memcpy(((uint8_t*)pResultData) + index_byte, pRhsData, rhsSize_byte);
}

Expand Down
21 changes: 21 additions & 0 deletions test/HephCommon/StopwatchTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "gtest/gtest.h"
#include "Stopwatch.h"
#include <thread>
#include <chrono>

TEST(StopwatchTest, All)
{
constexpr double sleepDuration_ms = 100;

for (size_t i = 0; i < 2; ++i)
{
HEPH_SW_RESET;
std::this_thread::sleep_for(std::chrono::milliseconds((uint64_t)sleepDuration_ms));

EXPECT_GE(HEPH_SW_DT, sleepDuration_ms * 1e-3);
EXPECT_GE(HEPH_SW_DT_S, sleepDuration_ms * 1e-3);
EXPECT_GE(HEPH_SW_DT_MS, sleepDuration_ms);
EXPECT_GE(HEPH_SW_DT_US, sleepDuration_ms * 1e3);
EXPECT_GE(HEPH_SW_DT_NS, sleepDuration_ms * 1e6);
}
}
1 change: 1 addition & 0 deletions test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<ClCompile Include="HephCommon\EventTest.cpp" />
<ClCompile Include="HephCommon\HephMathTest.cpp" />
<ClCompile Include="HephCommon\HephSharedTest.cpp" />
<ClCompile Include="HephCommon\StopwatchTest.cpp" />
<ClCompile Include="HephCommon\StringHelpersTest.cpp" />
<ClCompile Include="HephCommon\UserEventArgsTest.cpp" />
</ItemGroup>
Expand Down

0 comments on commit 1f74417

Please sign in to comment.