diff options
author | Matheus Izvekov <mizvekov@gmail.com> | 2024-01-18 04:03:03 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-18 08:03:03 +0100 |
commit | 361016f680abf830004ef726f816820a0c8c1950 (patch) | |
tree | c342f8ffa768bd49400341a9d3aa66e50224db6f /llvm/lib/Support/Path.cpp | |
parent | baaf0c968edd1a11ba0d1e2ae97b8578455ceed2 (diff) | |
download | llvm-361016f680abf830004ef726f816820a0c8c1950.zip llvm-361016f680abf830004ef726f816820a0c8c1950.tar.gz llvm-361016f680abf830004ef726f816820a0c8c1950.tar.bz2 |
[Path] Fix off-by-one in finding filename for win style paths (#78055)
This fixes a crash where `path::parent_path` causes an invalid access on
a string upon receiving a path that consists of a single colon.
On Windows machine, with runtime checks enabled build, upon `clang -I:
test.cc` produces:
```
Assertion failed: Index < Length && "Invalid index!", file llvm\include\llvm/ADT/StringRef.h, line 232
...
#6 0x00007ff7816201eb `anonymous namespace'::parent_path_end llvm\lib\Support\Path.cpp:144:0
#7 0x00007ff781620135 llvm::sys::path::parent_path(class llvm::StringRef, enum llvm::sys::path::Style) llvm\lib\Support\Path.cpp:470:0
```
Ideally, we can look for the last colon starting from the last
character, but we can instead start from second to last, and handle
empty paths by abusing `0 - 1 == npos`.
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 9410252..c8de2c0 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -104,7 +104,7 @@ namespace { if (is_style_windows(style)) { if (pos == StringRef::npos) - pos = str.find_last_of(':', str.size() - 2); + pos = str.find_last_of(':', str.size() - 1); } if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style))) |