From d64b9ea4835eb6a38b5522b5470e7cd0e43a071a Mon Sep 17 00:00:00 2001 From: LingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org> Date: Wed, 5 Oct 2022 22:00:56 +0200 Subject: [PATCH] rusticl/api: Fix creating a program if a nul byte is within the specified string length 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: --- src/gallium/frontends/rusticl/api/program.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/gallium/frontends/rusticl/api/program.rs b/src/gallium/frontends/rusticl/api/program.rs index f1e9bd9..c47840f 100644 --- a/src/gallium/frontends/rusticl/api/program.rs +++ b/src/gallium/frontends/rusticl/api/program.rs @@ -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); } } -- 2.7.4