Skip to content

Commit 695b33a

Browse files
committed
[lldb/API] Overwrite variables with SBLaunchInfo::SetEnvironment(append=true)
Summary: This function was documented to overwrite entries with D76111, which was adding a couple of similar functions. However, this function (unlike the functions added in that patch) was/is not actually overwriting variables -- any pre-existing variables would get ignored. This behavior does not seem to be intentional. In fact, before the refactor in D41359, this function could introduce duplicate entries, which could have very surprising effects both inside lldb and on other applications (some applications would take the first value, some the second one; in lldb, attempting to unset a variable could make the second variable become active, etc.). Overwriting seems to be the most reasonable behavior here, so change the code to match documentation. Reviewers: clayborg, wallace, jingham Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D83306
1 parent b199131 commit 695b33a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lldb/source/API/SBLaunchInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ void SBLaunchInfo::SetEnvironment(const SBEnvironment &env, bool append) {
190190
LLDB_RECORD_METHOD(void, SBLaunchInfo, SetEnvironment,
191191
(const lldb::SBEnvironment &, bool), env, append);
192192
Environment &refEnv = env.ref();
193-
if (append)
194-
m_opaque_sp->GetEnvironment().insert(refEnv.begin(), refEnv.end());
195-
else
193+
if (append) {
194+
for (auto &KV : refEnv)
195+
m_opaque_sp->GetEnvironment().insert_or_assign(KV.first(), KV.second);
196+
} else
196197
m_opaque_sp->GetEnvironment() = refEnv;
197198
m_opaque_sp->RegenerateEnvp();
198199
}

lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def test_launch_info(self):
5353
launch_info.SetEnvironment(env, append=True)
5454
self.assertEqual(launch_info.GetEnvironment().GetNumValues(), env_count + 1)
5555

56+
env.Set("FOO", "baz", overwrite=True)
57+
launch_info.SetEnvironment(env, append=True)
58+
self.assertEqual(launch_info.GetEnvironment().GetNumValues(), env_count + 1)
59+
self.assertEqual(launch_info.GetEnvironment().Get("FOO"), "baz")
60+
5661
# Make sure we can replace the launchInfo's environment
5762
env.Clear()
5863
env.Set("BAR", "foo", overwrite=True)
@@ -120,6 +125,11 @@ def test_creating_and_modifying_environment(self):
120125
env.SetEntries(entries, append=False)
121126
self.assertEqualEntries(env, ["X=x", "Y=y"])
122127

128+
entries.Clear()
129+
entries.AppendList(["X=y", "Y=x"], 2)
130+
env.SetEntries(entries, append=True)
131+
self.assertEqualEntries(env, ["X=y", "Y=x"])
132+
123133
# Test clear
124134
env.Clear()
125135
self.assertEqualEntries(env, [])

0 commit comments

Comments
 (0)