From 130d5298fcbb867686d833534d5879714f681991 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Mon, 18 Jul 2016 12:32:25 +0200 Subject: [PATCH] Fixes Undefined Behavior in LaneTupel (Strict Aliasing), resolves 2665 It's complicated :sigh: read this please: http://dbp-consulting.com/tutorials/StrictAliasing.html tl;dr: has to go through a memcpy (in C++) as in: https://github.com/WebAssembly/binaryen/blob/184cc11cee2a65d30c7696eb3284e132099e4acb/src/support/utilities.h#L29-L40 --- src/util/guidance/turn_lanes.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/util/guidance/turn_lanes.cpp b/src/util/guidance/turn_lanes.cpp index 79d461878..cba73fdb1 100644 --- a/src/util/guidance/turn_lanes.cpp +++ b/src/util/guidance/turn_lanes.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -24,10 +25,8 @@ LaneTupel::LaneTupel(const LaneID lanes_in_turn, const LaneID first_lane_from_th // comparation based on interpretation as unsigned 32bit integer bool LaneTupel::operator==(const LaneTupel other) const { - static_assert(sizeof(LaneTupel) == sizeof(std::uint16_t), - "Comparation requires LaneTupel to be the of size 16Bit"); - return *reinterpret_cast(this) == - *reinterpret_cast(&other); + return std::tie(lanes_in_turn, first_lane_from_the_right) == + std::tie(other.lanes_in_turn, other.first_lane_from_the_right); } bool LaneTupel::operator!=(const LaneTupel other) const { return !(*this == other); } @@ -35,10 +34,8 @@ bool LaneTupel::operator!=(const LaneTupel other) const { return !(*this == othe // comparation based on interpretation as unsigned 32bit integer bool LaneTupel::operator<(const LaneTupel other) const { - static_assert(sizeof(LaneTupel) == sizeof(std::uint16_t), - "Comparation requires LaneTupel to be the of size 16Bit"); - return *reinterpret_cast(this) < - *reinterpret_cast(&other); + return std::tie(lanes_in_turn, first_lane_from_the_right) < + std::tie(other.lanes_in_turn, other.first_lane_from_the_right); } } // namespace guidance