mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-16 14:56:00 +00:00
GPU: Compile Metal shader source from NSString
Using the @() syntax to construct an NSString assumes the parenthesized pointer is null-terminated, but the Metal shader source included in render/sdlgpu/shaders/metal.h is not null-terminated. Quoting the clang documentation on Objective-C literals: When the type of the parenthesized expression is (char *) or (const char *), the result of the boxed expression is a pointer to an NSString object containing equivalent character data, which is assumed to be ‘\0’-terminated and UTF-8 encoded. Because the @() syntax assumes null-termination, it may read garbage data after the shader source (up to the next null byte), which can then cause the Metal shader compiler to fail. To prevent this, instead of using the @() boxing syntax, we explicitly construct an NSString using the string length passed by the caller.
This commit is contained in:

committed by
Sam Lantinga

parent
df501040fd
commit
deb313dd99
@@ -740,8 +740,12 @@ static MetalLibraryFunction METAL_INTERNAL_CompileShader(
|
|||||||
id<MTLFunction> function;
|
id<MTLFunction> function;
|
||||||
|
|
||||||
if (format == SDL_GPU_SHADERFORMAT_MSL) {
|
if (format == SDL_GPU_SHADERFORMAT_MSL) {
|
||||||
|
NSString *codeString = [[NSString alloc]
|
||||||
|
initWithBytes:code
|
||||||
|
length:codeSize
|
||||||
|
encoding:NSUTF8StringEncoding];
|
||||||
library = [renderer->device
|
library = [renderer->device
|
||||||
newLibraryWithSource:@((const char *)code)
|
newLibraryWithSource:codeString
|
||||||
options:nil
|
options:nil
|
||||||
error:&error];
|
error:&error];
|
||||||
} else if (format == SDL_GPU_SHADERFORMAT_METALLIB) {
|
} else if (format == SDL_GPU_SHADERFORMAT_METALLIB) {
|
||||||
|
Reference in New Issue
Block a user