diff --git a/crnlib/crn_packed_uint.h b/crnlib/crn_packed_uint.h index dedca4ae..71146342 100644 --- a/crnlib/crn_packed_uint.h +++ b/crnlib/crn_packed_uint.h @@ -39,14 +39,15 @@ struct packed_uint { } inline operator unsigned int() const { - switch (sizeof(m_buf) / sizeof(m_buf[0])) { + CRNLIB_ASSUME(sizeof(m_buf) == N); + switch (N) { case 1: return m_buf[0]; case 2: return (m_buf[0] << 8U) | m_buf[1]; case 3: return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]); - case 4: + default: return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]); } } diff --git a/inc/crn_defs.h b/inc/crn_defs.h index 0a457ccb..699b64aa 100644 --- a/inc/crn_defs.h +++ b/inc/crn_defs.h @@ -225,14 +225,15 @@ struct crn_packed_uint { } inline operator unsigned int() const { - switch (sizeof(m_buf) / sizeof(m_buf[0])) { + static_assert(sizeof(m_buf) == N, ""); + switch (N) { case 1: return m_buf[0]; case 2: return (m_buf[0] << 8U) | m_buf[1]; case 3: return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]); - case 4: + default: return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]); } }