Commit 345c161feb52f39c4ebfc1cda0cf80c05ce699fc

Sam Lantinga 2021-09-22T09:06:45

Fixed some accidental uses of external C runtime functions

diff --git a/src/audio/nas/SDL_nasaudio.c b/src/audio/nas/SDL_nasaudio.c
index 1f7ef90..1650726 100644
--- a/src/audio/nas/SDL_nasaudio.c
+++ b/src/audio/nas/SDL_nasaudio.c
@@ -113,11 +113,11 @@ LoadNASLibrary(void)
             /* Copy error string so we can use it in a new SDL_SetError(). */
             const char *origerr = SDL_GetError();
             const size_t len = SDL_strlen(origerr) + 1;
-            char *err = (char *) alloca(len);
+            char *err = SDL_stack_alloc(char, len);
             SDL_strlcpy(err, origerr, len);
+            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s", nas_library, err);
+            SDL_stack_free(err);
             retval = -1;
-            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s",
-                         nas_library, err);
         } else {
             retval = load_nas_syms();
             if (retval < 0) {
diff --git a/src/core/linux/SDL_evdev_kbd.c b/src/core/linux/SDL_evdev_kbd.c
index 4ea6b14..6792d8a 100644
--- a/src/core/linux/SDL_evdev_kbd.c
+++ b/src/core/linux/SDL_evdev_kbd.c
@@ -386,7 +386,7 @@ SDL_EVDEV_kbd_init(void)
         }
 
         /* Allow inhibiting keyboard mute with env. variable for debugging etc. */
-        if (getenv("SDL_INPUT_LINUX_KEEP_KBD") == NULL) {
+        if (SDL_getenv("SDL_INPUT_LINUX_KEEP_KBD") == NULL) {
             /* Mute the keyboard so keystrokes only generate evdev events
              * and do not leak through to the console
              */
diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c
index fa5beb8..c39c07a 100644
--- a/src/filesystem/unix/SDL_sysfilesystem.c
+++ b/src/filesystem/unix/SDL_sysfilesystem.c
@@ -245,7 +245,7 @@ SDL_GetBasePath(void)
 
     if (retval != NULL) {
         /* try to shrink buffer... */
-        char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
+        char *ptr = (char *) SDL_realloc(retval, SDL_strlen(retval) + 1);
         if (ptr != NULL)
             retval = ptr;  /* oh well if it failed. */
     }
diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index b183ca7..555fd83 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -166,7 +166,7 @@ SDL_SYS_HapticInit(void)
     i = 0;
     for (j = 0; j < MAX_HAPTICS; ++j) {
 
-        snprintf(path, PATH_MAX, joydev_pattern, i++);
+        SDL_snprintf(path, PATH_MAX, joydev_pattern, i++);
         MaybeAddDevice(path);
     }
 
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 610bf35..70fa710 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -621,7 +621,7 @@ static SDL_bool BReadDeviceInfo(SDL_DriverSwitch_Context *ctx)
         ctx->m_eControllerType = (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType;
 
         // Bytes 4-9: MAC address (big-endian)
-        memcpy(ctx->m_rgucMACAddress, reply->deviceInfo.rgucMACAddress, sizeof(ctx->m_rgucMACAddress));
+        SDL_memcpy(ctx->m_rgucMACAddress, reply->deviceInfo.rgucMACAddress, sizeof(ctx->m_rgucMACAddress));
 
         return SDL_TRUE;
     }
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 0aa850d..73b7ac9 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -482,7 +482,7 @@ HIDAPI_UpdateDiscovery()
                 if (buf.event.len > 0 &&
                     !SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
                     if (StrHasPrefix(buf.event.name, "hidraw") &&
-                        StrIsInteger(buf.event.name + strlen ("hidraw"))) {
+                        StrIsInteger(buf.event.name + SDL_strlen ("hidraw"))) {
                         SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
                         /* We found an hidraw change. We still continue to
                          * drain the inotify fd to avoid leaving old
@@ -494,7 +494,7 @@ HIDAPI_UpdateDiscovery()
                 remain -= len;
 
                 if (remain != 0) {
-                    memmove(&buf.storage[0], &buf.storage[len], remain);
+                    SDL_memmove(&buf.storage[0], &buf.storage[len], remain);
                 }
             }
         }
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 88aea57..984005f 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -529,7 +529,7 @@ LINUX_InotifyJoystickDetect(void)
     while (remain > 0) {
         if (buf.event.len > 0) {
             if (StrHasPrefix(buf.event.name, "event") &&
-                StrIsInteger(buf.event.name + strlen ("event"))) {
+                StrIsInteger(buf.event.name + SDL_strlen ("event"))) {
                 char path[PATH_MAX];
 
                 SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", buf.event.name);
@@ -547,7 +547,7 @@ LINUX_InotifyJoystickDetect(void)
         remain -= len;
 
         if (remain != 0) {
-            memmove (&buf.storage[0], &buf.storage[len], remain);
+            SDL_memmove (&buf.storage[0], &buf.storage[len], remain);
         }
     }
 }
diff --git a/src/power/linux/SDL_syspower.c b/src/power/linux/SDL_syspower.c
index dee691d..f911005 100644
--- a/src/power/linux/SDL_syspower.c
+++ b/src/power/linux/SDL_syspower.c
@@ -44,14 +44,17 @@ static const char *sys_class_power_supply_path = "/sys/class/power_supply";
 static int
 open_power_file(const char *base, const char *node, const char *key)
 {
-    const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
-    char *path = (char *) alloca(pathlen);
+    int fd;
+    const size_t pathlen = SDL_strlen(base) + SDL_strlen(node) + SDL_strlen(key) + 3;
+    char *path = SDL_stack_alloc(char, pathlen);
     if (path == NULL) {
         return -1;  /* oh well. */
     }
 
     snprintf(path, pathlen, "%s/%s/%s", base, node, key);
-    return open(path, O_RDONLY | O_CLOEXEC);
+    fd = open(path, O_RDONLY | O_CLOEXEC);
+    SDL_stack_free(path);
+    return fd;
 }
 
 
@@ -146,20 +149,20 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
 
     ptr = &state[0];
     while (make_proc_acpi_key_val(&ptr, &key, &val)) {
-        if (strcmp(key, "present") == 0) {
-            if (strcmp(val, "yes") == 0) {
+        if (SDL_strcmp(key, "present") == 0) {
+            if (SDL_strcmp(val, "yes") == 0) {
                 *have_battery = SDL_TRUE;
             }
-        } else if (strcmp(key, "charging state") == 0) {
+        } else if (SDL_strcmp(key, "charging state") == 0) {
             /* !!! FIXME: what exactly _does_ charging/discharging mean? */
-            if (strcmp(val, "charging/discharging") == 0) {
+            if (SDL_strcmp(val, "charging/discharging") == 0) {
                 charge = SDL_TRUE;
-            } else if (strcmp(val, "charging") == 0) {
+            } else if (SDL_strcmp(val, "charging") == 0) {
                 charge = SDL_TRUE;
             }
-        } else if (strcmp(key, "remaining capacity") == 0) {
+        } else if (SDL_strcmp(key, "remaining capacity") == 0) {
             char *endptr = NULL;
-            const int cvt = (int) strtol(val, &endptr, 10);
+            const int cvt = (int) SDL_strtol(val, &endptr, 10);
             if (*endptr == ' ') {
                 remaining = cvt;
             }
@@ -168,9 +171,9 @@ check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
 
     ptr = &info[0];
     while (make_proc_acpi_key_val(&ptr, &key, &val)) {
-        if (strcmp(key, "design capacity") == 0) {
+        if (SDL_strcmp(key, "design capacity") == 0) {
             char *endptr = NULL;
-            const int cvt = (int) strtol(val, &endptr, 10);
+            const int cvt = (int) SDL_strtol(val, &endptr, 10);
             if (*endptr == ' ') {
                 maximum = cvt;
             }
@@ -225,8 +228,8 @@ check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
 
     ptr = &state[0];
     while (make_proc_acpi_key_val(&ptr, &key, &val)) {
-        if (strcmp(key, "state") == 0) {
-            if (strcmp(val, "on-line") == 0) {
+        if (SDL_strcmp(key, "state") == 0) {
+            if (SDL_strcmp(val, "on-line") == 0) {
                 *have_ac = SDL_TRUE;
             }
         }
@@ -315,7 +318,7 @@ static SDL_bool
 int_string(char *str, int *val)
 {
     char *endptr = NULL;
-    *val = (int) strtol(str, &endptr, 0);
+    *val = (int) SDL_strtol(str, &endptr, 0);
     return ((*str != '\0') && (*endptr == '\0'));
 }
 
@@ -377,8 +380,8 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
     if (!next_string(&ptr, &str)) {     /* remaining battery life percent */
         return SDL_FALSE;
     }
-    if (str[strlen(str) - 1] == '%') {
-        str[strlen(str) - 1] = '\0';
+    if (str[SDL_strlen(str) - 1] == '%') {
+        str[SDL_strlen(str) - 1] = '\0';
     }
     if (!int_string(str, &battery_percent)) {
         return SDL_FALSE;
@@ -392,7 +395,7 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
 
     if (!next_string(&ptr, &str)) {     /* remaining battery life time units */
         return SDL_FALSE;
-    } else if (strcmp(str, "min") == 0) {
+    } else if (SDL_strcmp(str, "min") == 0) {
         battery_time *= 60;
     }
 
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index fbc2c96..95198fb 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -141,14 +141,12 @@ static int get_dricount(void)
 
     if (!(stat(KMSDRM_DRI_PATH, &sb) == 0
                 && S_ISDIR(sb.st_mode))) {
-        printf("The path %s cannot be opened or is not available\n",
-               KMSDRM_DRI_PATH);
+        /*printf("The path %s cannot be opened or is not available\n", KMSDRM_DRI_PATH);*/
         return 0;
     }
 
     if (access(KMSDRM_DRI_PATH, F_OK) == -1) {
-        printf("The path %s cannot be opened\n",
-               KMSDRM_DRI_PATH);
+        /*printf("The path %s cannot be opened\n", KMSDRM_DRI_PATH);*/
         return 0;
     }
 
diff --git a/src/video/kmsdrm/SDL_kmsdrmvulkan.c b/src/video/kmsdrm/SDL_kmsdrmvulkan.c
index 4e9d208..390271e 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvulkan.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvulkan.c
@@ -437,18 +437,18 @@ SDL_bool KMSDRM_Vulkan_CreateSurface(_THIS,
         }
 
         /* Get the list of displays supported by this plane. */
-        supported_displays = (VkDisplayKHR*)malloc(sizeof(VkDisplayKHR) * supported_displays_count);
+        supported_displays = (VkDisplayKHR*)SDL_malloc(sizeof(VkDisplayKHR) * supported_displays_count);
         vkGetDisplayPlaneSupportedDisplaysKHR(gpu, i,
             &supported_displays_count, supported_displays);
 
-	/* The plane must be bound to the chosen display, or not in use.
+        /* The plane must be bound to the chosen display, or not in use.
            If none of these is true, iterate to another plane. */
-	if (!((plane_props[i].currentDisplay == display) ||
-	      (plane_props[i].currentDisplay == VK_NULL_HANDLE))) 
+        if (!((plane_props[i].currentDisplay == display) ||
+              (plane_props[i].currentDisplay == VK_NULL_HANDLE))) 
             continue;
 
-	/* Iterate the list of displays supported by this plane
-	   in order to find out if the chosen display is among them. */
+        /* Iterate the list of displays supported by this plane
+           in order to find out if the chosen display is among them. */
         plane_supports_display = SDL_FALSE;
         for (j = 0; j < supported_displays_count; j++) {
             if (supported_displays[j] == display) {
@@ -457,9 +457,10 @@ SDL_bool KMSDRM_Vulkan_CreateSurface(_THIS,
             }
         }
 
-	/* Free the list of displays supported by this plane. */
-        if (supported_displays)
-	    free(supported_displays);
+        /* Free the list of displays supported by this plane. */
+        if (supported_displays) {
+            SDL_free(supported_displays);
+        }
 
         /* If the display is not supported by this plane, iterate to the next plane. */
         if (!plane_supports_display) {
@@ -469,9 +470,9 @@ SDL_bool KMSDRM_Vulkan_CreateSurface(_THIS,
         /* Want a plane that supports the alpha mode we have chosen. */
         vkGetDisplayPlaneCapabilitiesKHR(gpu, display_mode, i, &plane_caps);
         if (plane_caps.supportedAlpha == alpha_mode) {
-	    /* Yep, this plane is alright. */
-	    plane = i;
-	    break;
+            /* Yep, this plane is alright. */
+            plane = i;
+            break;
         }
     }