From 9c337908099ec3c2033722af14245e4ff455e42c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 17 Jan 2024 09:50:55 +0100 Subject: [PATCH] Check if we exhausted the instance buffer prior to copying underlines This fixes another potential segfault. --- crates/gpui/src/platform/mac/metal_renderer.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/gpui/src/platform/mac/metal_renderer.rs b/crates/gpui/src/platform/mac/metal_renderer.rs index d3a32cc41d..1589757d93 100644 --- a/crates/gpui/src/platform/mac/metal_renderer.rs +++ b/crates/gpui/src/platform/mac/metal_renderer.rs @@ -658,6 +658,12 @@ impl MetalRenderer { let underline_bytes_len = mem::size_of_val(underlines); let buffer_contents = unsafe { (self.instances.contents() as *mut u8).add(*offset) }; + + let next_offset = *offset + underline_bytes_len; + if next_offset > INSTANCE_BUFFER_SIZE { + return false; + } + unsafe { ptr::copy_nonoverlapping( underlines.as_ptr() as *const u8, @@ -666,11 +672,6 @@ impl MetalRenderer { ); } - let next_offset = *offset + underline_bytes_len; - if next_offset > INSTANCE_BUFFER_SIZE { - return false; - } - command_encoder.draw_primitives_instanced( metal::MTLPrimitiveType::Triangle, 0,