Skip to content

Commit

Permalink
Fix the windows build by fixing printf related issues (#641)
Browse files Browse the repository at this point in the history
- Fix printf/string-related issues
- CI: Turn on -Wformat-signedness for GNU-cc
- Windows: Assert that struct has the correct size
  • Loading branch information
cmorty committed Nov 17, 2023
1 parent 02c0f78 commit 4168d87
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 154 deletions.
33 changes: 17 additions & 16 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: GitHub Builds
on: [push, pull_request]

env:
NIX_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror
NIX_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror
GNU_COMPILE_FLAGS: -Wall -Wextra -pedantic -Werror -Wformat-signedness
MSVC_COMPILE_FLAGS: /W4 /WX

jobs:
Expand Down Expand Up @@ -112,8 +113,8 @@ jobs:
- name: Configure CMake
run: |
rm -rf build install
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}"
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}"
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}"
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}"
- name: Build CMake Shared
working-directory: build/shared
run: make install
Expand All @@ -139,7 +140,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_ROOT=install/shared \
-DCMAKE_INSTALL_PREFIX=install/shared_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/shared_test
make install
- name: Check CMake Export Package Static
Expand All @@ -149,7 +150,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_ROOT=install/static \
-DCMAKE_INSTALL_PREFIX=install/static_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/static_test
make install
Expand Down Expand Up @@ -282,7 +283,7 @@ jobs:
- name: Configure CMake MinGW
shell: cmd
run: |
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%"
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%"
- name: Build CMake MinGW
working-directory: build\mingw
run: cmake --build . --target install
Expand All @@ -303,7 +304,7 @@ jobs:
-S hidapisrc\hidtest ^
-Dhidapi_ROOT=install\mingw ^
-DCMAKE_INSTALL_PREFIX=install\mingw_test ^
"-DCMAKE_C_FLAGS=%NIX_COMPILE_FLAGS%"
"-DCMAKE_C_FLAGS=%GNU_COMPILE_FLAGS%"
cd build\mingw_test
cmake --build . --target install
- name: Run CTest MinGW
Expand Down Expand Up @@ -370,8 +371,8 @@ jobs:
- name: Configure CMake
run: |
rm -rf build install
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}"
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}"
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}"
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}"
- name: Configure Automake
working-directory: hidapisrc
run: |
Expand Down Expand Up @@ -411,7 +412,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_DIR=$PWD/install/shared-cmake/lib/cmake/hidapi \
-DCMAKE_INSTALL_PREFIX=install/shared_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/shared_test
ninja install
- name: Check CMake Export Package Static
Expand All @@ -422,7 +423,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_DIR=$PWD/install/static-cmake/lib/cmake/hidapi \
-DCMAKE_INSTALL_PREFIX=install/static_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/static_test
ninja install
Expand All @@ -442,8 +443,8 @@ jobs:
- name: Configure CMake
run: |
rm -rf build install
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}"
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}"
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}"
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}"
- name: Configure Automake
working-directory: hidapisrc
run: |
Expand Down Expand Up @@ -486,7 +487,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_ROOT=install/shared-cmake \
-DCMAKE_INSTALL_PREFIX=install/shared_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/shared_test
make install
- name: Check CMake Export Package Static
Expand All @@ -496,7 +497,7 @@ jobs:
-S hidapisrc/hidtest \
-Dhidapi_ROOT=install/static-cmake \
-DCMAKE_INSTALL_PREFIX=install/static_test \
"-DCMAKE_C_FLAGS=${NIX_COMPILE_FLAGS}"
"-DCMAKE_C_FLAGS=${GNU_COMPILE_FLAGS}"
cd build/static_test
make install
Expand All @@ -507,7 +508,7 @@ jobs:
container: alpine:edge
env:
# A bug in musl: https://www.openwall.com/lists/musl/2020/01/20/2
ALPINE_COMPILE_FLAGS: ${NIX_COMPILE_FLAGS} -Wno-overflow
ALPINE_COMPILE_FLAGS: ${GNU_COMPILE_FLAGS} -Wno-overflow
steps:
- uses: actions/checkout@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion hidtest/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void print_device(struct hid_device_info *cur_dev) {
printf(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
printf(" Bus type: %d (%s)\n", cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
printf(" Bus type: %u (%s)\n", (unsigned)cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
printf("\n");
}

Expand Down
2 changes: 1 addition & 1 deletion libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ uint16_t get_usb_code_for_current_locale(void)
return 0x0;

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

/* Chop off the encoding part, and make it lower case. */
Expand Down
8 changes: 8 additions & 0 deletions windows/hidapi_descriptor_reconstruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <windows.h>

#include "hidapi_hidsdi.h"
#include <assert.h>

#define NUM_OF_HIDP_REPORT_TYPES 3

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

// Note: This is risk-reduction-measure for this specific struct, as it has ULONG bit-field.
// Although very unlikely, it might still be possible that the compiler creates a memory layout that is
// not binary compatile.
// Other structs are not checked at the time of writing.
static_assert(sizeof(struct hid_pp_link_collection_node_) == 16,
"Size of struct hid_pp_link_collection_node_ not as expected. This might break binary compatibility");

typedef struct hidp_unknown_token_ {
UCHAR Token; /* Specifies the one-byte prefix of a global item. */
UCHAR Reserved[3];
Expand Down

0 comments on commit 4168d87

Please sign in to comment.