Skip to content

Commit 0249554

Browse files
authored
[clang-tidy] fix incorrect configuration file path resolving when file paths contain .. (#121323)
`makeAbsolute` will not normalize path. When getting parent folder, `..` will go into the subfolder instead of the parent folder.
1 parent ae9bf17 commit 0249554

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

clang-tools-extra/clang-tidy/ClangTidyOptions.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/SmallString.h"
1313
#include "llvm/Support/Debug.h"
1414
#include "llvm/Support/Errc.h"
15+
#include "llvm/Support/ErrorOr.h"
1516
#include "llvm/Support/FileSystem.h"
1617
#include "llvm/Support/MemoryBufferRef.h"
1718
#include "llvm/Support/Path.h"
@@ -298,12 +299,11 @@ ConfigOptionsProvider::getRawOptions(llvm::StringRef FileName) {
298299
if (ConfigOptions.InheritParentConfig.value_or(false)) {
299300
LLVM_DEBUG(llvm::dbgs()
300301
<< "Getting options for file " << FileName << "...\n");
301-
assert(FS && "FS must be set.");
302302

303-
llvm::SmallString<128> AbsoluteFilePath(FileName);
304-
305-
if (!FS->makeAbsolute(AbsoluteFilePath)) {
306-
addRawFileOptions(AbsoluteFilePath, RawOptions);
303+
llvm::ErrorOr<llvm::SmallString<128>> AbsoluteFilePath =
304+
getNormalizedAbsolutePath(FileName);
305+
if (AbsoluteFilePath) {
306+
addRawFileOptions(AbsoluteFilePath->str(), RawOptions);
307307
}
308308
}
309309
RawOptions.emplace_back(ConfigOptions,
@@ -334,6 +334,17 @@ FileOptionsBaseProvider::FileOptionsBaseProvider(
334334
OverrideOptions(std::move(OverrideOptions)),
335335
ConfigHandlers(std::move(ConfigHandlers)) {}
336336

337+
llvm::ErrorOr<llvm::SmallString<128>>
338+
FileOptionsBaseProvider::getNormalizedAbsolutePath(llvm::StringRef Path) {
339+
assert(FS && "FS must be set.");
340+
llvm::SmallString<128> NormalizedAbsolutePath = {Path};
341+
std::error_code Err = FS->makeAbsolute(NormalizedAbsolutePath);
342+
if (Err)
343+
return Err;
344+
llvm::sys::path::remove_dots(NormalizedAbsolutePath, /*remove_dot_dot=*/true);
345+
return NormalizedAbsolutePath;
346+
}
347+
337348
void FileOptionsBaseProvider::addRawFileOptions(
338349
llvm::StringRef AbsolutePath, std::vector<OptionsSource> &CurOptions) {
339350
auto CurSize = CurOptions.size();
@@ -397,16 +408,15 @@ std::vector<OptionsSource>
397408
FileOptionsProvider::getRawOptions(StringRef FileName) {
398409
LLVM_DEBUG(llvm::dbgs() << "Getting options for file " << FileName
399410
<< "...\n");
400-
assert(FS && "FS must be set.");
401-
402-
llvm::SmallString<128> AbsoluteFilePath(FileName);
403411

404-
if (FS->makeAbsolute(AbsoluteFilePath))
412+
llvm::ErrorOr<llvm::SmallString<128>> AbsoluteFilePath =
413+
getNormalizedAbsolutePath(FileName);
414+
if (!AbsoluteFilePath)
405415
return {};
406416

407417
std::vector<OptionsSource> RawOptions =
408-
DefaultOptionsProvider::getRawOptions(AbsoluteFilePath.str());
409-
addRawFileOptions(AbsoluteFilePath, RawOptions);
418+
DefaultOptionsProvider::getRawOptions(AbsoluteFilePath->str());
419+
addRawFileOptions(AbsoluteFilePath->str(), RawOptions);
410420
OptionsSource CommandLineOptions(OverrideOptions,
411421
OptionsSourceTypeCheckCommandLineOption);
412422

clang-tools-extra/clang-tidy/ClangTidyOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
1111

1212
#include "llvm/ADT/IntrusiveRefCntPtr.h"
13+
#include "llvm/ADT/SmallString.h"
1314
#include "llvm/ADT/StringMap.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/Support/ErrorOr.h"
@@ -237,6 +238,9 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
237238
void addRawFileOptions(llvm::StringRef AbsolutePath,
238239
std::vector<OptionsSource> &CurOptions);
239240

241+
llvm::ErrorOr<llvm::SmallString<128>>
242+
getNormalizedAbsolutePath(llvm::StringRef AbsolutePath);
243+
240244
/// Try to read configuration files from \p Directory using registered
241245
/// \c ConfigHandlers.
242246
std::optional<OptionsSource> tryReadConfigFile(llvm::StringRef Directory);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ Improvements to clang-tidy
117117
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
118118
happening on certain platforms when interrupting the script.
119119

120+
- Improved :program:`clang-tidy` by fixing incorrect configuration file path
121+
resolving when file paths contain ``..``.
122+
120123
- Removed :program:`clang-tidy`'s global options for most of checks. All options
121124
are changed to local options except `IncludeStyle`, `StrictMode` and
122125
`IgnoreMacros`. Global scoped `StrictMode` and `IgnoreMacros` are deprecated

clang-tools-extra/test/clang-tidy/infrastructure/Inputs/normalized-path/code.cpp

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
InvalidYamlFormat
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: clang-tidy %S/Inputs/normalized-path/error-config/../code.cpp --verify-config 2>&1 | FileCheck %s
2+
3+
// CHECK-NOT: Error parsing

0 commit comments

Comments
 (0)