rusticl/api: Fix creating a program if a nul byte is within the specified string...
authorLingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org>
Wed, 5 Oct 2022 20:00:56 +0000 (22:00 +0200)
committerMarge Bot <emma+marge@anholt.net>
Wed, 19 Oct 2022 23:32:50 +0000 (23:32 +0000)
The code assumed that if the length of a string was specified and greater than zero, the string
would not contain a nul byte.
Legal or not, there are apparently applications which violate that assumption. Since the spec
doesn't say anything about this case, take the likely most compatible route and treat a nul byte as
terminating the string early.

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/7408
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18978>

src/gallium/frontends/rusticl/api/program.rs

index f1e9bd9..c47840f 100644 (file)
@@ -144,7 +144,15 @@ pub fn create_program_with_source(
             if *len == 0 {
                 source.extend_from_slice(CStr::from_ptr(string_ptr).to_bytes());
             } else {
+                // The spec doesn't say how nul bytes should be handled here or
+                // if they are legal at all. Assume they truncate the string.
                 let arr = slice::from_raw_parts(string_ptr.cast(), *len);
+                // TODO: simplify this a bit with from_bytes_until_nul once
+                // that's stabilized and available in our msrv
+                let arr = arr
+                    .iter()
+                    .position(|&x| x == 0)
+                    .map_or(arr, |nul_index| &arr[..nul_index]);
                 source.extend_from_slice(arr);
             }
         }