Files
SDL/test/testdescriptor.c
Antonio Ospite b9758218c8 Misc cleanup for src/joystick/hidapi/SDL_report_descriptor.c and new test program (#15959)
* testevdev: Always use decimal for HID report descriptors size

The size of the HID report descriptors in the SDL_COMPILE_TIME_ASSERT
checks is specified sometimes in decimal and sometimes in octal.

Always used decimal for consistency.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* SDL_report_descriptor: Add missing include guards in header file

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* SDL_report_descriptor: Fix some ignored return values

Fix some ignored return values resulting in incomplete error checking.

For instance:

  - ParseMainItem() was always returning true because the return value of
    AddInputFields() was not handled.

  - ParseLocalItem() was always returning true because the return value of
    AddUsage() was not handled.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* test: Add new test program for SDL_report_descriptor code

Add support for testing the SDL_report_descriptor code by adding a new
executable to make it easier to experiment with HID report descriptor
parsing.

The main() function which originally was in
src/joystick/hidapi/SDL_report_descriptor.c is moved to a separate file
named test/testdescriptor.c in order to simplify dealing with multiple
build systems.

For instance if the main() was in
src/joystick/hidapi/SDL_report_descriptor.c the code would have to
account for the cases when the file is built as part of the library or
when it's built as part of a standalone executable, in which case the
dynamic API mechanism needs to be disabled.

This would pose a problem especially with MSVC which uses pre-compiled
headers (PCH) which do not sit well with the preprocessor logic that
would be needed, like the hacks added in test/testevdev.c

Using a completely separate source file for the standalone executable
avoids the issue, especially considering that the new file is not built
with MSVC at all.

The new test program can be built with:

  cmake -DCMAKE_BUILD_TYPE=Debug -DSDL_TESTS=ON -S . -B build
  make -C testdescriptor

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* testdescriptor: Fix compiler warning

Fix compiler warning:

-----------------------------------------------------------------------
.../test/testdescriptor.c: In function ‘main’:
.../test/testdescriptor.c:56:17: warning: unused variable ‘file’ [-Wunused-variable]
   56 |     const char *file = argv[1];
      |                 ^~~~

-----------------------------------------------------------------------

Use the `file` variable when appropriate since it is already declared
and assigned.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* testdescriptor: Clean up descriptor context before exiting

For correctness clean up the descriptor parsing context before exiting,
even though this is not a problem in practice for a standalone
executable.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* testdescriptor: Exercise public API for parsing report descriptor

This gives some more coverage when running the SDL_report_descriptor
test program.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* SDL_report_descriptor: use size_t for variables that represent buffer sizes

Using size_t type for variables that represent buffer sizes in
src/joystick/hidapi/SDL_report_descriptor.[hc] addresses possible errors
when using the functions in combination with SDL_LoadFile, like in
test/testdescriptor.c

When building with CMake on Windows x64 the compilation was failing with
the following error:

-----------------------------------------------------------------------
D:\a\SDL\SDL\test\testdescriptor.c(69): error C2220: the following warning is treated as an error
D:\a\SDL\SDL\test\testdescriptor.c(69): warning C4267: 'function': conversion from 'size_t' to 'int', possible loss of data
-----------------------------------------------------------------------

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

* SDL_hidapi_xboxone: protect against passing negative size to SDL_ReadReportData

Since SDL_ReadReportData() is now using size_t for the buffer size, also
change its user HIDAPI_DriverXboxOne_HandleDescriptorReport to pass
size_t, but protecting against possible cases of passing negative values
to SDL_ReadReportData().

To do that it is enough to check that the "size" argument is not zero,
so it will never be negative when decremented (size--) a few lines
below.

Note: this just addresses a theoretical issue with future callers of
HIDAPI_DriverXboxOne_HandleDescriptorReport, the current code would be
already safe as the current caller already checks that size > 0.

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>

---------

Signed-off-by: Antonio Ospite <antonio.ospite@collabora.com>
2026-07-13 08:26:03 -07:00

77 lines
2.3 KiB
C

/*
Simple DirectMedia Layer
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
* Standalone driver for src/joystick/hidapi/SDL_report_descriptor.c, to make
* it easier to experiment with HID report descriptor parsing.
*/
/* Hack #1: avoid inclusion of SDL_main.h by SDL_internal.h */
#define SDL_main_h_
/* Hack #2: avoid dynapi renaming (must be done before #include <SDL3/SDL.h>) */
#include "../src/dynapi/SDL_dynapi.h"
#ifdef SDL_DYNAMIC_API
#undef SDL_DYNAMIC_API
#endif
#define SDL_DYNAMIC_API 0
#ifdef HAVE_BUILD_CONFIG
#include "../src/SDL_internal.h"
#endif
/* Hack #3: undo Hack #1 */
#ifdef SDL_main_h_
#undef SDL_main_h_
#endif
#ifdef SDL_MAIN_NOIMPL
#undef SDL_MAIN_NOIMPL
#endif
#include <SDL3/SDL_main.h>
#include "../src/joystick/hidapi/SDL_report_descriptor.h"
#include "../src/joystick/hidapi/SDL_report_descriptor.c"
int main(int argc, char *argv[])
{
const char *file = argv[1];
if (argc < 2) {
SDL_Log("Usage: %s file", argv[0]);
return 1;
}
size_t descriptor_size = 0;
Uint8 *descriptor = SDL_LoadFile(file, &descriptor_size);
if (!descriptor) {
SDL_Log("Couldn't load %s: %s", file, SDL_GetError());
return 2;
}
SDL_ReportDescriptor *sdl_descriptor = SDL_ParseReportDescriptor(descriptor, descriptor_size);
if (sdl_descriptor == NULL) {
SDL_Log("Couldn't parse %s: %s", file, SDL_GetError());
return 3;
}
SDL_DestroyDescriptor(sdl_descriptor);
return 0;
}