]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5091] Use a switch in writeInt<> to avoid a g++ bug
authorFrancis Dupont <fdupont@isc.org>
Wed, 21 Dec 2016 15:38:36 +0000 (16:38 +0100)
committerFrancis Dupont <fdupont@isc.org>
Wed, 21 Dec 2016 15:38:36 +0000 (16:38 +0100)
src/lib/dhcp/tests/option_custom_unittest.cc
src/lib/dhcp/tests/option_data_types_unittest.cc

index c24c6b77951ce30f7dc35c9128835b5d458d439b..98ac281862da2d5f258958b0b85aef5242c62041 100644 (file)
@@ -99,9 +99,24 @@ public:
     /// @tparam integer type.
     template<typename T>
     void writeInt(T value, std::vector<uint8_t>& 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);
+            }
         }
     }
 
index cb914b2447d19a39e0d1db17950f3d72b9c77a00..3661943dfa13d6c216f0bdc5b129449261cc06db 100644 (file)
@@ -44,9 +44,24 @@ public:
     /// @tparam integer type.
     template<typename T>
     void writeInt(T value, std::vector<uint8_t>& 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);
+            }
         }
     }