SDL_shadercross: Add NULL checks (#61)

From 6ab5542f3df246c3364234f8a28d0b067196c1a1 Mon Sep 17 00:00:00 2001
From: Beyley Thomas <[EMAIL REDACTED]>
Date: Fri, 15 Nov 2024 19:55:29 -0800
Subject: [PATCH] Add NULL checks (#61)

* Add NULL checks for TranspileFromSPIRV

This function can return null on error, and right now a null dereference occurs when this happened.

* Add NULL check for D3DCompile

On linux, this returned NULL, causing a crash.
---
 src/SDL_shadercross.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/SDL_shadercross.c b/src/SDL_shadercross.c
index 04a8ce8..079dcca 100644
--- a/src/SDL_shadercross.c
+++ b/src/SDL_shadercross.c
@@ -685,9 +685,13 @@ static ID3DBlob *SDL_ShaderCross_INTERNAL_CompileDXBC(
         &errorBlob);
 
     if (ret < 0) {
-        SDL_SetError(
-            "HLSL compilation failed: %s",
-            (char *)errorBlob->lpVtbl->GetBufferPointer(errorBlob));
+        if (errorBlob != NULL) {
+            SDL_SetError(
+                "HLSL compilation failed: %s",
+                (char *)errorBlob->lpVtbl->GetBufferPointer(errorBlob));
+        } else {
+            SDL_SetError("HLSL compilation failed for an unknown reason.");
+        }
         return NULL;
     }
 
@@ -1734,6 +1738,10 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV(
         bytecodeSize,
         entrypoint);
 
+    if (transpileContext == NULL) {
+        return NULL;
+    }
+
     void *shaderObject = NULL;
 
     if (shaderStage == SDL_SHADERCROSS_SHADERSTAGE_COMPUTE) {
@@ -1820,6 +1828,10 @@ void *SDL_ShaderCross_TranspileMSLFromSPIRV(
         entrypoint
     );
 
+    if (context == NULL) {
+        return NULL;
+    }
+
     size_t length = SDL_strlen(context->translated_source) + 1;
     char *result = SDL_malloc(length);
     SDL_strlcpy(result, context->translated_source, length);
@@ -1843,6 +1855,10 @@ void *SDL_ShaderCross_TranspileHLSLFromSPIRV(
         entrypoint
     );
 
+    if (context == NULL) {
+        return NULL;
+    }
+
     size_t length = SDL_strlen(context->translated_source) + 1;
     char *result = SDL_malloc(length);
     SDL_strlcpy(result, context->translated_source, length);
@@ -1866,6 +1882,10 @@ void *SDL_ShaderCross_CompileDXBCFromSPIRV(
         bytecodeSize,
         entrypoint);
 
+    if (context == NULL) {
+        return NULL;
+    }
+
     void *result = SDL_ShaderCross_INTERNAL_CompileDXBCFromHLSL(
         context->translated_source,
         context->cleansed_entrypoint,
@@ -1898,6 +1918,10 @@ void *SDL_ShaderCross_CompileDXILFromSPIRV(
         bytecodeSize,
         entrypoint);
 
+    if (context == NULL) {
+        return NULL;
+    }
+
     void *result = SDL_ShaderCross_CompileDXILFromHLSL(
         context->translated_source,
         context->cleansed_entrypoint,