Escape all env var strings sent to the Python interpreter

Fixes https://gitlab.com/kicad/code/kicad/issues/5130
This commit is contained in:
Ian McInerney 2020-08-11 00:50:09 +01:00
parent 259cacf648
commit 0a0ed9e064
1 changed files with 25 additions and 3 deletions

View File

@ -319,6 +319,26 @@ void pcbnewFinishPythonScripting()
} }
wxString PyEscapeString( const wxString& aSource )
{
wxString converted;
for( wxUniChar c: aSource )
{
if( c == '\\' )
converted += "\\\\";
else if( c == '\'' )
converted += "\\\'";
else if( c == '\"' )
converted += "\\\"";
else
converted += c;
}
return converted;
}
void pcbnewUpdatePythonEnvVar( const std::string& aVar, const wxString& aValue ) void pcbnewUpdatePythonEnvVar( const std::string& aVar, const wxString& aValue )
{ {
char cmd[1024]; char cmd[1024];
@ -327,11 +347,14 @@ void pcbnewUpdatePythonEnvVar( const std::string& aVar, const wxString& aValue )
if( !Py_IsInitialized() ) if( !Py_IsInitialized() )
return; return;
wxString escapedVar = PyEscapeString( aVar );
wxString escapedVal = PyEscapeString( aValue );
snprintf( cmd, sizeof( cmd ), snprintf( cmd, sizeof( cmd ),
"# coding=utf-8\n" // The values could potentially be UTF8 "# coding=utf-8\n" // The values could potentially be UTF8
"os.environ[\"%s\"]=\"%s\"\n", "os.environ[\"%s\"]=\"%s\"\n",
aVar.c_str(), TO_UTF8( escapedVar ),
TO_UTF8( aValue ) ); TO_UTF8( escapedVal ) );
PyLOCK lock; PyLOCK lock;
@ -342,7 +365,6 @@ void pcbnewUpdatePythonEnvVar( const std::string& aVar, const wxString& aValue )
} }
#if defined( KICAD_SCRIPTING_WXPYTHON ) #if defined( KICAD_SCRIPTING_WXPYTHON )
void RedirectStdio() void RedirectStdio()
{ {