From 25879d90c3cc72faeb461b6a19a32718f405f4e7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 10:46:00 +0000 Subject: [PATCH] inputsystem: map touch finger ids to slots (iPhone tap crash) With function names in the build, the iPhone crash finally resolved to: CInputSystem::FingerEvent <- TouchSDLWatcher <- SDL_PushEvent <- SDL_SendTouch <- Emscripten_HandleTouch. The earlier "~30 s" out-of-bounds crashes had the same frame indices: they were the player's taps. FingerEvent used SDL's finger id directly as the index into m_touchAccumX/Y and only checked the upper bound. SDL ids are arbitrary 64-bit values; Android gives 0,1,2..., but in a browser they are the DOM Touch.identifier, which on iPhone Safari is large. Truncated to int it is often negative, so every tap wrote outside the arrays. TouchSDLWatcher now assigns each live finger a slot 0..9 on down/motion and frees it on up; FingerEvent and GetTouchAccumulators reject anything outside 0..TOUCH_FINGER_MAX_COUNT-1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Qy5DKdwTqGQqScWvJyNcft --- inputsystem/touch_sdl.cpp | 56 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/inputsystem/touch_sdl.cpp b/inputsystem/touch_sdl.cpp index 5eb80844e4..37a8a21bbf 100644 --- a/inputsystem/touch_sdl.cpp +++ b/inputsystem/touch_sdl.cpp @@ -13,6 +13,38 @@ // NOTE: This has to be the last file included! #include "tier0/memdbgon.h" +//----------------------------------------------------------------------------- +// SDL finger ids are arbitrary 64-bit values. Android happens to hand out +// 0, 1, 2..., but browsers pass the DOM Touch.identifier, which on iPhone +// Safari is a large number. Used directly as an index (truncated to int, so +// often negative) it wrote outside m_touchAccumX/Y on every tap: the iPhone +// "out of bounds memory access" in CInputSystem::FingerEvent. Map each live +// finger to a small slot instead. +//----------------------------------------------------------------------------- +static SDL_FingerID s_FingerSlotId[TOUCH_FINGER_MAX_COUNT]; +static bool s_FingerSlotUsed[TOUCH_FINGER_MAX_COUNT]; + +static int FingerSlot( SDL_FingerID id, bool bAllocate ) +{ + for ( int i = 0; i < TOUCH_FINGER_MAX_COUNT; i++ ) + { + if ( s_FingerSlotUsed[i] && s_FingerSlotId[i] == id ) + return i; + } + if ( !bAllocate ) + return -1; + for ( int i = 0; i < TOUCH_FINGER_MAX_COUNT; i++ ) + { + if ( !s_FingerSlotUsed[i] ) + { + s_FingerSlotUsed[i] = true; + s_FingerSlotId[i] = id; + return i; + } + } + return -1; // more fingers than slots: ignore the extra one +} + //----------------------------------------------------------------------------- // Handle the events coming from the Touch SDL subsystem. //----------------------------------------------------------------------------- @@ -22,15 +54,26 @@ int TouchSDLWatcher( void *userInfo, SDL_Event *event ) if( !event || !pInputSystem ) return 1; + int slot; switch ( event->type ) { case SDL_FINGERDOWN: - pInputSystem->FingerEvent( IE_FingerDown, event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); + slot = FingerSlot( event->tfinger.fingerId, true ); + if ( slot >= 0 ) + pInputSystem->FingerEvent( IE_FingerDown, slot, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); break; case SDL_FINGERUP: - pInputSystem->FingerEvent( IE_FingerUp, event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); + slot = FingerSlot( event->tfinger.fingerId, false ); + if ( slot >= 0 ) + { + pInputSystem->FingerEvent( IE_FingerUp, slot, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); + s_FingerSlotUsed[slot] = false; + } break; case SDL_FINGERMOTION: - pInputSystem->FingerEvent( IE_FingerMotion ,event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); + // A finger that went down before the watcher existed gets a slot here. + slot = FingerSlot( event->tfinger.fingerId, true ); + if ( slot >= 0 ) + pInputSystem->FingerEvent( IE_FingerMotion, slot, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy ); break; } @@ -66,6 +109,11 @@ void CInputSystem::ShutdownTouch() bool CInputSystem::GetTouchAccumulators( int fingerId, float &dx, float &dy ) { + if ( fingerId < 0 || fingerId >= TOUCH_FINGER_MAX_COUNT ) + { + dx = dy = 0.f; + return false; + } dx = m_touchAccumX[fingerId]; dy = m_touchAccumY[fingerId]; @@ -76,7 +124,7 @@ bool CInputSystem::GetTouchAccumulators( int fingerId, float &dx, float &dy ) void CInputSystem::FingerEvent(int eventType, int fingerId, float x, float y, float dx, float dy) { - if( fingerId >= TOUCH_FINGER_MAX_COUNT ) + if( fingerId < 0 || fingerId >= TOUCH_FINGER_MAX_COUNT ) return; if( eventType == IE_FingerUp )