From 42a5bb16c6f008c0105bd9643abb85d7379219e0 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Fri, 31 Mar 2023 16:20:32 +0100 Subject: [PATCH] Fix failure to write back when reset != 0; tidy-up Signed-off-by: Dave Rodgman --- library/timing.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/timing.c b/library/timing.c index cd741295c..462d7b71a 100644 --- a/library/timing.c +++ b/library/timing.c @@ -234,12 +234,13 @@ volatile int mbedtls_timing_alarmed = 0; unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) { /* 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)); + uint64_t val_aligned[(sizeof(struct mbedtls_timing_hr_time) + sizeof(uint64_t) - 1) / sizeof(uint64_t)]; + memcpy(val_aligned, val, sizeof(struct mbedtls_timing_hr_time)); struct _hr_time *t = (struct _hr_time *)val_aligned; if (reset) { QueryPerformanceCounter(&t->start); + memcpy(val, t, sizeof(struct _hr_time)); return 0; } else { unsigned long delta; @@ -283,12 +284,13 @@ void mbedtls_set_alarm(int seconds) unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) { /* 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)); + uint64_t val_aligned[(sizeof(struct mbedtls_timing_hr_time) + sizeof(uint64_t) - 1) / sizeof(uint64_t)]; + memcpy(val_aligned, val, sizeof(struct mbedtls_timing_hr_time)); struct _hr_time *t = (struct _hr_time *)val_aligned; if (reset) { gettimeofday(&t->start, NULL); + memcpy(val, t, sizeof(struct _hr_time)); return 0; } else { unsigned long delta;