From d07c9a55a647b48bcc3a8fd64bfe22c4eb4badf9 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 13 Apr 2017 12:06:20 -0500 Subject: [PATCH] events: Remove strict-aliasing warning Several opaque buffers are used to to wrap c++ classes to pass to the c layer. The reinterpret cast to c++ classes is fine as long as the underlying buffer is not interpreted as different incompatible types, or else the behaviour is undefined. In the equeue_tick_init function, placement new is used to initialize the buffers. However, this interprets the buffer as a simple array of bytes, not the actual class type. Later the buffer is casted to the class type. From the point of view of the compiler, these two types are incompatible, and the compiler is free to reorder the operations under the assumption that they can't affect each other. Reinterpet casting the buffer to a class pointer before using placement new insures that the buffer is only interpreted as a single type. Or simple using the return value from placement new will handle the aliasing appropriately. --- equeue_mbed.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/equeue_mbed.cpp b/equeue_mbed.cpp index ca9ecb3..bc0f10f 100644 --- a/equeue_mbed.cpp +++ b/equeue_mbed.cpp @@ -31,13 +31,12 @@ static void equeue_tick_init() { "The equeue_timer buffer must fit the class Timer"); MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker), "The equeue_ticker buffer must fit the class Ticker"); - new (equeue_timer) Timer; - new (equeue_ticker) Ticker; + Timer *timer = new (equeue_timer) Timer; + Ticker *ticker = new (equeue_ticker) Ticker; equeue_minutes = 0; - reinterpret_cast(equeue_timer)->start(); - reinterpret_cast(equeue_ticker) - ->attach_us(equeue_tick_update, 1000 << 16); + timer->start(); + ticker->attach_us(equeue_tick_update, 1000 << 16); equeue_tick_inited = true; }