Eeschema: fix Bug #1483693 : eeschema for Windows fails to deal with Cyrillic (UTF8) (Windows specific).

lib_dxf/libdxfrw.cpp: try to fix a coverity warning about resource leak.
This commit is contained in:
jean-pierre charras 2015-08-12 09:24:18 +02:00
parent 5ba6cb975b
commit 666e825e17
2 changed files with 45 additions and 9 deletions

View File

@ -717,32 +717,62 @@ bool LIB_PIN::Save( OUTPUTFORMATTER& aFormatter )
return true; return true;
} }
#include <wx/tokenzr.h>
bool LIB_PIN::Load( LINE_READER& aLineReader, wxString& aErrorMsg ) bool LIB_PIN::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
{ {
int i, j; int i, j;
char pinAttrs[64]; char pinAttrs[64];
char pinName[256];
char pinNum[64];
char pinOrient[64]; char pinOrient[64];
char pinType[64]; char pinType[64];
char* line = (char*) aLineReader;
*pinAttrs = 0; *pinAttrs = 0;
i = sscanf( line + 2, "%255s %63s %d %d %d %63s %d %d %d %d %63s %63s", pinName, // We cannot use sscanf, at least on Windows, to parse the pin description.
pinNum, &m_position.x, &m_position.y, &m_length, pinOrient, &m_numTextSize, // The reason is the pin name is free, and use UTF8 encoding.
&m_nameTextSize, &m_Unit, &m_Convert, pinType, pinAttrs ); // We encourtered issues (Windows specific) to read this name for some UTF8
// cyrillic codes
// So, read the pin name (and num) after conversion from UTF8, and read the others
// parameters (in ASCII) using sscanf
// the full line starts by "X ". The pin data starts at line + 2.
wxString utf8line = FROM_UTF8( aLineReader.Line() + 2 );
wxStringTokenizer tokenizer( utf8line, wxT(" \n\r" ) );
i = tokenizer.CountTokens();
if( i < 11 ) if( i < 11 )
{ {
aErrorMsg.Printf( wxT( "pin only had %d parameters of the required 11 or 12" ), i ); aErrorMsg.Printf( wxT( "pin had %d parameters of the required 11 or 12" ), i );
return false; return false;
} }
// Extract the pinName (UTF8 encoded)
m_name = tokenizer.GetNextToken();
wxString tmp;
// Extract the pinName (UTF8 encoded accepted, but should be only ASCII8.)
tmp = tokenizer.GetNextToken();
SetPinNumFromString( tmp );
// Read other parameters, in pure ASCII
char line[1024];
tmp = tokenizer.GetString();
unsigned len = tmp.Length();
if( len >= sizeof( line ) ) // Should not occur.
len = sizeof( line) - 1;
strncpy( line, TO_UTF8( tmp ), len );
line[len] = 0;
sscanf( line, "%d %d %d %63s %d %d %d %d %63s %63s",
&m_position.x, &m_position.y, &m_length, pinOrient, &m_numTextSize,
&m_nameTextSize, &m_Unit, &m_Convert, pinType, pinAttrs );
m_orientation = pinOrient[0] & 255; m_orientation = pinOrient[0] & 255;
strncpy( (char*) &m_number, pinNum, 4 );
m_name = FROM_UTF8( pinName );
switch( *pinType & 255 ) switch( *pinType & 255 )
{ {

View File

@ -3593,6 +3593,7 @@ bool dxfRW::processVertex( DRW_Polyline* pl )
DBG( "dxfRW::processVertex" ); DBG( "dxfRW::processVertex" );
int code; int code;
DRW_Vertex* v = new DRW_Vertex(); DRW_Vertex* v = new DRW_Vertex();
bool appended = false;
while( reader->readRec( &code, !binary ) ) while( reader->readRec( &code, !binary ) )
{ {
@ -3603,6 +3604,7 @@ bool dxfRW::processVertex( DRW_Polyline* pl )
case 0: case 0:
{ {
pl->appendVertex( v ); pl->appendVertex( v );
appended = true;
nextentity = reader->getString(); nextentity = reader->getString();
DBG( nextentity ); DBG( "\n" ); DBG( nextentity ); DBG( "\n" );
@ -3612,7 +3614,11 @@ bool dxfRW::processVertex( DRW_Polyline* pl )
} }
else if( nextentity == "VERTEX" ) else if( nextentity == "VERTEX" )
{ {
if( !appended )
delete v;
v = new DRW_Vertex(); // another vertex v = new DRW_Vertex(); // another vertex
appended = false;
} }
} }