SDL: Don't call scandir() inside of scandir()

From 57927a2458142192a402a4fbb41a77fe71894c22 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 18 Apr 2022 12:57:28 -0700
Subject: [PATCH] Don't call scandir() inside of scandir()

This works around a crash in address sanitizer
---
 src/joystick/linux/SDL_sysjoystick.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 07cacfbb870..a268c0d9b41 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -636,8 +636,10 @@ filter_entries(const struct dirent *entry)
     return IsJoystickDeviceNode(entry->d_name);
 }
 static int
-sort_entries(const struct dirent **a, const struct dirent **b)
+sort_entries(const void *_a, const void *_b)
 {
+    const struct dirent **a = (const struct dirent **)_a;
+    const struct dirent **b = (const struct dirent **)_b;
     int numA, numB;
     int offset;
 
@@ -682,7 +684,10 @@ LINUX_FallbackJoystickDetect(void)
             struct dirent **entries = NULL;
             char path[PATH_MAX];
 
-            count = scandir("/dev/input", &entries, filter_entries, sort_entries);
+            count = scandir("/dev/input", &entries, filter_entries, NULL);
+            if (count > 1) {
+                qsort(entries, count, sizeof(*entries), sort_entries);
+            }
             for (i = 0; i < count; ++i) {
                 SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
                 MaybeAddDevice(path);