Skip to content

[lldb] Fix string truncation method when substring is the prefix of string (NFC) #94785

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 3 commits into from
Jul 11, 2024

Conversation

xgupta
Copy link
Contributor

@xgupta xgupta commented Jun 7, 2024

Correct the method used to truncate the source_file string when substring is a prefix. The previous method used substr, which was changed to resize for clarity and efficiency.

Caught by cppcheck -
lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp:290:19: performance: Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead. [uselessCallsSubstr]

Source code -
source_file = source_file.substr(0, pos);

Fix #91211

@xgupta xgupta requested a review from JDevlieghere as a code owner June 7, 2024 18:16
@llvmbot llvmbot added the lldb label Jun 7, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-lldb

Author: Shivam Gupta (xgupta)

Changes

lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp:290:19: performance: Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead. [uselessCallsSubstr]

Fix #91211


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

1 Files Affected:

  • (modified) lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp (+1-1)
diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
index e177c134fea20..6367763dd8b4a 100644
--- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
+++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
@@ -287,7 +287,7 @@ Status PlatformAndroid::DownloadModuleSlice(const FileSpec &src_file_spec,
   static constexpr llvm::StringLiteral k_zip_separator("!/");
   size_t pos = source_file.find(k_zip_separator);
   if (pos != std::string::npos)
-    source_file = source_file.substr(0, pos);
+    source_file = source_file.resize(0, pos);
 
   Status error;
   AdbClientUP adb(GetAdbClient(error));

@xgupta xgupta changed the title [LLDB][NFC] Fix a cppcheck warning in Platform/Android/PlatformAndroid.cpp [lldb] Fix string truncation method when substring is the prefix of string (NFC) Jun 8, 2024
@xgupta xgupta merged commit a18f45f into llvm:main Jul 11, 2024
5 checks passed
@xgupta xgupta deleted the fix91211 branch July 11, 2024 07:27
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
…tring (NFC) (llvm#94785)

Correct the method used to truncate the source_file string when
substring is a prefix. The previous method used substr, which was
changed to resize for clarity and efficiency.

Caught by cppcheck - 
lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp:290:19:
performance: Ineffective call of function 'substr' because a prefix of
the string is assigned to itself. Use resize() or pop_back() instead.
[uselessCallsSubstr]

Source code - 
source_file = source_file.substr(0, pos);

Fix llvm#91211

---------

Co-authored-by: Shivam Gupta <[email protected]>
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.

lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp:290: clumsy string resize ?
5 participants