Skip to content

Commit 2ead128

Browse files
committed
Fix check for path to account for escaped characters
1 parent 21e20d2 commit 2ead128

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/filepattern/cpp/internal/filepattern.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "filepattern.hpp"
2+
#include "../util/util.hpp"
23
#include <chrono>
34

45
using namespace std;
@@ -25,7 +26,7 @@ FilePatternObject::FilePatternObject(const string& path, const string& file_patt
2526
this->recursive_ = recursive; // Iterate over subdirectories
2627

2728
// check if filepattern contains directory capturing
28-
if (file_pattern.find('/') != std::string::npos || file_pattern.find('\\') != std::string::npos) {
29+
if (s::isPath(file_pattern)) {
2930
this->setCaptureDirectoryNames(true);
3031
this->recursive_ = true; // need to be recursive to capture directory names
3132

@@ -76,6 +77,7 @@ void FilePatternObject::matchFilesOneDir(){
7677
auto length = end - start;
7778
throw invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\"");
7879
}
80+
7981
regex pattern_regex = regex(this->getRegexFilePattern());
8082
smatch sm;
8183

@@ -115,7 +117,7 @@ void FilePatternObject::matchFilesMultDir(){
115117
}
116118

117119
if(regex_match(file, sm, pattern_regex)){
118-
120+
119121
if(this->getJustPath() || this->captureDirectoryNames()) {
120122
tup = getVariableMap(file_path, sm);
121123
} else {

src/filepattern/cpp/util/util.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,35 @@ static const std::string SLASH = "/";
5757
#endif
5858

5959

60+
61+
6062
/**
6163
* @brief Functions for std::strings and Types.
6264
*
6365
*/
6466
namespace s {
6567

68+
inline bool isPath(const std::string& str) {
69+
70+
if (str.find('/') != std::string::npos) {
71+
return true;
72+
}
73+
74+
// regex characters to escape
75+
const std::unordered_set<char> escape_chars = {'*', '?', '^', '$', '(', ')', '[', ']', '|'};
76+
77+
for (auto i = 0; i < str.size()-1; ++i) {
78+
if (str[i] == '\\' &&
79+
std::find(std::begin(escape_chars), std::end(escape_chars), str[i+1]) == std::end(escape_chars)
80+
) {
81+
return true; // Contains a slash that is not proceeded by a character being escaped
82+
}
83+
}
84+
85+
// no slashes were found that are not used for escaping characters
86+
return false;
87+
}
88+
6689
inline std::string escapeForwardSlashes(const std::string& input) {
6790
std::string result;
6891

0 commit comments

Comments
 (0)