SDL: Vulkan: Make sure validation layer name is in-scope

From b8a52c12377428f8743f503fbdc80a032a67391e Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Sun, 25 Feb 2024 16:35:34 +0800
Subject: [PATCH] Vulkan: Make sure validation layer name is in-scope

When enabling the Vulkan validation layers, the 'validationLayerName'
variable technically went out of scope before vkCreateInstance() was
called. While most compilers won't clean up stack variables after random
'if' statements, some will, particularly when optimisation or memory
sanitizers are enabled.

This can lead to vkCreateInstance() segfaulting when
SDL_HINT_RENDER_VULKAN_DEBUG is enabled.

Instead, make the validationLayerName visible throughout the entire
VULKAN_CreateDeviceResources() function.

While we're at it, extract the validation layer name out into a
preprocessor #define, so that we are definitely using the same name in
VULKAN_ValidationLayersFound().

Signed-off-by: David Gow <david@ingeniumdigital.com>
---
 src/render/vulkan/SDL_render_vulkan.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/render/vulkan/SDL_render_vulkan.c b/src/render/vulkan/SDL_render_vulkan.c
index faf72458fc1d..603e01943e85 100644
--- a/src/render/vulkan/SDL_render_vulkan.c
+++ b/src/render/vulkan/SDL_render_vulkan.c
@@ -29,6 +29,7 @@
 #define SDL_VULKAN_NUM_UPLOAD_BUFFERS           32
 #define SDL_VULKAN_MAX_DESCRIPTOR_SETS          4096
 
+#define SDL_VULKAN_VALIDATION_LAYER_NAME        "VK_LAYER_KHRONOS_validation"
 
 #define VK_NO_PROTOTYPES
 #include "../../video/SDL_vulkan_internal.h"
@@ -1478,7 +1479,6 @@ static SDL_bool VULKAN_InstanceExtensionFound(VULKAN_RenderData *rendererData, c
 
 static SDL_bool VULKAN_ValidationLayersFound()
 {
-    const char *validationLayerName = "VK_LAYER_KHRONOS_validation";
     uint32_t instanceLayerCount = 0;
     uint32_t i;
     SDL_bool foundValidation = SDL_FALSE;
@@ -1488,7 +1488,7 @@ static SDL_bool VULKAN_ValidationLayersFound()
         VkLayerProperties *instanceLayers = SDL_calloc(instanceLayerCount, sizeof(VkLayerProperties));
         vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayers);
         for (i = 0; i < instanceLayerCount; i++) {
-            if (!SDL_strcmp(validationLayerName, instanceLayers[i].layerName)) {
+            if (!SDL_strcmp(SDL_VULKAN_VALIDATION_LAYER_NAME, instanceLayers[i].layerName)) {
                 foundValidation = SDL_TRUE;
                 break;
             }
@@ -1507,6 +1507,7 @@ static VkResult VULKAN_CreateDeviceResources(SDL_Renderer *renderer)
     VkResult result = VK_SUCCESS;
     PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
     SDL_bool createDebug = SDL_GetHintBoolean(SDL_HINT_RENDER_VULKAN_DEBUG, SDL_FALSE);
+    const char *validationLayerName[] = { SDL_VULKAN_VALIDATION_LAYER_NAME };
 
     if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
         SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "SDL_Vulkan_LoadLibrary failed." );
@@ -1551,7 +1552,6 @@ static VkResult VULKAN_CreateDeviceResources(SDL_Renderer *renderer)
     }
     instanceCreateInfo.ppEnabledExtensionNames = (const char* const*) instanceExtensionsCopy;
     if (createDebug && VULKAN_ValidationLayersFound()) {
-        const char *validationLayerName[] = { "VK_LAYER_KHRONOS_validation" };
         instanceCreateInfo.ppEnabledLayerNames = validationLayerName;
         instanceCreateInfo.enabledLayerCount = 1;
     }