Skip to content

Commit 1dfd8ca

Browse files
committed
do not treat directories like regular files in existence checks
1 parent 986ff24 commit 1dfd8ca

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ int main(int argc, char **argv)
121121
std::cout << "error: could not open file '" << filename << "'" << std::endl;
122122
std::exit(1);
123123
}
124+
if (!simplecpp::isFile(filename)) {
125+
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
126+
std::exit(1);
127+
}
124128
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
125129
}
126130
else {

simplecpp.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,9 +3019,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
30193019
if (nonExistingFilesCache.contains(simplePath))
30203020
return ""; // file is known not to exist, skip expensive file open call
30213021
#endif
3022-
f.open(simplePath.c_str());
3023-
if (f.is_open())
3024-
return simplePath;
3022+
if (simplecpp::isFile(simplePath)) {
3023+
f.open(simplePath.c_str());
3024+
if (f.is_open())
3025+
return simplePath;
3026+
}
30253027
#ifdef SIMPLECPP_WINDOWS
30263028
nonExistingFilesCache.add(simplePath);
30273029
#endif
@@ -3126,18 +3128,19 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
31263128
if (ret.find(filename) != ret.end())
31273129
continue;
31283130

3129-
std::ifstream fin(filename.c_str());
3130-
if (!fin.is_open()) {
3131-
if (outputList) {
3132-
simplecpp::Output err(filenames);
3133-
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3134-
err.location = Location(filenames);
3135-
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3136-
outputList->push_back(err);
3131+
{
3132+
std::ifstream fin(filename.c_str());
3133+
if (!fin.is_open() || !isFile(filename)) {
3134+
if (outputList) {
3135+
simplecpp::Output err(filenames);
3136+
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3137+
err.location = Location(filenames);
3138+
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3139+
outputList->push_back(err);
3140+
}
3141+
continue;
31373142
}
3138-
continue;
31393143
}
3140-
fin.close();
31413144

31423145
TokenList *tokenlist = new TokenList(filename, filenames, outputList);
31433146
if (!tokenlist->front()) {

test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,44 @@ static void missingHeader3()
17761776
ASSERT_EQUALS("", toString(outputList));
17771777
}
17781778

1779+
#ifndef _WIN32
1780+
static void missingHeader4()
1781+
{
1782+
// this is a directory
1783+
const char code[] = "#include \"/\"\n";
1784+
simplecpp::OutputList outputList;
1785+
ASSERT_EQUALS("", preprocess(code, &outputList));
1786+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
1787+
}
1788+
1789+
static void missingHeader5()
1790+
{
1791+
// this is a directory
1792+
const char code[] = "#include \"/usr\"\n";
1793+
simplecpp::OutputList outputList;
1794+
ASSERT_EQUALS("", preprocess(code, &outputList));
1795+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
1796+
}
1797+
1798+
static void missingHeader6()
1799+
{
1800+
// this is a directory
1801+
const char code[] = "#include </>\n";
1802+
simplecpp::OutputList outputList;
1803+
ASSERT_EQUALS("", preprocess(code, &outputList));
1804+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </>\n", toString(outputList));
1805+
}
1806+
1807+
static void missingHeader7()
1808+
{
1809+
// this is a directory
1810+
const char code[] = "#include </usr>\n";
1811+
simplecpp::OutputList outputList;
1812+
ASSERT_EQUALS("", preprocess(code, &outputList));
1813+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </usr>\n", toString(outputList));
1814+
}
1815+
#endif
1816+
17791817
static void nestedInclude()
17801818
{
17811819
const char code[] = "#include \"test.h\"\n";
@@ -2876,6 +2914,12 @@ int main(int argc, char **argv)
28762914
TEST_CASE(missingHeader1);
28772915
TEST_CASE(missingHeader2);
28782916
TEST_CASE(missingHeader3);
2917+
#ifndef _WIN32
2918+
TEST_CASE(missingHeader4);
2919+
TEST_CASE(missingHeader5);
2920+
TEST_CASE(missingHeader6);
2921+
TEST_CASE(missingHeader7);
2922+
#endif
28792923
TEST_CASE(nestedInclude);
28802924
TEST_CASE(systemInclude);
28812925

0 commit comments

Comments
 (0)