Skip to content

Commit ac223ba

Browse files
committed
do not treat directories like regular files in existence checks
1 parent 1e851c6 commit ac223ba

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
@@ -117,6 +117,10 @@ int main(int argc, char **argv)
117117
std::cout << "error: could not open file '" << filename << "'" << std::endl;
118118
std::exit(1);
119119
}
120+
if (!simplecpp::isFile(filename)) {
121+
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
122+
std::exit(1);
123+
}
120124
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
121125
} else {
122126
rawtokens = new simplecpp::TokenList(filename,files,&outputList);

simplecpp.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,9 +3165,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
31653165
if (nonExistingFilesCache.contains(simplePath))
31663166
return ""; // file is known not to exist, skip expensive file open call
31673167
#endif
3168-
f.open(simplePath.c_str());
3169-
if (f.is_open())
3170-
return simplePath;
3168+
if (simplecpp::isFile(simplePath)) {
3169+
f.open(simplePath.c_str());
3170+
if (f.is_open())
3171+
return simplePath;
3172+
}
31713173
#ifdef SIMPLECPP_WINDOWS
31723174
nonExistingFilesCache.add(simplePath);
31733175
#endif
@@ -3302,18 +3304,19 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
33023304
if (ret.find(filename) != ret.end())
33033305
continue;
33043306

3305-
std::ifstream fin(filename.c_str());
3306-
if (!fin.is_open()) {
3307-
if (outputList) {
3308-
simplecpp::Output err(filenames);
3309-
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3310-
err.location = Location(filenames);
3311-
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3312-
outputList->push_back(err);
3307+
{
3308+
std::ifstream fin(filename.c_str());
3309+
if (!fin.is_open() || !isFile(filename)) {
3310+
if (outputList) {
3311+
simplecpp::Output err(filenames);
3312+
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3313+
err.location = Location(filenames);
3314+
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3315+
outputList->push_back(err);
3316+
}
3317+
continue;
33133318
}
3314-
continue;
33153319
}
3316-
fin.close();
33173320

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

test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,44 @@ static void missingHeader4()
19331933
ASSERT_EQUALS("file0,1,syntax_error,No header in #include\n", toString(outputList));
19341934
}
19351935

1936+
#ifndef _WIN32
1937+
static void missingHeader5()
1938+
{
1939+
// this is a directory
1940+
const char code[] = "#include \"/\"\n";
1941+
simplecpp::OutputList outputList;
1942+
ASSERT_EQUALS("", preprocess(code, &outputList));
1943+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
1944+
}
1945+
1946+
static void missingHeader6()
1947+
{
1948+
// this is a directory
1949+
const char code[] = "#include \"/usr\"\n";
1950+
simplecpp::OutputList outputList;
1951+
ASSERT_EQUALS("", preprocess(code, &outputList));
1952+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
1953+
}
1954+
1955+
static void missingHeader7()
1956+
{
1957+
// this is a directory
1958+
const char code[] = "#include </>\n";
1959+
simplecpp::OutputList outputList;
1960+
ASSERT_EQUALS("", preprocess(code, &outputList));
1961+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </>\n", toString(outputList));
1962+
}
1963+
1964+
static void missingHeader8()
1965+
{
1966+
// this is a directory
1967+
const char code[] = "#include </usr>\n";
1968+
simplecpp::OutputList outputList;
1969+
ASSERT_EQUALS("", preprocess(code, &outputList));
1970+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </usr>\n", toString(outputList));
1971+
}
1972+
#endif
1973+
19361974
static void nestedInclude()
19371975
{
19381976
const char code[] = "#include \"test.h\"\n";
@@ -3168,6 +3206,12 @@ int main(int argc, char **argv)
31683206
TEST_CASE(missingHeader2);
31693207
TEST_CASE(missingHeader3);
31703208
TEST_CASE(missingHeader4);
3209+
#ifndef _WIN32
3210+
TEST_CASE(missingHeader5);
3211+
TEST_CASE(missingHeader6);
3212+
TEST_CASE(missingHeader7);
3213+
TEST_CASE(missingHeader8);
3214+
#endif
31713215
TEST_CASE(nestedInclude);
31723216
TEST_CASE(systemInclude);
31733217

0 commit comments

Comments
 (0)