Skip to content

Commit b18f547

Browse files
authored
Update rcore_desktop_sdl.c, fix crash when strncpy tries to copy using NULL pointer (#5359)
When SDL_GameControllerNameForIndex returns null, the app crashes. This was addressed earlier in PR#4859 though the fix submitted on PR #4859 was only fixing the crashing and not addressing the root cause.
1 parent be9a24e commit b18f547

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/platforms/rcore_desktop_sdl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,10 @@ void PollInputEvents(void)
17231723
CORE.Input.Gamepad.axisState[nextAvailableSlot][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
17241724
CORE.Input.Gamepad.axisState[nextAvailableSlot][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
17251725
memset(CORE.Input.Gamepad.name[nextAvailableSlot], 0, MAX_GAMEPAD_NAME_LENGTH);
1726-
strncpy(CORE.Input.Gamepad.name[nextAvailableSlot], SDL_GameControllerNameForIndex(nextAvailableSlot), MAX_GAMEPAD_NAME_LENGTH - 1);
1726+
if (SDL_GameControllerNameForIndex(nextAvailableSlot))
1727+
strncpy(CORE.Input.Gamepad.name[nextAvailableSlot], SDL_GameControllerNameForIndex(nextAvailableSlot), MAX_GAMEPAD_NAME_LENGTH - 1);
1728+
else
1729+
strncpy(CORE.Input.Gamepad.name[nextAvailableSlot], "Noname", 6);
17271730
}
17281731
else
17291732
{

0 commit comments

Comments
 (0)