Skip to content

Commit

Permalink
Copy the struct to align it, avoiding an ABI break
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Rodgman <[email protected]>
  • Loading branch information
daverodgman committed Mar 31, 2023
1 parent 1a0a2c6 commit 0feecbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/mbedtls/timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern "C" {
* \brief timer structure
*/
struct mbedtls_timing_hr_time {
uint64_t opaque[4];
unsigned char opaque[32];
};

/**
Expand Down
12 changes: 10 additions & 2 deletions library/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* limitations under the License.
*/

#include <string.h>

#include "common.h"

#include "mbedtls/platform.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0feecbd

Please sign in to comment.