2020 https://github.com/libusb/hidapi .
2121********************************************************/
2222
23- #if defined(_MSC_VER ) && !defined(_CRT_SECURE_NO_WARNINGS )
24- /* Do not warn about wcsncpy usage.
25- https://docs.microsoft.com/cpp/c-runtime-library/security-features-in-the-crt */
26- #define _CRT_SECURE_NO_WARNINGS
27- #endif
28-
2923#ifdef __cplusplus
3024extern "C" {
3125#endif
@@ -60,6 +54,16 @@ typedef LONG NTSTATUS;
6054#include <stdlib.h>
6155#include <string.h>
6256
57+ /* MSVC secure CRT (VS2005+) provides swprintf_s/wcsncpy_s.
58+ Older MSVC and GCC/MinGW/Cygwin use the classic variants. */
59+ #if defined(_MSC_VER ) && (_MSC_VER >= 1400 )
60+ #define HIDAPI_SWPRINTF swprintf_s
61+ #define HIDAPI_WCSNCPY (dest , dest_count , src ) wcsncpy_s((dest), (dest_count), (src), _TRUNCATE)
62+ #else
63+ #define HIDAPI_SWPRINTF swprintf
64+ #define HIDAPI_WCSNCPY (dest , dest_count , src ) wcsncpy((dest), (src), (dest_count))
65+ #endif
66+
6367#ifdef MIN
6468#undef MIN
6569#endif
@@ -280,7 +284,8 @@ static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR
280284 if (!msg )
281285 return ;
282286
283- int printf_written = swprintf (msg , msg_len + 1 , L"%.*ls: (0x%08X) %.*ls" , (int )op_len , op , error_code , (int )system_err_len , system_err_buf );
287+ int printf_written = HIDAPI_SWPRINTF (msg , msg_len + 1 , L"%.*ls: (0x%08X) %.*ls" , (int )op_len , op , error_code , (int )system_err_len , system_err_buf );
288+ msg [msg_len ] = L'\0' ;
284289
285290 if (printf_written < 0 )
286291 {
@@ -1426,7 +1431,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev
14261431 return -1 ;
14271432 }
14281433
1429- wcsncpy (string , dev -> device_info -> manufacturer_string , maxlen );
1434+ HIDAPI_WCSNCPY (string , maxlen , dev -> device_info -> manufacturer_string );
14301435 string [maxlen - 1 ] = L'\0' ;
14311436
14321437 register_string_error (dev , NULL );
@@ -1446,7 +1451,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch
14461451 return -1 ;
14471452 }
14481453
1449- wcsncpy (string , dev -> device_info -> product_string , maxlen );
1454+ HIDAPI_WCSNCPY (string , maxlen , dev -> device_info -> product_string );
14501455 string [maxlen - 1 ] = L'\0' ;
14511456
14521457 register_string_error (dev , NULL );
@@ -1466,7 +1471,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de
14661471 return -1 ;
14671472 }
14681473
1469- wcsncpy (string , dev -> device_info -> serial_number , maxlen );
1474+ HIDAPI_WCSNCPY (string , maxlen , dev -> device_info -> serial_number );
14701475 string [maxlen - 1 ] = L'\0' ;
14711476
14721477 register_string_error (dev , NULL );
0 commit comments