summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArnaud Bienner <abien@dolby.com>2020-08-04 16:42:29 +0200
committerArnaud Bienner <abien@dolby.com>2020-08-05 14:37:31 +0200
commitc39fb9011025f69eaca571b2de6c577dacd455ce (patch)
tree76fc6f326d5ed00652b464b54702d0e5a0262491 /src
parent474dfebead4e815d5ca374eeccb1c05360b31962 (diff)
Fix potential cast of misaligned memory, which has undefined behavior
Diffstat (limited to 'src')
-rw-r--r--src/KM_platform.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/KM_platform.h b/src/KM_platform.h
index defcd8a..3e50e54 100644
--- a/src/KM_platform.h
+++ b/src/KM_platform.h
@@ -64,6 +64,7 @@ typedef long long i64_t;
# endif // KM_WIN32
# include <stdio.h>
+# include <string.h>
# include <assert.h>
# include <stdlib.h>
# include <limits.h>
@@ -135,11 +136,17 @@ namespace Kumu
// read an integer from byte-structured storage
template<class T>
- inline T cp2i(const byte_t* p) { return *(T*)p; }
+ inline T cp2i(const byte_t* p) {
+ T value;
+ memcpy(&value, p, sizeof(T));
+ return value;
+ }
// write an integer to byte-structured storage
template<class T>
- inline void i2p(T i, byte_t* p) { *(T*)p = i; }
+ inline void i2p(T i, byte_t* p) {
+ memcpy(p, &i, sizeof(T));
+ }
# ifdef KM_BIG_ENDIAN