From a17f3fb1ae3f4bcdedf80f2be0ba53e0659b0b94 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 19 Jun 2026 22:07:25 -0400 Subject: [PATCH 1/2] refactor(sonar): fix cpp:S3630 --- tests/fixtures/linux_backend_test_hooks.cpp | 62 ++++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/tests/fixtures/linux_backend_test_hooks.cpp b/tests/fixtures/linux_backend_test_hooks.cpp index f0ed96a..fd4bdf4 100644 --- a/tests/fixtures/linux_backend_test_hooks.cpp +++ b/tests/fixtures/linux_backend_test_hooks.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +80,39 @@ namespace lvh::detail::test { FakeLibevdevDevice device; }; + template + Target opaque_test_handle(Source *source) noexcept { + static_assert(sizeof(Target) == sizeof(source), "opaque test handles must preserve pointer size"); + return std::bit_cast(source); + } + + libevdev *libevdev_handle(FakeLibevdevDevice *device) noexcept { + return opaque_test_handle(device); + } + + FakeLibevdevDevice *fake_libevdev_device(libevdev *device) noexcept { + return opaque_test_handle(device); + } + + const FakeLibevdevDevice *fake_libevdev_device(const libevdev *device) noexcept { + return opaque_test_handle(device); + } + + libevdev_uinput *libevdev_uinput_handle(FakeLibevdevUinput *device) noexcept { + return opaque_test_handle(device); + } + + FakeLibevdevUinput *fake_libevdev_uinput(libevdev_uinput *device) noexcept { + return opaque_test_handle(device); + } + + #if defined(LIBVIRTUALHID_HAVE_XTEST) + Display *fake_display_handle() noexcept { + static std::byte display_handle {}; + return opaque_test_handle(&display_handle); + } + #endif + struct LinuxTestSyscalls { bool override_access = false; int access_result = 0; @@ -247,7 +281,7 @@ std::ptrdiff_t lvh_linux_test_read(int fd, void *buffer, std::size_t size) { #if defined(LIBVIRTUALHID_HAVE_XTEST) Display *lvh_linux_test_x_open_display(const char *) { - return reinterpret_cast(0x1); + return lvh::detail::test::fake_display_handle(); } int lvh_linux_test_x_close_display(Display *) { @@ -309,14 +343,14 @@ extern "C" libevdev *lvh_linux_test_libevdev_new() { if (lvh::detail::test::active_test_syscalls->libevdev_new_returns_null) { return nullptr; } - return reinterpret_cast(new lvh::detail::test::FakeLibevdevDevice); + return lvh::detail::test::libevdev_handle(new lvh::detail::test::FakeLibevdevDevice); } return ::libevdev_new(); } extern "C" void lvh_linux_test_libevdev_free(libevdev *device) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - delete reinterpret_cast(device); + delete lvh::detail::test::fake_libevdev_device(device); return; } ::libevdev_free(device); @@ -324,7 +358,7 @@ extern "C" void lvh_linux_test_libevdev_free(libevdev *device) { extern "C" void lvh_linux_test_libevdev_set_name(libevdev *device, const char *name) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - reinterpret_cast(device)->name = name == nullptr ? "" : name; + lvh::detail::test::fake_libevdev_device(device)->name = name == nullptr ? "" : name; return; } ::libevdev_set_name(device, name); @@ -332,7 +366,7 @@ extern "C" void lvh_linux_test_libevdev_set_name(libevdev *device, const char *n extern "C" void lvh_linux_test_libevdev_set_id_bustype(libevdev *device, int bustype) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - reinterpret_cast(device)->bustype = static_cast(bustype); + lvh::detail::test::fake_libevdev_device(device)->bustype = static_cast(bustype); return; } ::libevdev_set_id_bustype(device, bustype); @@ -340,7 +374,7 @@ extern "C" void lvh_linux_test_libevdev_set_id_bustype(libevdev *device, int bus extern "C" void lvh_linux_test_libevdev_set_id_vendor(libevdev *device, int vendor) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - reinterpret_cast(device)->vendor = static_cast(vendor); + lvh::detail::test::fake_libevdev_device(device)->vendor = static_cast(vendor); return; } ::libevdev_set_id_vendor(device, vendor); @@ -348,7 +382,7 @@ extern "C" void lvh_linux_test_libevdev_set_id_vendor(libevdev *device, int vend extern "C" void lvh_linux_test_libevdev_set_id_product(libevdev *device, int product) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - reinterpret_cast(device)->product = static_cast(product); + lvh::detail::test::fake_libevdev_device(device)->product = static_cast(product); return; } ::libevdev_set_id_product(device, product); @@ -356,7 +390,7 @@ extern "C" void lvh_linux_test_libevdev_set_id_product(libevdev *device, int pro extern "C" void lvh_linux_test_libevdev_set_id_version(libevdev *device, int version) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - reinterpret_cast(device)->version = static_cast(version); + lvh::detail::test::fake_libevdev_device(device)->version = static_cast(version); return; } ::libevdev_set_id_version(device, version); @@ -367,7 +401,7 @@ extern "C" int lvh_linux_test_libevdev_enable_event_type(libevdev *device, unsig if (lvh::detail::test::active_test_syscalls->fail_libevdev_event_type) { return -EIO; } - reinterpret_cast(device)->event_types.push_back(type); + lvh::detail::test::fake_libevdev_device(device)->event_types.push_back(type); return 0; } return ::libevdev_enable_event_type(device, type); @@ -397,7 +431,7 @@ extern "C" int lvh_linux_test_libevdev_enable_event_code( event_code.flat = absinfo->flat; event_code.resolution = absinfo->resolution; } - reinterpret_cast(device)->event_codes.push_back(event_code); + lvh::detail::test::fake_libevdev_device(device)->event_codes.push_back(event_code); return 0; } return ::libevdev_enable_event_code(device, type, code, data); @@ -408,7 +442,7 @@ extern "C" int lvh_linux_test_libevdev_enable_property(libevdev *device, unsigne if (lvh::detail::test::active_test_syscalls->fail_libevdev_property) { return -EIO; } - reinterpret_cast(device)->properties.push_back(property); + lvh::detail::test::fake_libevdev_device(device)->properties.push_back(property); return 0; } return ::libevdev_enable_property(device, property); @@ -424,9 +458,9 @@ extern "C" int lvh_linux_test_libevdev_uinput_create_from_device( return -EIO; } - const auto &fake_device = *reinterpret_cast(device); + const auto &fake_device = *lvh::detail::test::fake_libevdev_device(device); lvh::detail::test::active_test_syscalls->libevdev_devices.push_back(fake_device); - *uinput_device = reinterpret_cast(new lvh::detail::test::FakeLibevdevUinput {fake_device}); + *uinput_device = lvh::detail::test::libevdev_uinput_handle(new lvh::detail::test::FakeLibevdevUinput {fake_device}); return 0; } return ::libevdev_uinput_create_from_device(device, uinput_fd, uinput_device); @@ -435,7 +469,7 @@ extern "C" int lvh_linux_test_libevdev_uinput_create_from_device( extern "C" void lvh_linux_test_libevdev_uinput_destroy(libevdev_uinput *uinput_device) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { ++lvh::detail::test::active_test_syscalls->libevdev_destroy_count; - delete reinterpret_cast(uinput_device); + delete lvh::detail::test::fake_libevdev_uinput(uinput_device); return; } ::libevdev_uinput_destroy(uinput_device); From a6254883ed93e2928bedbc5f837890f9e2ef5813 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 19 Jun 2026 22:14:22 -0400 Subject: [PATCH 2/2] Additional sonar fixes --- tests/fixtures/linux_backend_test_hooks.cpp | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/fixtures/linux_backend_test_hooks.cpp b/tests/fixtures/linux_backend_test_hooks.cpp index fd4bdf4..f2babb1 100644 --- a/tests/fixtures/linux_backend_test_hooks.cpp +++ b/tests/fixtures/linux_backend_test_hooks.cpp @@ -147,6 +147,8 @@ namespace lvh::detail::test { bool fail_libevdev_event_code = false; bool fail_libevdev_property = false; bool fail_libevdev_create = false; + std::vector> owned_libevdev_devices; + std::vector> owned_libevdev_uinput_devices; std::vector libevdev_devices; std::size_t libevdev_destroy_count = 0; }; @@ -343,14 +345,23 @@ extern "C" libevdev *lvh_linux_test_libevdev_new() { if (lvh::detail::test::active_test_syscalls->libevdev_new_returns_null) { return nullptr; } - return lvh::detail::test::libevdev_handle(new lvh::detail::test::FakeLibevdevDevice); + auto fake_device = std::make_unique(); + auto *device = fake_device.get(); + lvh::detail::test::active_test_syscalls->owned_libevdev_devices.push_back(std::move(fake_device)); + return lvh::detail::test::libevdev_handle(device); } return ::libevdev_new(); } extern "C" void lvh_linux_test_libevdev_free(libevdev *device) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { - delete lvh::detail::test::fake_libevdev_device(device); + auto *fake_device = lvh::detail::test::fake_libevdev_device(device); + static_cast(std::erase_if( + lvh::detail::test::active_test_syscalls->owned_libevdev_devices, + [fake_device](const auto &owned_device) { + return owned_device.get() == fake_device; + } + )); return; } ::libevdev_free(device); @@ -460,7 +471,10 @@ extern "C" int lvh_linux_test_libevdev_uinput_create_from_device( const auto &fake_device = *lvh::detail::test::fake_libevdev_device(device); lvh::detail::test::active_test_syscalls->libevdev_devices.push_back(fake_device); - *uinput_device = lvh::detail::test::libevdev_uinput_handle(new lvh::detail::test::FakeLibevdevUinput {fake_device}); + auto fake_uinput_device = std::make_unique(lvh::detail::test::FakeLibevdevUinput {fake_device}); + auto *created_uinput_device = fake_uinput_device.get(); + lvh::detail::test::active_test_syscalls->owned_libevdev_uinput_devices.push_back(std::move(fake_uinput_device)); + *uinput_device = lvh::detail::test::libevdev_uinput_handle(created_uinput_device); return 0; } return ::libevdev_uinput_create_from_device(device, uinput_fd, uinput_device); @@ -469,7 +483,13 @@ extern "C" int lvh_linux_test_libevdev_uinput_create_from_device( extern "C" void lvh_linux_test_libevdev_uinput_destroy(libevdev_uinput *uinput_device) { if (lvh::detail::test::active_test_syscalls != nullptr && lvh::detail::test::active_test_syscalls->override_libevdev) { ++lvh::detail::test::active_test_syscalls->libevdev_destroy_count; - delete lvh::detail::test::fake_libevdev_uinput(uinput_device); + auto *fake_uinput_device = lvh::detail::test::fake_libevdev_uinput(uinput_device); + static_cast(std::erase_if( + lvh::detail::test::active_test_syscalls->owned_libevdev_uinput_devices, + [fake_uinput_device](const auto &owned_device) { + return owned_device.get() == fake_uinput_device; + } + )); return; } ::libevdev_uinput_destroy(uinput_device);