From 9e6d66b94e25f2637fde795c1fb001b40dce1d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Mon, 21 Nov 2022 10:29:13 +0100 Subject: [PATCH 1/4] Use our own implementation of wcsnlen() if not available This seems to be required to make our code compile with OpenBSD. See: https://mixxx.zulipchat.com/#narrow/stream/247620-development-help/topic/compiling.20mixxx.20on.20openbsd.20-current --- src/util/string.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/util/string.h b/src/util/string.h index 357235234ea..a204e84b7eb 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -35,27 +35,40 @@ class StringCollator { /// A nullptr-safe variant of the corresponding standard C function. /// /// Treats nullptr like an empty string and returns 0. +/// The c++11 strnlen_s() is not available on all targets inline std::size_t strnlen( const char* str, - std::size_t len) { + std::size_t maxlen) { if (str == nullptr) { return 0; } - // Invoke the global function - return ::strnlen(str, len); + // Invoke the global function and benefit from SIMD implementations + return ::strnlen(str, maxlen); } /// A nullptr-safe variant of the corresponding standard C function. /// /// Treats nullptr like an empty string and returns 0. +/// The c++11 wcsnlen_s is not available on all targets +/// and wcsnlen() is not available on OpenBSD inline std::size_t wcsnlen( const wchar_t* wcs, - std::size_t len) { + std::size_t maxlen) { if (wcs == nullptr) { return 0; } - // Invoke the global function - return ::wcsnlen(wcs, len); +#if !defined(__BSD__) || defined(__USE_XOPEN2K8) + // Invoke the global function SIMD implementations + return ::wcsnlen(wcs, maxlen); +#else + std::size_t n; + for (n = 0; n < maxlen; n++) { + if (!wcs[n]) { + break; + } + } + return n; +#endif } /// Convert a wide-character C string to QString. From 2c8d4d2d06528c63763ef7142ac662bd33018e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Tue, 22 Nov 2022 01:01:08 +0100 Subject: [PATCH 2/4] rename our wrapper strnlen_s and wcsnlen_s to match the standard names --- src/controllers/hid/hiddevice.cpp | 8 ++++---- src/util/string.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/controllers/hid/hiddevice.cpp b/src/controllers/hid/hiddevice.cpp index b78b5a65d3f..bff36b391c2 100644 --- a/src/controllers/hid/hiddevice.cpp +++ b/src/controllers/hid/hiddevice.cpp @@ -33,16 +33,16 @@ DeviceInfo::DeviceInfo( usage_page(device_info.usage_page), usage(device_info.usage), interface_number(device_info.interface_number), - m_pathRaw(device_info.path, mixxx::strnlen(device_info.path, PATH_MAX)), + m_pathRaw(device_info.path, mixxx::strnlen_s(device_info.path, PATH_MAX)), m_serialNumberRaw(device_info.serial_number, - mixxx::wcsnlen(device_info.serial_number, + mixxx::wcsnlen_s(device_info.serial_number, kDeviceInfoStringMaxLength)), m_manufacturerString(mixxx::convertWCStringToQString( device_info.manufacturer_string, - mixxx::wcsnlen(device_info.manufacturer_string, + mixxx::wcsnlen_s(device_info.manufacturer_string, kDeviceInfoStringMaxLength))), m_productString(mixxx::convertWCStringToQString(device_info.product_string, - mixxx::wcsnlen(device_info.product_string, + mixxx::wcsnlen_s(device_info.product_string, kDeviceInfoStringMaxLength))), m_serialNumber(mixxx::convertWCStringToQString( m_serialNumberRaw.data(), m_serialNumberRaw.size())) { diff --git a/src/util/string.h b/src/util/string.h index a204e84b7eb..0cbf3279180 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -36,7 +36,7 @@ class StringCollator { /// /// Treats nullptr like an empty string and returns 0. /// The c++11 strnlen_s() is not available on all targets -inline std::size_t strnlen( +inline std::size_t strnlen_s( const char* str, std::size_t maxlen) { if (str == nullptr) { @@ -51,7 +51,7 @@ inline std::size_t strnlen( /// Treats nullptr like an empty string and returns 0. /// The c++11 wcsnlen_s is not available on all targets /// and wcsnlen() is not available on OpenBSD -inline std::size_t wcsnlen( +inline std::size_t wcsnlen_s( const wchar_t* wcs, std::size_t maxlen) { if (wcs == nullptr) { From db1323ceb6376c578845cc4d054ac999d635aad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Tue, 22 Nov 2022 07:45:35 +0100 Subject: [PATCH 3/4] Fix comment strnlen_s is C11 not C++11 --- src/util/string.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/string.h b/src/util/string.h index 0cbf3279180..b221a68cc97 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -35,7 +35,7 @@ class StringCollator { /// A nullptr-safe variant of the corresponding standard C function. /// /// Treats nullptr like an empty string and returns 0. -/// The c++11 strnlen_s() is not available on all targets +/// The c11 strnlen_s() is not available on all targets inline std::size_t strnlen_s( const char* str, std::size_t maxlen) { @@ -49,7 +49,7 @@ inline std::size_t strnlen_s( /// A nullptr-safe variant of the corresponding standard C function. /// /// Treats nullptr like an empty string and returns 0. -/// The c++11 wcsnlen_s is not available on all targets +/// The c11 wcsnlen_s is not available on all targets /// and wcsnlen() is not available on OpenBSD inline std::size_t wcsnlen_s( const wchar_t* wcs, From 6c088bbf67e16614d0c6407de1023002cc4014d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Tue, 20 Dec 2022 13:44:19 +0100 Subject: [PATCH 4/4] replace one more wcsnlen by wcsnlen_s --- src/util/string.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/string.h b/src/util/string.h index b221a68cc97..1605acbe303 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -85,7 +85,7 @@ inline QString convertWCStringToQString( DEBUG_ASSERT(len == 0); return QString(); } - DEBUG_ASSERT(wcsnlen(wcs, len) == len); + DEBUG_ASSERT(wcsnlen_s(wcs, len) == len); const auto ilen = static_cast(len); DEBUG_ASSERT(ilen >= 0); // unsigned -> signed switch (sizeof(wchar_t)) {