Skip to content

Commit be3a8a1

Browse files
committed
Attempt to remove _CRT_SECURE_NO_WARNINGS
- use *_s version of swprintf/wcsncpy/sscanf when possible Fixes: #770
1 parent 90a2819 commit be3a8a1

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

windows/hid.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
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
3024
extern "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);

windows/hidapi_descriptor_reconstruct.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
#ifndef HIDAPI_DESCRIPTOR_RECONSTRUCT_H__
2020
#define HIDAPI_DESCRIPTOR_RECONSTRUCT_H__
2121

22-
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
23-
/* Do not warn about wcsncpy usage.
24-
https://docs.microsoft.com/cpp/c-runtime-library/security-features-in-the-crt */
25-
#define _CRT_SECURE_NO_WARNINGS
26-
#endif
27-
2822
#include "hidapi_winapi.h"
2923

3024
#ifdef _MSC_VER

windows/test/hid_report_reconstructor_test.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#include <stdio.h>
1010
#include <string.h>
1111

12+
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
13+
#define HIDAPI_SSCANF sscanf_s
14+
#define HIDAPI_SCANSET_SIZE(buf) , (unsigned)_countof(buf)
15+
#else
16+
#define HIDAPI_SSCANF sscanf
17+
#define HIDAPI_SCANSET_SIZE(buf)
18+
#endif
19+
1220
static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename)
1321
{
1422
FILE* file;
@@ -45,15 +53,15 @@ static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename)
4553
header_read_success = TRUE;
4654
break;
4755
}
48-
if (sscanf(line, "dev->vendor_id = 0x%04hX\n", &vendor_id)) continue;
49-
if (sscanf(line, "dev->product_id = 0x%04hX\n", &product_id)) continue;
50-
if (sscanf(line, "dev->usage_page = 0x%04hX\n", &usage_page)) continue;
51-
if (sscanf(line, "dev->usage = 0x%04hX\n", &usage)) continue;
52-
if (sscanf(line, "dev->manufacturer_string = \"%127[^\"\n]", manufacturer_string)) continue;
53-
if (sscanf(line, "dev->product_string = \"%127[^\"\n]", product_string)) continue;
54-
if (sscanf(line, "dev->release_number = 0x%04hX\n", &release_number)) continue;
55-
if (sscanf(line, "dev->interface_number = %d\n", &interface_number)) continue;
56-
// if (sscanf(line, "dev->path = \"%127[^\"]\n", path)) continue;
56+
if (HIDAPI_SSCANF(line, "dev->vendor_id = 0x%04hX\n", &vendor_id)) continue;
57+
if (HIDAPI_SSCANF(line, "dev->product_id = 0x%04hX\n", &product_id)) continue;
58+
if (HIDAPI_SSCANF(line, "dev->usage_page = 0x%04hX\n", &usage_page)) continue;
59+
if (HIDAPI_SSCANF(line, "dev->usage = 0x%04hX\n", &usage)) continue;
60+
if (HIDAPI_SSCANF(line, "dev->manufacturer_string = \"%127[^\"\n]", manufacturer_string HIDAPI_SCANSET_SIZE(manufacturer_string))) continue;
61+
if (HIDAPI_SSCANF(line, "dev->product_string = \"%127[^\"\n]", product_string HIDAPI_SCANSET_SIZE(product_string))) continue;
62+
if (HIDAPI_SSCANF(line, "dev->release_number = 0x%04hX\n", &release_number)) continue;
63+
if (HIDAPI_SSCANF(line, "dev->interface_number = %d\n", &interface_number)) continue;
64+
// if (HIDAPI_SSCANF(line, "dev->path = \"%127[^\"]\n", path)) continue;
5765
}
5866
if (!header_read_success) {
5967
fprintf(stderr, "ERROR: Couldn't read PP Data header (missing newline)\n");

0 commit comments

Comments
 (0)