Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPLVerifyConfiguration(): make it rely only on static_assert() #9267

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions port/cpl_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
#endif
#include <string>

#if __cplusplus >= 202002L
#include <type_traits> // For std::endian
#endif

#include "cpl_config.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
Expand Down Expand Up @@ -186,8 +190,6 @@ void *CPLMalloc(size_t nSize)
if (nSize == 0)
return nullptr;

CPLVerifyConfiguration();

if ((nSize >> (8 * sizeof(nSize) - 1)) != 0)
{
// coverity[dead_error_begin]
Expand Down Expand Up @@ -1564,33 +1566,36 @@ int CPLPrintTime(char *pszBuffer, int nMaxLen, const char *pszFormat,
void CPLVerifyConfiguration()

{
static bool verified = false;
if (verified)
{
return;
}
verified = true;

/* -------------------------------------------------------------------- */
/* Verify data types. */
/* -------------------------------------------------------------------- */
CPL_STATIC_ASSERT(sizeof(GInt32) == 4);
CPL_STATIC_ASSERT(sizeof(GInt16) == 2);
CPL_STATIC_ASSERT(sizeof(GByte) == 1);
static_assert(sizeof(short) == 2); // We unfortunately rely on this
static_assert(sizeof(int) == 4); // We unfortunately rely on this
static_assert(sizeof(float) == 4); // We unfortunately rely on this
static_assert(sizeof(double) == 8); // We unfortunately rely on this
static_assert(sizeof(GInt64) == 8);
static_assert(sizeof(GInt32) == 4);
static_assert(sizeof(GInt16) == 2);
static_assert(sizeof(GByte) == 1);

/* -------------------------------------------------------------------- */
/* Verify byte order */
/* -------------------------------------------------------------------- */
GInt32 nTest = 1;

#ifdef CPL_LSB
if (reinterpret_cast<GByte *>(&nTest)[0] != 1)
#if __cplusplus >= 202002L
static_assert(std::endian::native == std::endian::little);
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
#endif
#ifdef CPL_MSB
if (reinterpret_cast<GByte *>(&nTest)[3] != 1)
#elif defined(CPL_MSB)
#if __cplusplus >= 202002L
static_assert(std::endian::native == std::endian::big);
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
static_assert(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
#endif
#else
#error "CPL_LSB or CPL_MSB must be defined"
#endif
CPLError(CE_Fatal, CPLE_AppDefined,
"CPLVerifyConfiguration(): byte order set wrong.");
}

#ifdef DEBUG_CONFIG_OPTIONS
Expand Down
Loading