From 0feecbd6f3c95bf88a906cf05d950223fce342d1 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Fri, 31 Mar 2023 16:10:18 +0100 Subject: [PATCH] Copy the struct to align it, avoiding an ABI break Signed-off-by: Dave Rodgman --- include/mbedtls/timing.h | 2 +- library/timing.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/mbedtls/timing.h b/include/mbedtls/timing.h index 43ce52308fcf..597ef75211ce 100644 --- a/include/mbedtls/timing.h +++ b/include/mbedtls/timing.h @@ -42,7 +42,7 @@ extern "C" { * \brief timer structure */ struct mbedtls_timing_hr_time { - uint64_t opaque[4]; + unsigned char opaque[32]; }; /** diff --git a/library/timing.c b/library/timing.c index 47e34f922768..cd741295cf3f 100644 --- a/library/timing.c +++ b/library/timing.c @@ -17,6 +17,8 @@ * limitations under the License. */ +#include + #include "common.h" #include "mbedtls/platform.h" @@ -231,7 +233,10 @@ volatile int mbedtls_timing_alarmed = 0; 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 @@ void mbedtls_set_alarm(int seconds) 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);