Skip to content

Commit 4168d87

Browse files
authored
Fix the windows build by fixing printf related issues (#641)
- Fix printf/string-related issues - CI: Turn on -Wformat-signedness for GNU-cc - Windows: Assert that struct has the correct size
1 parent 02c0f78 commit 4168d87

File tree

6 files changed

+165
-154
lines changed

6 files changed

+165
-154
lines changed

.github/workflows/builds.yml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: GitHub Builds
33
on: [push, pull_request]
44

55
env:
6-
NIX_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror
6+
NIX_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror
7+
GNU_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror -Wformat-signedness
78
MSVC_COMPILE_FLAGS: /W4 /WX
89

910
jobs:
@@ -112,8 +113,8 @@ jobs:
112113
- name: Configure CMake
113114
run: |
114115
rm -rf build install
115-
cmake -B build/shared -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_ENABLE_ASAN=ON -DCMAKE_INSTALL_PREFIX=install/shared -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
116-
cmake -B build/static -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_ENABLE_ASAN=ON -DCMAKE_INSTALL_PREFIX=install/static -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
116+
cmake -B build/shared -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_ENABLE_ASAN=ON -DCMAKE_INSTALL_PREFIX=install/shared -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
117+
cmake -B build/static -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_ENABLE_ASAN=ON -DCMAKE_INSTALL_PREFIX=install/static -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
117118
- name: Build CMake Shared
118119
working-directory: build/shared
119120
run: make install
@@ -139,7 +140,7 @@ jobs:
139140
-S hidapisrc/hidtest \
140141
-Dhidapi_ROOT=install/shared \
141142
-DCMAKE_INSTALL_PREFIX=install/shared_test \
142-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
143+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
143144
cd build/shared_test
144145
make install
145146
- name: Check CMake Export Package Static
@@ -149,7 +150,7 @@ jobs:
149150
-S hidapisrc/hidtest \
150151
-Dhidapi_ROOT=install/static \
151152
-DCMAKE_INSTALL_PREFIX=install/static_test \
152-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
153+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
153154
cd build/static_test
154155
make install
155156
@@ -282,7 +283,7 @@ jobs:
282283
- name: Configure CMake MinGW
283284
shell: cmd
284285
run: |
285-
cmake -G"MinGW Makefiles" -B build\mingw -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_WITH_TESTS=ON -DHIDAPI_BUILD_PP_DATA_DUMP=ON -DCMAKE_INSTALL_PREFIX=install\mingw -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=%NIX_COMPILE_FLAGS%"
286+
cmake -G"MinGW Makefiles" -B build\mingw -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHIDAPI_WITH_TESTS=ON -DHIDAPI_BUILD_PP_DATA_DUMP=ON -DCMAKE_INSTALL_PREFIX=install\mingw -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=%GNU_COMPILE_FLAGS%"
286287
- name: Build CMake MinGW
287288
working-directory: build\mingw
288289
run: cmake --build . --target install
@@ -303,7 +304,7 @@ jobs:
303304
-S hidapisrc\hidtest ^
304305
-Dhidapi_ROOT=install\mingw ^
305306
-DCMAKE_INSTALL_PREFIX=install\mingw_test ^
306-
"-DCMAKE_C_FLAGS=%NIX_COMPILE_FLAGS%"
307+
"-DCMAKE_C_FLAGS=%GNU_COMPILE_FLAGS%"
307308
cd build\mingw_test
308309
cmake --build . --target install
309310
- name: Run CTest MinGW
@@ -370,8 +371,8 @@ jobs:
370371
- name: Configure CMake
371372
run: |
372373
rm -rf build install
373-
mingw64-cmake -B build/shared-cmake -S hidapisrc -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/shared-cmake -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
374-
mingw64-cmake -B build/static-cmake -S hidapisrc -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/static-cmake -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
374+
mingw64-cmake -B build/shared-cmake -S hidapisrc -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/shared-cmake -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
375+
mingw64-cmake -B build/static-cmake -S hidapisrc -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/static-cmake -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
375376
- name: Configure Automake
376377
working-directory: hidapisrc
377378
run: |
@@ -411,7 +412,7 @@ jobs:
411412
-S hidapisrc/hidtest \
412413
-Dhidapi_DIR=$PWD/install/shared-cmake/lib/cmake/hidapi \
413414
-DCMAKE_INSTALL_PREFIX=install/shared_test \
414-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
415+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
415416
cd build/shared_test
416417
ninja install
417418
- name: Check CMake Export Package Static
@@ -422,7 +423,7 @@ jobs:
422423
-S hidapisrc/hidtest \
423424
-Dhidapi_DIR=$PWD/install/static-cmake/lib/cmake/hidapi \
424425
-DCMAKE_INSTALL_PREFIX=install/static_test \
425-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
426+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
426427
cd build/static_test
427428
ninja install
428429
@@ -442,8 +443,8 @@ jobs:
442443
- name: Configure CMake
443444
run: |
444445
rm -rf build install
445-
cmake -B build/shared-cmake -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/shared-cmake -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
446-
cmake -B build/static-cmake -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/static-cmake -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
446+
cmake -B build/shared-cmake -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/shared-cmake -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
447+
cmake -B build/static-cmake -S hidapisrc -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=install/static-cmake -DBUILD_SHARED_LIBS=FALSE -DHIDAPI_BUILD_HIDTEST=ON "-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
447448
- name: Configure Automake
448449
working-directory: hidapisrc
449450
run: |
@@ -486,7 +487,7 @@ jobs:
486487
-S hidapisrc/hidtest \
487488
-Dhidapi_ROOT=install/shared-cmake \
488489
-DCMAKE_INSTALL_PREFIX=install/shared_test \
489-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
490+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
490491
cd build/shared_test
491492
make install
492493
- name: Check CMake Export Package Static
@@ -496,7 +497,7 @@ jobs:
496497
-S hidapisrc/hidtest \
497498
-Dhidapi_ROOT=install/static-cmake \
498499
-DCMAKE_INSTALL_PREFIX=install/static_test \
499-
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
500+
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
500501
cd build/static_test
501502
make install
502503
@@ -507,7 +508,7 @@ jobs:
507508
container: alpine:edge
508509
env:
509510
# A bug in musl: https://www.openwall.com/lists/musl/2020/01/20/2
510-
ALPINE_COMPILE_FLAGS: ${NIX_COMPILE_FLAGS} -Wno-overflow
511+
ALPINE_COMPILE_FLAGS: ${GNU_COMPILE_FLAGS} -Wno-overflow
511512
steps:
512513
- uses: actions/checkout@v3
513514
with:

hidtest/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void print_device(struct hid_device_info *cur_dev) {
7777
printf(" Release: %hx\n", cur_dev->release_number);
7878
printf(" Interface: %d\n", cur_dev->interface_number);
7979
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
80-
printf(" Bus type: %d (%s)\n", cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
80+
printf(" Bus type: %u (%s)\n", (unsigned)cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
8181
printf("\n");
8282
}
8383

libusb/hid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ uint16_t get_usb_code_for_current_locale(void)
19291929
return 0x0;
19301930

19311931
/* Make a copy of the current locale string. */
1932-
strncpy(search_string, locale, sizeof(search_string));
1932+
strncpy(search_string, locale, sizeof(search_string)-1);
19331933
search_string[sizeof(search_string)-1] = '\0';
19341934

19351935
/* Chop off the encoding part, and make it lower case. */

windows/hidapi_descriptor_reconstruct.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <windows.h>
3838

3939
#include "hidapi_hidsdi.h"
40+
#include <assert.h>
4041

4142
#define NUM_OF_HIDP_REPORT_TYPES 3
4243

@@ -125,6 +126,13 @@ typedef struct hid_pp_link_collection_node_ {
125126
// Same as the public API structure HIDP_LINK_COLLECTION_NODE, but without PVOID UserContext at the end
126127
} hid_pp_link_collection_node, *phid_pp_link_collection_node;
127128

129+
// Note: This is risk-reduction-measure for this specific struct, as it has ULONG bit-field.
130+
// Although very unlikely, it might still be possible that the compiler creates a memory layout that is
131+
// not binary compatile.
132+
// Other structs are not checked at the time of writing.
133+
static_assert(sizeof(struct hid_pp_link_collection_node_) == 16,
134+
"Size of struct hid_pp_link_collection_node_ not as expected. This might break binary compatibility");
135+
128136
typedef struct hidp_unknown_token_ {
129137
UCHAR Token; /* Specifies the one-byte prefix of a global item. */
130138
UCHAR Reserved[3];

0 commit comments

Comments
 (0)