Skip to content

Commit f22d23b

Browse files
committed
replace reinterpret_cast with memcpy
Avoids potential aliasing issues. Signed-off-by: Rosen Penev <[email protected]>
1 parent d4b053a commit f22d23b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/bmffimage.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#endif
2121

2222
// + standard includes
23+
#include <array>
2324
#include <cstdio>
2425
#include <cstring>
2526
#include <iostream>
@@ -86,8 +87,9 @@ BmffImage::BmffImage(BasicIo::UniquePtr io, bool /* create */, size_t max_box_de
8687
} // BmffImage::BmffImage
8788

8889
std::string BmffImage::toAscii(uint32_t n) {
89-
const auto p = reinterpret_cast<const char*>(&n);
90-
std::string result(p, p + 4);
90+
std::array<char, sizeof(uint32_t)> p;
91+
std::memcpy(p.data(), &n, sizeof(uint32_t));
92+
std::string result(p.begin(), p.end());
9193
if (!isBigEndianPlatform())
9294
std::reverse(result.begin(), result.end());
9395
// show 0 as _

src/jp2image.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ Jp2Image::Jp2Image(BasicIo::UniquePtr io, bool create) : Image(ImageType::jp2, m
112112

113113
// Obtains the ascii version from the box.type
114114
std::string Jp2Image::toAscii(uint32_t n) {
115-
const auto p = reinterpret_cast<const char*>(&n);
116-
std::string result(p, p + 4);
115+
std::array<char, sizeof(uint32_t)> p;
116+
std::memcpy(p.data(), &n, sizeof(uint32_t));
117+
std::string result(p.begin(), p.end());
117118
if (isBigEndianPlatform())
118119
return result;
119120
std::reverse(result.begin(), result.end());

0 commit comments

Comments
 (0)