Skip to content
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

[Path] Fix off-by-one in finding filename for win style paths #78055

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

mizvekov
Copy link
Contributor

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.

@mizvekov mizvekov self-assigned this Jan 13, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 13, 2024

@llvm/pr-subscribers-llvm-support

Author: Matheus Izvekov (mizvekov)

Changes

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 &lt; Length &amp;&amp; "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.


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

2 Files Affected:

  • (modified) llvm/lib/Support/Path.cpp (+1-1)
  • (modified) llvm/unittests/Support/Path.cpp (+1)
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index c2456dcac0974a..1c441100679ed7 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)))
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index a7b7e6a0f5044d..837ca03216f87a 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -190,6 +190,7 @@ TEST(Support, Path) {
   paths.push_back("c:\\foo\\");
   paths.push_back("c:\\foo/");
   paths.push_back("c:/foo\\bar");
+  paths.push_back(":");
 
   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
                                                   e = paths.end();

@mizvekov mizvekov changed the title [Path] Dix off-by-one in finding filename for win style paths [Path] Fix off-by-one in finding filename for win style paths Jan 13, 2024
Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

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

Didn't think about it too hard - but sounds plausible.

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`.
@mizvekov mizvekov force-pushed the users/mizvekov/bug/sys-path-win-colon branch from bcf9f92 to eda99a5 Compare January 18, 2024 02:49
@mizvekov mizvekov merged commit 361016f into main Jan 18, 2024
6 of 7 checks passed
@mizvekov mizvekov deleted the users/mizvekov/bug/sys-path-win-colon branch January 18, 2024 07:03
ampandey-1995 pushed a commit to ampandey-1995/llvm-project that referenced this pull request Jan 19, 2024
…8055)

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
...
 llvm#6 0x00007ff7816201eb `anonymous namespace'::parent_path_end llvm\lib\Support\Path.cpp:144:0
 llvm#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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants