Skip to content

Commit

Permalink
Add NULL checks (#61)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Beyley authored Nov 16, 2024
1 parent 36204cf commit 6ab5542
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/SDL_shadercross.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6ab5542

Please sign in to comment.