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

OpenBSD: Use our own implementation of wcsnlen()if not available #11083

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions src/controllers/hid/hiddevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
29 changes: 21 additions & 8 deletions src/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
inline std::size_t strnlen(
/// The c11 strnlen_s() is not available on all targets
inline std::size_t strnlen_s(
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.
inline std::size_t wcsnlen(
/// 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,
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;
daschuer marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

/// Convert a wide-character C string to QString.
Expand Down