Skip to content

Commit af081de

Browse files
authored
Remove _CRT_SECURE_NO_WARNINGS (#774)
- use *_s version of swprintf/wcsncpy/sscanf when possible Fixes: #770
1 parent 517880e commit af081de

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
@@ -66,6 +60,16 @@ typedef LONG NTSTATUS;
6660
#include <stdlib.h>
6761
#include <string.h>
6862

63+
/* MSVC secure CRT (VS2005+) provides swprintf_s/wcsncpy_s.
64+
Older MSVC and GCC/MinGW/Cygwin use the classic variants. */
65+
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
66+
#define HIDAPI_SWPRINTF swprintf_s
67+
#define HIDAPI_WCSNCPY(dest, dest_count, src) wcsncpy_s((dest), (dest_count), (src), _TRUNCATE)
68+
#else
69+
#define HIDAPI_SWPRINTF swprintf
70+
#define HIDAPI_WCSNCPY(dest, dest_count, src) wcsncpy((dest), (src), (dest_count))
71+
#endif
72+
6973
#ifdef MIN
7074
#undef MIN
7175
#endif
@@ -286,7 +290,8 @@ static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR
286290
if (!msg)
287291
return;
288292

289-
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);
293+
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);
294+
msg[msg_len] = L'\0';
290295

291296
if (printf_written < 0)
292297
{
@@ -1432,7 +1437,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev
14321437
return -1;
14331438
}
14341439

1435-
wcsncpy(string, dev->device_info->manufacturer_string, maxlen);
1440+
HIDAPI_WCSNCPY(string, maxlen, dev->device_info->manufacturer_string);
14361441
string[maxlen - 1] = L'\0';
14371442

14381443
register_string_error(dev, NULL);
@@ -1452,7 +1457,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch
14521457
return -1;
14531458
}
14541459

1455-
wcsncpy(string, dev->device_info->product_string, maxlen);
1460+
HIDAPI_WCSNCPY(string, maxlen, dev->device_info->product_string);
14561461
string[maxlen - 1] = L'\0';
14571462

14581463
register_string_error(dev, NULL);
@@ -1472,7 +1477,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de
14721477
return -1;
14731478
}
14741479

1475-
wcsncpy(string, dev->device_info->serial_number, maxlen);
1480+
HIDAPI_WCSNCPY(string, maxlen, dev->device_info->serial_number);
14761481
string[maxlen - 1] = L'\0';
14771482

14781483
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)