Escape all env var strings sent to the Python interpreter

Fixes https://gitlab.com/kicad/code/kicad/issues/5130

(Cherry-picked from 0a0ed9e064)
This commit is contained in:
Ian McInerney 2020-08-11 00:50:09 +01:00
parent 1b2d4d6522
commit 1088d613d5
1 changed files with 25 additions and 2 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 wxString& aVar, const wxString& aValue )
{
char cmd[1024];
@ -327,11 +347,14 @@ void pcbnewUpdatePythonEnvVar( const wxString& aVar, const wxString& aValue )
if( !Py_IsInitialized() )
return;
wxString escapedVar = PyEscapeString( aVar );
wxString escapedVal = PyEscapeString( aValue );
snprintf( cmd, sizeof( cmd ),
"# coding=utf-8\n" // The values could potentially be UTF8
"os.environ[\"%s\"]=\"%s\"\n",
TO_UTF8( aVar ),
TO_UTF8( aValue ) );
TO_UTF8( escapedVar ),
TO_UTF8( escapedVal ) );
PyLOCK lock;