Skip to content

Commit 3a81d4d

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 3a81d4d

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ CMakeLists.txt.user
3030

3131
# doxgen output
3232
doxygen/html/
33+
34+
# Visual Studio Code + CMake
35+
.vscode/
36+
build/

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: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,40 @@
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_FSCANF fscanf_s
15+
#define HIDAPI_SCANSET_SIZE(buf) , (unsigned)_countof(buf)
16+
#else
17+
#define HIDAPI_SSCANF sscanf
18+
#define HIDAPI_FSCANF fscanf
19+
#define HIDAPI_SCANSET_SIZE(buf)
20+
#endif
21+
22+
#define sscanf HIDAPI_SSCANF
23+
24+
static const char* hidapi_strerror_compat(int errnum, char* buf, size_t buf_size)
25+
{
26+
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
27+
if (strerror_s(buf, buf_size, errnum) == 0) {
28+
return buf;
29+
}
30+
return "Unknown error";
31+
#else
32+
(void)buf;
33+
(void)buf_size;
34+
return strerror(errnum);
35+
#endif
36+
}
37+
1238
static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename)
1339
{
1440
FILE* file;
1541
errno_t err = fopen_s(&file, filename, "r");
1642

1743
if (err != 0) {
18-
fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, strerror(err));
44+
char err_buf[128];
45+
fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, hidapi_strerror_compat((int)err, err_buf, sizeof(err_buf)));
1946
return NULL;
2047
}
2148

@@ -49,8 +76,8 @@ static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename)
4976
if (sscanf(line, "dev->product_id = 0x%04hX\n", &product_id)) continue;
5077
if (sscanf(line, "dev->usage_page = 0x%04hX\n", &usage_page)) continue;
5178
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;
79+
if (sscanf(line, "dev->manufacturer_string = \"%127[^\"\n]", manufacturer_string HIDAPI_SCANSET_SIZE(manufacturer_string))) continue;
80+
if (sscanf(line, "dev->product_string = \"%127[^\"\n]", product_string HIDAPI_SCANSET_SIZE(product_string))) continue;
5481
if (sscanf(line, "dev->release_number = 0x%04hX\n", &release_number)) continue;
5582
if (sscanf(line, "dev->interface_number = %d\n", &interface_number)) continue;
5683
// if (sscanf(line, "dev->path = \"%127[^\"]\n", path)) continue;
@@ -456,14 +483,15 @@ static BOOLEAN read_hex_data_from_text_file(const char *filename, unsigned char
456483
FILE* file = NULL;
457484
errno_t err = fopen_s(&file, filename, "r");
458485
if (err != 0) {
459-
fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, strerror(err));
486+
char err_buf[128];
487+
fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, hidapi_strerror_compat((int)err, err_buf, sizeof(err_buf)));
460488
return FALSE;
461489
}
462490

463491
BOOLEAN result = TRUE;
464492
unsigned int val;
465493
char buf[16];
466-
while (fscanf(file, "%15s", buf) == 1) {
494+
while (HIDAPI_FSCANF(file, "%15s", buf HIDAPI_SCANSET_SIZE(buf)) == 1) {
467495
if (sscanf(buf, "0x%X", &val) != 1) {
468496
fprintf(stderr, "Invalid HEX text ('%s') file, got %s\n", filename, buf);
469497
result = FALSE;

0 commit comments

Comments
 (0)