Skip to content

Commit 28a1997

Browse files
committed
gcc: callback: prevent wrong optimizations
Porting of arduino/ArduinoCore-mbed#755
1 parent 3636262 commit 28a1997

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

platform/include/platform/Callback.h

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
#include <mstd_type_traits>
2727
#include <mstd_functional>
2828

29+
#pragma GCC push_options
30+
// This prevents the GCC compiler from applying optimizations that assume the code follows strict aliasing rules.
31+
// In order to prevent bugs arising from undefined behavior that is tricky to find in the Callback implementation,
32+
// or simply from compiler bugs in GCC.
33+
#pragma GCC optimize("-fno-strict-aliasing")
34+
// This prevents the GCC compiler from generating incorrect inline code for the Callback constructor.
35+
#pragma GCC optimize("-fno-inline")
36+
2937
// Controlling switches from config:
3038
// MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL - support storing non-trivial function objects
3139
// MBED_CONF_PLATFORM_CALLBACK_COMPARABLE - support memcmp comparing stored objects (requires zero padding)
@@ -835,4 +843,6 @@ Callback(R(*func)(const volatile T *, ArgTs...), const volatile U *arg) -> Callb
835843

836844
} // namespace mbed
837845

846+
#pragma GCC pop_options
847+
838848
#endif

0 commit comments

Comments
 (0)