From 44e61e20e07371ba9dd4d016e831f3d142236d11 Mon Sep 17 00:00:00 2001 From: eboasson Date: Fri, 13 Sep 2019 12:12:16 +0200 Subject: [PATCH] Fix "type punning" warning in printing floats (#33) Signed-off-by: Erik Boasson --- .../include/rmw_cyclonedds_cpp/serdes.hpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/serdes.hpp b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/serdes.hpp index 0fb3954..dfbe01c 100755 --- a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/serdes.hpp +++ b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/serdes.hpp @@ -411,18 +411,22 @@ public: } inline void print(float & x) { + union { uint32_t u; float f; } tmp; align(sizeof(x)); - uint32_t z = *reinterpret_cast(data + pos); - if (swap_bytes) {z = bswap4u(z);} - prtf(&buf, &bufsize, "%f", *reinterpret_cast(&z)); + tmp.u = *reinterpret_cast(data + pos); + if (swap_bytes) {tmp.u = bswap4u(tmp.u);} + static_cast(tmp.u); + prtf(&buf, &bufsize, "%f", tmp.f); pos += sizeof(x); } inline void print(double & x) { + union { uint64_t u; double f; } tmp; align(sizeof(x)); - uint64_t z = *reinterpret_cast(data + pos); - if (swap_bytes) {z = bswap8u(z);} - prtf(&buf, &bufsize, "%f", *reinterpret_cast(&z)); + tmp.u = *reinterpret_cast(data + pos); + if (swap_bytes) {tmp.u = bswap8u(tmp.u);} + static_cast(tmp.u); + prtf(&buf, &bufsize, "%f", tmp.f); pos += sizeof(x); } inline uint32_t get32()