Skip to content

Commit

Permalink
Support: Avoid SmallVector::set_size() in Unix code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dexonsmith committed Jan 12, 2022
1 parent 4d44394 commit b0492d9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions llvm/lib/Support/Unix/Path.inc
Expand Up @@ -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();
}

Expand Down

0 comments on commit b0492d9

Please sign in to comment.