From fdf1b61379f0675f050c475543209390856066a5 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Wed, 10 Jul 2024 23:28:25 +0200 Subject: [PATCH] test --- crnlib/crn_packed_uint.h | 5 +++-- inc/crn_defs.h | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) 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]); } }