Check if we exhausted the instance buffer prior to copying underlines

Antonio Scandurra created

This fixes another potential segfault.

Change summary

crates/gpui/src/platform/mac/metal_renderer.rs | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

Detailed changes

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,