Copy the struct to align it, avoiding an ABI break
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
diff --git a/library/timing.c b/library/timing.c
index 47e34f9..cd74129 100644
--- a/library/timing.c
+++ b/library/timing.c
@@ -17,6 +17,8 @@
* limitations under the License.
*/
+#include <string.h>
+
#include "common.h"
#include "mbedtls/platform.h"
@@ -231,7 +233,10 @@
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
- struct _hr_time *t = (struct _hr_time *) val;
+ /* Copy val to an 8-byte-aligned address, so that we can safely cast it */
+ uint64_t val_aligned[(sizeof(struct mbedtls_timing_hr_time) + 7) / 8];
+ memcpy(val_aligned, val, sizeof(struct _hr_time));
+ struct _hr_time *t = (struct _hr_time *)val_aligned;
if (reset) {
QueryPerformanceCounter(&t->start);
@@ -277,7 +282,10 @@
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
- struct _hr_time *t = (struct _hr_time *) val;
+ /* Copy val to an 8-byte-aligned address, so that we can safely cast it */
+ uint64_t val_aligned[(sizeof(struct mbedtls_timing_hr_time) + 7) / 8];
+ memcpy(val_aligned, val, sizeof(struct _hr_time));
+ struct _hr_time *t = (struct _hr_time *)val_aligned;
if (reset) {
gettimeofday(&t->start, NULL);