From f75b0aa814ed46a1ea7cf3f2f99c33429db9466d Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 7 Aug 2024 18:13:00 -0700
Subject: [PATCH] kmsdrm: free the connector when looking for available devices
Fixes https://github.com/libsdl-org/SDL/issues/10499
(cherry picked from commit 1a57ea7fba9eda1e57f19f174f35201840234553)
(cherry picked from commit a5bff78d812f8387474fe5e824a49b48312598eb)
---
src/video/kmsdrm/SDL_kmsdrmvideo.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 307b4b67c2f59..5510a4cb550b7 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -97,7 +97,7 @@ static int get_driindex(void)
SDL_strlcpy(device + kmsdrm_dri_pathsize, kmsdrm_dri_devname,
sizeof(device) - kmsdrm_dri_devnamesize);
- while((res = readdir(folder)) != NULL) {
+ while((res = readdir(folder)) != NULL && available < 0) {
if (SDL_memcmp(res->d_name, kmsdrm_dri_devname,
kmsdrm_dri_devnamesize) == 0) {
SDL_strlcpy(device + kmsdrm_dri_pathsize + kmsdrm_dri_devnamesize,
@@ -123,7 +123,7 @@ static int get_driindex(void)
resources->count_encoders > 0 &&
resources->count_crtcs > 0) {
available = -ENOENT;
- for (i = 0; i < resources->count_connectors; i++) {
+ for (i = 0; i < resources->count_connectors && available < 0; i++) {
drmModeConnector *conn =
KMSDRM_drmModeGetConnector(
drm_fd, resources->connectors[i]);
@@ -134,20 +134,21 @@ static int get_driindex(void)
if (conn->connection == DRM_MODE_CONNECTED &&
conn->count_modes) {
+ SDL_bool access_denied = SDL_FALSE;
if (SDL_GetHintBoolean(
SDL_HINT_KMSDRM_REQUIRE_DRM_MASTER,
SDL_TRUE)) {
/* Skip this device if we can't obtain
* DRM master */
KMSDRM_drmSetMaster(drm_fd);
- if (KMSDRM_drmAuthMagic(drm_fd, 0) ==
- -EACCES) {
- continue;
+ if (KMSDRM_drmAuthMagic(drm_fd, 0) == -EACCES) {
+ access_denied = SDL_TRUE;
}
}
- available = devindex;
- break;
+ if (!access_denied) {
+ available = devindex;
+ }
}
KMSDRM_drmModeFreeConnector(conn);
@@ -158,11 +159,10 @@ static int get_driindex(void)
SDL_KMSDRM_UnloadSymbols();
}
close(drm_fd);
+ } else {
+ SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
+ "Failed to open KMSDRM device %s, errno: %d\n", device, errno);
}
-
- SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
- "Failed to open KMSDRM device %s, errno: %d\n", device,
- errno);
}
}