Fix undefined behavior when computing cursor end pointer

This commit is contained in:
Cameron Gutman 2024-03-08 17:26:43 -06:00
parent 7cdd156bce
commit ce3b625983

View File

@ -1240,8 +1240,13 @@ namespace platf {
auto delta_width = std::min<uint32_t>(captured_cursor.src_w, std::max<int32_t>(0, screen_width - cursor_x)) - cursor_delta_x;
for (auto y = 0; y < delta_height; ++y) {
// Offset into the cursor image to skip drawing the parts of the cursor image that are off screen
auto cursor_begin = (uint32_t *) &captured_cursor.pixels[((y + cursor_delta_y) * captured_cursor.src_w + cursor_delta_x) * 4];
auto cursor_end = (uint32_t *) &captured_cursor.pixels[((y + cursor_delta_y) * captured_cursor.src_w + delta_width + cursor_delta_x) * 4];
//
// NB: We must access the elements via the data() function because cursor_end may point to the
// the first element beyond the valid range of the vector. Using vector's [] operator in that
// manner is undefined behavior (and triggers errors when using debug libc++), while doing the
// same with an array is fine.
auto cursor_begin = (uint32_t *) &captured_cursor.pixels.data()[((y + cursor_delta_y) * captured_cursor.src_w + cursor_delta_x) * 4];
auto cursor_end = (uint32_t *) &captured_cursor.pixels.data()[((y + cursor_delta_y) * captured_cursor.src_w + delta_width + cursor_delta_x) * 4];
auto pixels_begin = &pixels[(y + cursor_y) * (img.row_pitch / img.pixel_pitch) + cursor_x];