From 58d3ea6548f03db43efb66af1c78676e6340ae56 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 28 Sep 2016 17:33:44 -0400 Subject: [PATCH] Don't rely on undefined behavior --- include/beast/websocket/detail/hybi13.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/include/beast/websocket/detail/hybi13.hpp b/include/beast/websocket/detail/hybi13.hpp index 154e8582..72999f68 100644 --- a/include/beast/websocket/detail/hybi13.hpp +++ b/include/beast/websocket/detail/hybi13.hpp @@ -23,15 +23,17 @@ template std::string make_sec_ws_key(Gen& g) { - union U + std::array a; + for(int i = 0; i < 16; i += 4) { - std::array a4; - std::array a16; - }; - U u; - for(int i = 0; i < 4; ++i) - u.a4[i] = g(); - return beast::detail::base64_encode(u.a16.data(), u.a16.size()); + auto const v = g(); + a[i ] = v & 0xff; + a[i+1] = (v >> 8) & 0xff; + a[i+2] = (v >> 16) & 0xff; + a[i+3] = (v >> 24) & 0xff; + } + return beast::detail::base64_encode( + a.data(), a.size()); } template