From 4d4fdfff700640eb370dec96dddf9f83c3c99047 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Fri, 17 Feb 2023 07:24:22 -0500 Subject: [PATCH] freetype is not thread safe, zone filler can trigger multi-thread access sentry KICAD-B1 https://freetype.org/freetype2/docs/reference/ft2-base_interface.html [Since 2.5.6] In multi-threaded applications it is easiest to use one FT_Library object per thread. In case this is too cumbersome, a single FT_Library object across threads is possible also, as long as a mutex lock is used around FT_New_Face and FT_Done_Face. (cherry picked from commit 65d5b34da3dfea7c5bb85a44fe9c79a4546fda7c) --- common/font/outline_font.cpp | 9 ++++++++- include/font/outline_font.h | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/common/font/outline_font.cpp b/common/font/outline_font.cpp index 6929822a54..3ec4dcf877 100644 --- a/common/font/outline_font.cpp +++ b/common/font/outline_font.cpp @@ -44,6 +44,7 @@ using namespace KIFONT; FT_Library OUTLINE_FONT::m_freeType = nullptr; +std::mutex OUTLINE_FONT::m_freeTypeMutex; OUTLINE_FONT::OUTLINE_FONT() : m_face(NULL), @@ -51,6 +52,8 @@ OUTLINE_FONT::OUTLINE_FONT() : m_fakeBold( false ), m_fakeItal( false ) { + std::lock_guard guard( m_freeTypeMutex ); + if( !m_freeType ) FT_Init_FreeType( &m_freeType ); } @@ -58,6 +61,8 @@ OUTLINE_FONT::OUTLINE_FONT() : wxString OUTLINE_FONT::FreeTypeVersion() { + std::lock_guard guard( m_freeTypeMutex ); + if( !m_freeType ) FT_Init_FreeType( &m_freeType ); @@ -118,6 +123,8 @@ OUTLINE_FONT* OUTLINE_FONT::LoadFont( const wxString& aFontName, bool aBold, boo FT_Error OUTLINE_FONT::loadFace( const wxString& aFontFileName ) { + std::lock_guard guard( m_freeTypeMutex ); + // TODO: check that going from wxString to char* with UTF-8 // conversion for filename makes sense on any/all platforms FT_Error e = FT_New_Face( m_freeType, aFontFileName.mb_str( wxConvUTF8 ), 0, &m_face ); @@ -369,7 +376,7 @@ VECTOR2I OUTLINE_FONT::getTextAsGlyphs( BOX2I* aBBox, std::vector #include +#include + namespace KIFONT { /** @@ -135,6 +137,7 @@ protected: private: // FreeType variables + static std::mutex m_freeTypeMutex; static FT_Library m_freeType; FT_Face m_face; const int m_faceSize;