Skip to content

[LLDB][SBProgress] Fix bad optional in sbprogress #128971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 27, 2025

Conversation

Jlalond
Copy link
Contributor

@Jlalond Jlalond commented Feb 26, 2025

This fixes the obvious, but untested case of sending None/Null to SBProgress. This was an oversight on my part.

@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2025

@llvm/pr-subscribers-lldb

Author: Jacob Lalonde (Jlalond)

Changes

This fixes the obvious, but untested case of sending None/Null to SBProgress. This was an oversight on my part.


Full diff: https://github.com/llvm/llvm-project/pull/128971.diff

2 Files Affected:

  • (modified) lldb/source/API/SBProgress.cpp (+4-1)
  • (modified) lldb/test/API/python_api/sbprogress/TestSBProgress.py (+12)
diff --git a/lldb/source/API/SBProgress.cpp b/lldb/source/API/SBProgress.cpp
index e67e289a60eff..d40e11da973d4 100644
--- a/lldb/source/API/SBProgress.cpp
+++ b/lldb/source/API/SBProgress.cpp
@@ -40,7 +40,10 @@ SBProgress::~SBProgress() = default;
 void SBProgress::Increment(uint64_t amount, const char *description) {
   LLDB_INSTRUMENT_VA(amount, description);
 
-  m_opaque_up->Increment(amount, description);
+  std::optional<std::string> description_opt;
+  if (description && description[0])
+    description_opt = description;
+  m_opaque_up->Increment(amount, description_opt);
 }
 
 lldb_private::Progress &SBProgress::ref() const { return *m_opaque_up; }
diff --git a/lldb/test/API/python_api/sbprogress/TestSBProgress.py b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
index c456247da80c6..5f7820a5bd81e 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -33,3 +33,15 @@ def test_without_external_bit_set(self):
         expected_string = "Test progress first increment"
         progress.Increment(1, expected_string)
         self.assertFalse(listener.PeekAtNextEvent(event))
+
+    def test_with_external_bit_set(self):
+        """Test SBProgress can handle null events."""
+
+        progress = lldb.SBProgress("Test SBProgress", "Test progress", self.dbg)
+        listener = lldb.SBListener("Test listener")
+        broadcaster = self.dbg.GetBroadcaster()
+        broadcaster.AddListener(listener, lldb.eBroadcastBitExternalProgress)
+        event = lldb.SBEvent()
+
+        progress.Increment(1, None)
+        self.assertTrue(listener.PeekAtNextEvent(event))

@@ -40,7 +40,10 @@ SBProgress::~SBProgress() = default;
void SBProgress::Increment(uint64_t amount, const char *description) {
LLDB_INSTRUMENT_VA(amount, description);

m_opaque_up->Increment(amount, description);
std::optional<std::string> description_opt;
if (description && description[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this only calls increment if there is a non-null and non-empty description. Is that the correct behavior? Or should we still be calling increment with a nullopt in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we only set the value of the description optional if non-null, non-empty description. So Increment(1, NULL/None in Python) will bump the count by one but send no message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I read that wrong. Thanks for confirming.

broadcaster.AddListener(listener, lldb.eBroadcastBitExternalProgress)
event = lldb.SBEvent()

progress.Increment(1, None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would progress.Increment(1, "") map to the case where description[0] == '\0'? If so seems like a cheap test to add.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout, I expanded the test case.

@Jlalond Jlalond changed the title [LLDB][SBProgress] Fix bad optional access in sbprogress [LLDB][SBProgress] Fix bad optional in sbprogress Feb 27, 2025
self.assertTrue(listener.GetNextEvent(event))
stream = lldb.SBStream()
event.GetDescription(stream)
self.assertIn("Step 3", stream.GetData())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test that we receive these events with the correct data here? (get the start event, the 3 increment events with no detail for the first two and then "Step 3" for the 3rd increment, though right now that might come through as an end event?

Copy link
Contributor

@dmpots dmpots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -40,7 +40,10 @@ SBProgress::~SBProgress() = default;
void SBProgress::Increment(uint64_t amount, const char *description) {
LLDB_INSTRUMENT_VA(amount, description);

m_opaque_up->Increment(amount, description);
std::optional<std::string> description_opt;
if (description && description[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I read that wrong. Thanks for confirming.

@Jlalond Jlalond merged commit 10a9dca into llvm:main Feb 27, 2025
10 checks passed
@Jlalond Jlalond deleted the sbprogress-bad-optional branch March 7, 2025 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants