Author Topic: Macro manager infinite loop  (Read 11918 times)

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Macro manager infinite loop
« on: March 05, 2013, 12:35:26 am »
OK, I've tried to use the $REMOVE_QUOTES{} macro with a variable as argument and C::B entered infinite loop.

To reproduce:
1. create a variable in build options - $TEST="-ftest -ftest2"
2. set compiler -> other options to $REMOVE_QUOTES{$TEST}
3. build

Looking at the code this infinite loop will happen if the argument to $REMOVE_QUOTES isn't quoted.

Here is a patch that fixes both problems, but I wonder if I really need the if statement that checks if the argument starts with '$'.
Can I skip it? What other macros should be fixed in a similar fashion?
@dev: Please comment on the above...
Code
Index: src/sdk/macrosmanager.cpp
===================================================================
--- src/sdk/macrosmanager.cpp   (revision 8898)
+++ src/sdk/macrosmanager.cpp   (working copy)
@@ -490,12 +490,16 @@ void MacrosManager::ReplaceMacros(wxString& buffer, ProjectBuildTarget* target,
     while (m_RE_RemoveQuotes.Matches(buffer))
     {
         search = m_RE_RemoveQuotes.GetMatch(buffer, 0);
-        const wxString content = m_RE_RemoveQuotes.GetMatch(buffer, 1);
+        wxString content = m_RE_RemoveQuotes.GetMatch(buffer, 1).Trim().Trim(false);
+        if (content.StartsWith(wxT("$")))
+            ReplaceMacros(content, target, subrequest);
         if (content.Len()>2 && content.StartsWith(wxT("\"")) && content.EndsWith(wxT("\"")))
         {
             replace = content.Mid(1,content.Len()-2); // with first and last char (the quotes) removed
             buffer.Replace(search, replace, false);
         }
+        else
+            buffer.Replace(search, content, false);
     }

     while (m_RE_Unix.Matches(buffer))
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Macro manager infinite loop
« Reply #1 on: March 08, 2013, 05:28:37 am »
Can I skip it? What other macros should be fixed in a similar fashion?
the reason for this was that macros sometimes contain quotes while you don't want that, for example path'es in envvars. Assume you want to compile a path list that this can screw the compile argument. So the reason for checking for a $ is to address only such macros. If you believe there is more flexibility and there are use cases also for a more general (not limited to macros) use case, feel free to do.

The philosophy for these macros was actually to fix what you get when resolving macros.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: Macro manager infinite loop
« Reply #2 on: March 08, 2013, 09:19:02 am »
The philosophy for these macros was actually to fix what you get when resolving macros.
I'm not sure I understand this.

Can you give me example use cases for this macro that you've used or you know are used in the field?

The test case example  $REMOVE_QUOTES{$TEST} is not one of them, because it didn't work.
What should be the result of this expansion $REMOVE_QUOTES{"bla bla $TEST bla bla"}?
I guess we don't want to replace $TEST in there and then remove the quotes from the expanded string?
We just want to remove the quotes, probably, but is this really useful?

Do you have some test cases? If you have can you post them so I can setup some little unit test project for this?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Macro manager infinite loop
« Reply #3 on: March 09, 2013, 01:53:46 pm »
I wanted to use path's from windows envvars like %USERPROFILE%. these come with quotes, if the path contains spaces. If you accumulate different path's together you end up in something like:
"$(USERPROFILE)Foo\Bar" -> ""C:\Users\Foo Bar\"Foo\Bar"
...which causes syntax errors for pros-processing tools.
Therefore changing this to:
"$REMOVE_QUOTES{$(USERPROFILE)}Foo\Bar"
...leads to: "C:\Users\Foo Bar\Foo\Bar"
...exactly what you want.

(Maybe it was related to %ProgramFiles%... I don't recall exactly.)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: Macro manager infinite loop
« Reply #4 on: March 09, 2013, 06:12:05 pm »
Does this currently work?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Macro manager infinite loop
« Reply #5 on: March 09, 2013, 06:26:20 pm »
Does this currently work?
Yes - I did it for code at my work as it is needed there and now works this way.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: Macro manager infinite loop
« Reply #6 on: March 09, 2013, 06:37:18 pm »
Okay, does it still work with my patch? :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Macro manager infinite loop
« Reply #7 on: March 09, 2013, 07:37:13 pm »
Okay, does it still work with my patch? :)
I'll report back... applied it now an re-compiling...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: Macro manager infinite loop
« Reply #8 on: March 30, 2013, 03:11:48 pm »
In svn...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

pr3dicine

  • Guest
Re: Macro manager infinite loop
« Reply #9 on: April 03, 2013, 07:51:37 pm »
would this work with my patch that i have as of now?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Macro manager infinite loop
« Reply #10 on: April 04, 2013, 07:34:14 pm »
would this work with my patch that i have as of now?
What patch?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ