[llvm] b0492d9 - Support: Avoid SmallVector::set_size() in Unix code

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 11 18:00:25 PST 2022


Author: Duncan P. N. Exon Smith
Date: 2022-01-11T17:57:23-08:00
New Revision: b0492d92adc53ca2b6a6bae16f4fae85e5396ff2

URL: https://github.com/llvm/llvm-project/commit/b0492d92adc53ca2b6a6bae16f4fae85e5396ff2
DIFF: https://github.com/llvm/llvm-project/commit/b0492d92adc53ca2b6a6bae16f4fae85e5396ff2.diff

LOG: Support: Avoid SmallVector::set_size() in Unix code

Replace a `reserve()`/`set_size()` pair with `resize_for_overwrite()`
and `truncate()`. The out parameter also needs a `clear()` call on the
error path.

Differential Revision: https://reviews.llvm.org/D115389

Added: 
    

Modified: 
    llvm/lib/Support/Unix/Path.inc

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 97b280bb073f8..788460d657fe5 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -380,20 +380,22 @@ std::error_code current_path(SmallVectorImpl<char> &result) {
     return std::error_code();
   }
 
-  result.reserve(PATH_MAX);
+  result.resize_for_overwrite(PATH_MAX);
 
   while (true) {
-    if (::getcwd(result.data(), result.capacity()) == nullptr) {
+    if (::getcwd(result.data(), result.size()) == nullptr) {
       // See if there was a real error.
-      if (errno != ENOMEM)
+      if (errno != ENOMEM) {
+        result.clear();
         return std::error_code(errno, std::generic_category());
+      }
       // Otherwise there just wasn't enough space.
-      result.reserve(result.capacity() * 2);
+      result.resize_for_overwrite(result.capacity() * 2);
     } else
       break;
   }
 
-  result.set_size(strlen(result.data()));
+  result.truncate(strlen(result.data()));
   return std::error_code();
 }
 


        


More information about the llvm-commits mailing list