[PATCH] D83321: [Support] Fix utf16 path's index upper bound

Ding Fei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 9 06:06:25 PDT 2020


danix800 added a comment.

It's just so obvious.

The unit-test `TEST_F(FileSystemTest, widenPath)` in `llvm/unittests/Support/Path.cpp:2080-2146`
has already demonstrated this.

  #ifdef _WIN32
  TEST_F(FileSystemTest, widenPath) {
    const std::wstring LongPathPrefix(L"\\\\?\\");
  
    // Test that the length limit is checked against the UTF-16 length and not the
    // UTF-8 length.
    std::string Input("C:\\foldername\\");
    const std::string Pi("\xcf\x80"); // UTF-8 lower case pi.
    // Add Pi up to the MAX_PATH limit.
    const size_t NumChars = MAX_PATH - Input.size() - 1;
    for (size_t i = 0; i < NumChars; ++i)
      Input += Pi;
    // Check that UTF-8 length already exceeds MAX_PATH.
    EXPECT_TRUE(Input.size() > MAX_PATH);
    SmallVector<wchar_t, MAX_PATH + 16> Result;
    ASSERT_NO_ERROR(windows::widenPath(Input, Result));
    // Result should not start with the long path prefix.
    EXPECT_TRUE(std::wmemcmp(Result.data(), LongPathPrefix.c_str(),
                             LongPathPrefix.size()) != 0);
    EXPECT_EQ(Result.size(), (size_t)MAX_PATH - 1);
  
    // Add another Pi to exceed the MAX_PATH limit.

`Input.size() > MAX_PATH` is expected to be true and `Result.size()` is expected to be `MAX_PATH - 1`, meaning
that `PathUTF16[Path.size() - 1]` is an out-of-bound array access.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83321/new/

https://reviews.llvm.org/D83321





More information about the llvm-commits mailing list