From: Francis Dupont Date: Wed, 21 Dec 2016 15:38:36 +0000 (+0100) Subject: [5091] Use a switch in writeInt<> to avoid a g++ bug X-Git-Tag: trac3590_base^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=931db90327cd275a817beadd3d2e1a38791ad712;p=thirdparty%2Fkea.git [5091] Use a switch in writeInt<> to avoid a g++ bug --- diff --git a/src/lib/dhcp/tests/option_custom_unittest.cc b/src/lib/dhcp/tests/option_custom_unittest.cc index c24c6b7795..98ac281862 100644 --- a/src/lib/dhcp/tests/option_custom_unittest.cc +++ b/src/lib/dhcp/tests/option_custom_unittest.cc @@ -99,9 +99,24 @@ public: /// @tparam integer type. template void writeInt(T value, std::vector& buf) { - // This loop is incorrectly compiled by some old g++?! - for (int i = 0; i < sizeof(T); ++i) { - buf.push_back(value >> ((sizeof(T) - i - 1) * 8) & 0xFF); + switch (sizeof(T)) { + case 4: + buf.push_back((value >> 24) & 0xFF); + /* falls into */ + case 3: + buf.push_back((value >> 16) & 0xFF); + /* falls into */ + case 2: + buf.push_back((value >> 8) & 0xFF); + /* falls into */ + case 1: + buf.push_back(value & 0xFF); + break; + default: + // This loop is incorrectly compiled by some old g++?! + for (int i = 0; i < sizeof(T); ++i) { + buf.push_back(value >> ((sizeof(T) - i - 1) * 8) & 0xFF); + } } } diff --git a/src/lib/dhcp/tests/option_data_types_unittest.cc b/src/lib/dhcp/tests/option_data_types_unittest.cc index cb914b2447..3661943dfa 100644 --- a/src/lib/dhcp/tests/option_data_types_unittest.cc +++ b/src/lib/dhcp/tests/option_data_types_unittest.cc @@ -44,9 +44,24 @@ public: /// @tparam integer type. template void writeInt(T value, std::vector& buf) { - // This loop is incorrectly compiled by some old g++?! - for (int i = 0; i < sizeof(T); ++i) { - buf.push_back(value >> ((sizeof(T) - i - 1) * 8) & 0xFF); + switch (sizeof(T)) { + case 4: + buf.push_back((value >> 24) & 0xFF); + /* falls into */ + case 3: + buf.push_back((value >> 16) & 0xFF); + /* falls into */ + case 2: + buf.push_back((value >> 8) & 0xFF); + /* falls into */ + case 1: + buf.push_back(value & 0xFF); + break; + default: + // This loop is incorrectly compiled by some old g++?! + for (int i = 0; i < sizeof(T); ++i) { + buf.push_back(value >> ((sizeof(T) - i - 1) * 8) & 0xFF); + } } }