From 3465fe3e50a6e8ee0cfd399f33ff80c6ecfe0cc7 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 20 Jan 2023 16:43:47 -0800 Subject: [PATCH] Allow variable recursion Simple recursion of variable expansion allows for multiple use cases in portability of designs. This also includes basic prevention of recursion loops Fixes https://gitlab.com/kicad/code/kicad/issues/10398 --- common/common.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index f553986d53..5d2c923c49 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -115,8 +115,15 @@ wxString ExpandTextVars( const wxString& aSource, // // Stolen from wxExpandEnvVars and then heavily optimized // -wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject ) +wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject, std::set* aSet = nullptr ) { + // If the same string is inserted twice, we have a loop + if( aSet ) + { + if( auto [ _, result ] = aSet->insert( str ); !result ) + return str; + } + size_t strlen = str.length(); wxString strResult; @@ -278,6 +285,13 @@ wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject ) } } + std::set loop_check; + auto first_pos = strResult.find_first_of( wxS( "{(%" ) ); + auto last_pos = strResult.find_last_of( wxS( "})%" ) ); + + if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos ) + strResult = KIwxExpandEnvVars( strResult, aProject, aSet ? aSet : &loop_check ); + return strResult; }