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>
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);
}
}