Skip to content

Commit

Permalink
Fix an infinite loop in replace_all() when the search string is empty
Browse files Browse the repository at this point in the history
The empty string is found at the start of the string, pos = 0, and
replaced over and over again.  If the replacement string is also empty,
the size of the data string will not change.  If the replacement string
is not empty, it will be repeatedly prepended to the data string.

While most callers are careful to ensure the search string is not empty,
PosixActivator::update_prompt() lost this property in "Micromamba:
Substitute env vars in .condarc (mamba-org#1423)" (9e5f8fd).  Rather than restore
the check in the caller, fix the root of the issue in replace_all()
itself.

I ran into this bug as a Micromamba user in a particular set of
circumstances where the CONDA_PROMPT_MODIFIER env var was the empty
string.¹  It manifested to me the user as a `micromamba create`
invocation hanging at the linking phase for any package with a post-link
script (such as openmpi).  The generated post-link script wrapper
invoked, indirectly, `micromamba shell --shell bash activate <path>`
which was hanging during update_prompt().

¹ nextstrain/cli#223 (comment)
  • Loading branch information
tsibley committed Oct 7, 2022
1 parent cb2342a commit 96ead14
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libmamba/src/core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ namespace mamba
template <class S>
void replace_all_impl(S& data, const S& search, const S& replace)
{
if (search.empty())
{
return;
}
std::size_t pos = data.find(search);
while (pos != std::string::npos)
{
Expand Down

0 comments on commit 96ead14

Please sign in to comment.