[Lldb-commits] [PATCH] D120101: [lldb] Fix (unintentional) recursion in CommandObjectRegexCommand

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 17 16:43:13 PST 2022

JDevlieghere created this revision.
JDevlieghere added reviewers: jingham, clayborg, aprantl, mib.
Herald added a subscriber: mgorny.
JDevlieghere requested review of this revision.

Jim noticed that the regex command is unintentionally recursive:

Let's use the following command regex as an example:

  (lldb) com regex humm 's/([^ ]+) ([^ ]+)/p %1 %2 %1 %2/'
  If we call it with arguments `foo bar`, thing behave as expected:

(lldb) humm foo bar
foo bar foo bar

  However, if we include `%2` in the arguments, things break down:

(lldb) humm fo%2o bar
fobaro bar fobaro bar

  The problem is that the implementation of the substitution is too naive.  It substitutes the %1 token into the target template in place, then does the %2 substitution starting with the resultant string. So if the previous substitution introduced a %2 token, it would get processed in the second sweep, etc.
  This patch addresses the issue by walking the command once and substituting the % variables in place. I didn't really trust myself so I also wrote a few unit tests to make sure I didn't miss any edge cases. 



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120101.409817.patch
Type: text/x-patch
Size: 6499 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220218/284182ce/attachment.bin>

More information about the lldb-commits mailing list