diff options
author | Jan Holthuis <jan.holthuis@ruhr-uni-bochum.de> | 2020-11-24 15:54:32 +0100 |
---|---|---|
committer | Jan Holthuis <jan.holthuis@ruhr-uni-bochum.de> | 2020-11-24 18:11:12 +0100 |
commit | 64630b28f8911ac35eb8e86c7c1da6e639b3c78a (patch) | |
tree | b88bcd551ae9b020e0a2838a9c26c82b1dcb9b1c | |
parent | 623d099bcbabd02e8e853998deea273cd8bd08a4 (diff) |
HidController: Use strcpy/wcsncpy from C++ std namspace
Hopefully this fixes the C4996 warnings on Windows. These suggest to
replace the functions with Microsoft-only versions.
src\controllers\hid\hidcontroller.cpp(41): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\string.h(334): note: see declaration of 'strncpy'
src\controllers\hid\hidcontroller.cpp(48): warning C4996: 'wcsncpy': This function or variable may be unsafe. Consider using wcsncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\corecrt_wstring.h(200): note: see declaration of 'wcsncpy'
-rw-r--r-- | src/controllers/hid/hidcontroller.cpp | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/controllers/hid/hidcontroller.cpp b/src/controllers/hid/hidcontroller.cpp index b7695c92d4..26eccace4d 100644 --- a/src/controllers/hid/hidcontroller.cpp +++ b/src/controllers/hid/hidcontroller.cpp @@ -6,15 +6,22 @@ * */ -#include <wchar.h> -#include <string.h> - -#include "util/path.h" // for PATH_MAX on Windows #include "controllers/hid/hidcontroller.h" -#include "controllers/defs_controllers.h" -#include "util/trace.h" + +#include <cstring> +#include <cwchar> + #include "controllers/controllerdebug.h" +#include "controllers/defs_controllers.h" +#include "util/path.h" // for PATH_MAX on Windows #include "util/time.h" +#include "util/trace.h" + +namespace { +constexpr size_t kHidPathBufferSize = PATH_MAX + 1; +constexpr size_t kHidSerialMaxLength = 512; +constexpr size_t kHidSerialBufferSize = kHidSerialMaxLength + 1; +} // namespace HidController::HidController(const hid_device_info& deviceInfo, UserSettingsPointer pConfig) : Controller(pConfig), @@ -37,16 +44,15 @@ HidController::HidController(const hid_device_info& deviceInfo, UserSettingsPoin } // Don't trust path to be null terminated. - hid_path = new char[PATH_MAX + 1]; - strncpy(hid_path, deviceInfo.path, PATH_MAX); - hid_path[PATH_MAX] = 0; - - hid_serial_raw = NULL; - if (deviceInfo.serial_number != NULL) { - size_t serial_max_length = 512; - hid_serial_raw = new wchar_t[serial_max_length+1]; - wcsncpy(hid_serial_raw, deviceInfo.serial_number, serial_max_length); - hid_serial_raw[serial_max_length] = 0; + hid_path = new char[kHidPathBufferSize]; + std::strncpy(hid_path, deviceInfo.path, PATH_MAX); + hid_path[PATH_MAX] = '\0'; + + hid_serial_raw = nullptr; + if (deviceInfo.serial_number != nullptr) { + hid_serial_raw = new wchar_t[kHidSerialBufferSize]; + std::wcsncpy(hid_serial_raw, deviceInfo.serial_number, kHidSerialMaxLength); + hid_serial_raw[kHidSerialMaxLength] = '\0'; } hid_serial = safeDecodeWideString(deviceInfo.serial_number, 512); |