From bd8b2bbdd3cd1e03c1bd15e0c71dff9863fd101d Mon Sep 17 00:00:00 2001 From: "Matwey V. Kornilov" Date: Sun, 29 Nov 2015 20:15:36 +0300 Subject: [PATCH] Handle UB in bit_scan_forward Commit 37ece8e0068c2dc523443d7a0cbfb541852d5197 introduced UB into bit_scan_forward when called with zero agrument. Check m_type == UNDEFINED explicitly in to_type() Also replace bit_scan_forward return type to int to avoid double conevtion. --- include/eixx/connect/transport_msg.hpp | 2 +- include/eixx/util/common.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/eixx/connect/transport_msg.hpp b/include/eixx/connect/transport_msg.hpp index a63b2e0..8a9e942 100644 --- a/include/eixx/connect/transport_msg.hpp +++ b/include/eixx/connect/transport_msg.hpp @@ -108,7 +108,7 @@ class transport_msg { /// Transport message type transport_msg_type type() const { return m_type; } - int to_type() const { return bit_scan_forward(m_type); } + int to_type() const { return m_type == UNDEFINED ? 0 : bit_scan_forward(m_type); } const tuple& cntrl() const { return m_cntrl;} const eterm& msg() const { return m_msg; } /// Returns true when the transport message contains message payload diff --git a/include/eixx/util/common.hpp b/include/eixx/util/common.hpp index ab8be62..814fdbf 100644 --- a/include/eixx/util/common.hpp +++ b/include/eixx/util/common.hpp @@ -74,7 +74,8 @@ int __inline__ log2(unsigned long n, uint8_t base = 2) { return n == 1 ? 0 : 1+log2(n/base, base); } -static __inline__ unsigned long bit_scan_forward(unsigned long v) +/// Note, that bit_scan_forward(0) leads to UB +static __inline__ int bit_scan_forward(unsigned long v) { return __builtin_ctzl(v); }