From 9486e8a0b1bc567161bc78cf269036701a657120 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <jnqnfe@gmail.com>
Date: Mon, 21 Sep 2020 16:47:33 +0100
Subject: [PATCH] freetype: avoid repeatedly recalculating new count

---
 modules/text_renderer/freetype/freetype.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/modules/text_renderer/freetype/freetype.c b/modules/text_renderer/freetype/freetype.c
index 8e094a2b8f8e..116be5ad6743 100644
--- a/modules/text_renderer/freetype/freetype.c
+++ b/modules/text_renderer/freetype/freetype.c
@@ -855,30 +855,32 @@ static size_t AddTextAndStyles( filter_sys_t *p_sys,
         return 0;
 
     const size_t i_newchars = i_bytes / 4;
-    if( SIZE_MAX / 4 < p_text_block->i_count + i_newchars )
+    const size_t i_new_count = p_text_block->i_count + i_newchars;
+    if( SIZE_MAX / 4 < i_new_count )
     {
         free( p_ucs4 );
         return 0;
     }
-    size_t i_realloc = (p_text_block->i_count + i_newchars) * 4;
+
+    size_t i_realloc = i_new_count * 4;
     void *p_realloc = realloc( p_text_block->p_uchars, i_realloc );
     if( unlikely(!p_realloc) )
         return 0;
     p_text_block->p_uchars = p_realloc;
 
     /* We want one per segment shared text_style_t* per unicode character */
-    if( SIZE_MAX / sizeof(text_style_t *) < p_text_block->i_count + i_newchars )
+    if( SIZE_MAX / sizeof(text_style_t *) < i_new_count )
         return 0;
-    i_realloc = (p_text_block->i_count + i_newchars) * sizeof(text_style_t *);
+    i_realloc = i_new_count * sizeof(text_style_t *);
     p_realloc = realloc( p_text_block->pp_styles, i_realloc );
     if ( unlikely(!p_realloc) )
         return 0;
     p_text_block->pp_styles = p_realloc;
 
     /* Same for ruby text */
-    if( SIZE_MAX / sizeof(text_segment_ruby_t *) < p_text_block->i_count + i_newchars )
+    if( SIZE_MAX / sizeof(text_segment_ruby_t *) < i_new_count )
         return 0;
-    i_realloc = (p_text_block->i_count + i_newchars) * sizeof(text_segment_ruby_t *);
+    i_realloc = i_new_count * sizeof(text_segment_ruby_t *);
     p_realloc = realloc( p_text_block->pp_ruby, i_realloc );
     if ( unlikely(!p_realloc) )
         return 0;
-- 
GitLab