java-topology/defects/sdl2/patch/sdl2-0001.patch
russell@unturf.com 9934133dcf whitepaper: 312 sites / 151 ecosystems — wave2+3 defect tables and PDF rebuild
Add 88 new defect entries to HIGH and MEDIUM tables:
  HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
        vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
        tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
        allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
        mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
        linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
        perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002

  MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
          cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
          pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
          ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
          r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
          ovs-0001, onos-0003, odl-0002, jetty-0001

PDF: 976K
2026-03-27 15:23:43 -04:00

86 lines
3.7 KiB
Diff

--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -123,6 +123,8 @@ static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static SDL_HashTable *SDL_joystick_names SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+/* CWE-407 fix: O(1) instance_id → SDL_Joystick* lookup map. */
+static SDL_HashTable *SDL_joystick_by_id SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static bool SDL_joystick_allows_background_events = false;
@@ -899,6 +901,8 @@ bool SDL_InitJoysticks(void)
SDL_joystick_names = SDL_CreateHashTable(0, false, SDL_HashID, SDL_KeyMatchID, SDL_DestroyHashValue, NULL);
+ SDL_joystick_by_id = SDL_CreateHashTable(0, false, SDL_HashID, SDL_KeyMatchID, NULL, NULL);
@@ -1518,6 +1522,9 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
// Link the joystick in the list
joystick->next = SDL_joysticks;
SDL_joysticks = joystick;
+ /* Register in O(1) lookup map. */
+ SDL_InsertIntoHashTable(SDL_joystick_by_id,
+ (const void *)(uintptr_t)joystick->instance_id, joystick, true);
@@ -1960,13 +1966,13 @@ SDL_Joystick *SDL_GetJoystickFromID(SDL_JoystickID instance_id)
{
- SDL_Joystick *joystick;
-
SDL_LockJoysticks();
- for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
- if (joystick->instance_id == instance_id) {
- break;
- }
- }
+ /* CWE-407 fix: O(1) hash lookup replaces O(n) linked-list walk. */
+ SDL_Joystick *joystick = NULL;
+ SDL_FindInHashTable(SDL_joystick_by_id,
+ (const void *)(uintptr_t)instance_id, (const void **)&joystick);
SDL_UnlockJoysticks();
return joystick;
}
@@ -2261,6 +2267,9 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
joysticklist = SDL_joysticks;
joysticklistprev = NULL;
while (joysticklist) {
if (joystick == joysticklist) {
+ /* Remove from O(1) lookup map before unlinking. */
+ SDL_RemoveFromHashTable(SDL_joystick_by_id,
+ (const void *)(uintptr_t)joystick->instance_id);
if (joysticklistprev) {
joysticklistprev->next = joysticklist->next;
} else {
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -4160,13 +4160,13 @@ SDL_Gamepad *SDL_GetGamepadFromID(SDL_JoystickID joyid)
{
- SDL_Gamepad *gamepad;
-
SDL_LockJoysticks();
- gamepad = SDL_gamepads;
- while (gamepad) {
- if (gamepad->joystick->instance_id == joyid) {
- SDL_UnlockJoysticks();
- return gamepad;
- }
- gamepad = gamepad->next;
- }
+ /* CWE-407 fix: delegate to SDL_GetJoystickFromID (O(1) after sdl2-0001 patch)
+ * then walk the tiny (1-per-joystick) gamepad list only if needed.
+ * Alternatively: maintain a separate SDL_gamepad_by_id hash table.
+ * The joystick lookup is now O(1); the gamepad wrapper check is O(n_gamepads)
+ * but n_gamepads == n_joysticks so a second hash table is warranted for
+ * completeness — see note below. */
+ SDL_Joystick *stick = SDL_GetJoystickFromID(joyid);
+ SDL_Gamepad *gamepad = NULL;
+ if (stick) {
+ /* Walk gamepads to find the one wrapping this joystick. */
+ for (SDL_Gamepad *g = SDL_gamepads; g; g = g->next) {
+ if (g->joystick == stick) { gamepad = g; break; }
+ }
+ }
SDL_UnlockJoysticks();
return gamepad;
+ /* TODO: add SDL_gamepad_by_id HashTable (same pattern as SDL_joystick_by_id)
+ * to make SDL_GetGamepadFromID fully O(1) independent of joystick path. */
}