Use safer UTF8 conversion routines to prevent crashes.

Fixes https://gitlab.com/kicad/code/kicad/issues/10982
This commit is contained in:
Jeff Young 2022-02-26 19:02:34 +00:00
parent 2c6a2c3479
commit 941d7f94ad
3 changed files with 23 additions and 12 deletions

View File

@ -34,6 +34,7 @@
// The "official" name of the building Kicad stroke font (always existing) // The "official" name of the building Kicad stroke font (always existing)
#include <font/kicad_font_name.h> #include <font/kicad_font_name.h>
#include "macros.h"
// markup_parser.h includes pegtl.hpp which includes windows.h... which leaks #define DrawText // markup_parser.h includes pegtl.hpp which includes windows.h... which leaks #define DrawText
@ -211,11 +212,11 @@ VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* a
if( aNode->has_content() ) if( aNode->has_content() )
{ {
std::string txt = aNode->string(); BOX2I bbox;
BOX2I bbox;
nextPosition = aFont->GetTextAsGlyphs( &bbox, aGlyphs, txt, aSize, aPosition, aAngle, nextPosition = aFont->GetTextAsGlyphs( &bbox, aGlyphs, aNode->wxString(), aSize,
aMirror, aOrigin, textStyle ); aPosition, aAngle, aMirror, aOrigin,
textStyle );
if( aBoundingBox ) if( aBoundingBox )
aBoundingBox->Merge( bbox ); aBoundingBox->Merge( bbox );
@ -238,7 +239,7 @@ VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYP
const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin, const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
TEXT_STYLE_FLAGS aTextStyle ) const TEXT_STYLE_FLAGS aTextStyle ) const
{ {
MARKUP::MARKUP_PARSER markupParser( aText.ToStdString() ); MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
std::unique_ptr<MARKUP::NODE> root = markupParser.Parse(); std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
return ::drawMarkup( aBoundingBox, aGlyphs, root, aPosition, this, aSize, aAngle, aMirror, return ::drawMarkup( aBoundingBox, aGlyphs, root, aPosition, this, aSize, aAngle, aMirror,
@ -348,14 +349,15 @@ void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
if( escapeChar ) if( escapeChar )
{ {
wxString word = wxString::Format( "%c{", escapeChar ); wxString word = wxString::Format( wxT( "%c{" ), escapeChar );
int width = 0; int width = 0;
if( aNode->has_content() ) if( aNode->has_content() )
{ {
VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->string(), aSize, VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->wxString(),
{0,0}, ANGLE_0, false, {0,0}, textStyle ); aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
word += aNode->string(); textStyle );
word += aNode->wxString();
width += next.x; width += next.x;
} }
@ -370,14 +372,14 @@ void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
width += childWord.second; width += childWord.second;
} }
word += "}"; word += wxT( "}" );
aWords->emplace_back( std::make_pair( word, width ) ); aWords->emplace_back( std::make_pair( word, width ) );
return; return;
} }
else else
{ {
wxString space( wxS( " " ) ); wxString space( wxS( " " ) );
wxString textRun( aNode->string() ); wxString textRun = aNode->wxString();
wxArrayString words; wxArrayString words;
wxStringSplit( textRun, words, ' ' ); wxStringSplit( textRun, words, ' ' );
@ -403,7 +405,7 @@ void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText, void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const
{ {
MARKUP::MARKUP_PARSER markupParser( aText.ToStdString() ); MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
std::unique_ptr<MARKUP::NODE> root = markupParser.Parse(); std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
::wordbreakMarkup( aWords, root, this, aSize, aTextStyle ); ::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );

View File

@ -21,6 +21,7 @@
#include <markup_parser.h> #include <markup_parser.h>
#include <sstream> #include <sstream>
#include "macros.h"
using namespace MARKUP; using namespace MARKUP;
@ -56,6 +57,12 @@ std::string NODE::typeString() const
} }
wxString NODE::wxString() const
{
return FROM_UTF8( string().c_str() );
}
std::string NODE::asString() const std::string NODE::asString() const
{ {
std::stringstream os; std::stringstream os;

View File

@ -42,6 +42,8 @@ struct NODE : parse_tree::basic_node<NODE>
std::string typeString() const; std::string typeString() const;
wxString wxString() const;
bool isOverbar() const { return is_type<MARKUP::overbar>(); } bool isOverbar() const { return is_type<MARKUP::overbar>(); }
bool isSubscript() const { return is_type<MARKUP::subscript>(); } bool isSubscript() const { return is_type<MARKUP::subscript>(); }
bool isSuperscript() const { return is_type<MARKUP::superscript>(); } bool isSuperscript() const { return is_type<MARKUP::superscript>(); }