diff --git a/Makefile b/Makefile index e69be1807..5d3fa4b02 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ TESTOBJ = test/options.o \ test/testdivision.o \ test/testerrorlogger.o \ test/testexceptionsafety.o \ + test/testfilelister_unix.o \ test/testincompletestatement.o \ test/testmathlib.o \ test/testmemleak.o \ @@ -171,7 +172,7 @@ lib/checkunusedfunctions.o: lib/checkunusedfunctions.cpp lib/checkunusedfunction lib/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/preprocessor.h lib/filelister.h lib/path.h lib/timer.h $(CXX) $(CXXFLAGS) -Ilib -c -o lib/cppcheck.o lib/cppcheck.cpp -lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/path.h +lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/path.h lib/cppcheck.h lib/settings.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h $(CXX) $(CXXFLAGS) -Ilib -c -o lib/errorlogger.o lib/errorlogger.cpp lib/executionpath.o: lib/executionpath.cpp lib/executionpath.h lib/token.h @@ -255,6 +256,9 @@ test/testerrorlogger.o: test/testerrorlogger.cpp test/testsuite.h lib/errorlogge test/testexceptionsafety.o: test/testexceptionsafety.cpp lib/tokenize.h lib/checkexceptionsafety.h lib/check.h lib/token.h lib/settings.h lib/errorlogger.h test/testsuite.h test/redirect.h $(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testexceptionsafety.o test/testexceptionsafety.cpp +test/testfilelister_unix.o: test/testfilelister_unix.cpp test/testsuite.h lib/errorlogger.h test/redirect.h + $(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testfilelister_unix.o test/testfilelister_unix.cpp + test/testincompletestatement.o: test/testincompletestatement.cpp test/testsuite.h lib/errorlogger.h test/redirect.h lib/tokenize.h lib/checkother.h lib/check.h lib/token.h lib/settings.h $(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testincompletestatement.o test/testincompletestatement.cpp diff --git a/lib/filelister_unix.cpp b/lib/filelister_unix.cpp index 556a34c75..fd2bf94dc 100644 --- a/lib/filelister_unix.cpp +++ b/lib/filelister_unix.cpp @@ -58,14 +58,15 @@ void FileListerUnix::recursiveAddFiles2(std::vector &relative, if (filename[filename.length()-1] != '/') { // File - char* const fname = realpath(filename.c_str(), NULL); - if (!fname) + char fname[PATH_MAX]; + if (realpath(filename.c_str(), fname) == NULL) + { continue; + } // Does absolute path exist? then bail out if (std::find(absolute.begin(), absolute.end(), std::string(fname)) != absolute.end()) { - free(fname); continue; } @@ -74,8 +75,6 @@ void FileListerUnix::recursiveAddFiles2(std::vector &relative, relative.push_back(filename); absolute.push_back(fname); } - - free(fname); } else { diff --git a/test/testfilelister_unix.cpp b/test/testfilelister_unix.cpp new file mode 100644 index 000000000..83c0c6168 --- /dev/null +++ b/test/testfilelister_unix.cpp @@ -0,0 +1,62 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "testsuite.h" + +#define private public + +#include "filelister_unix.h" + +class TestFileLister: public TestFixture +{ +public: + TestFileLister() + :TestFixture("TestFileLister") + {} + +private: + void run() + { + TEST_CASE(test_recursiveAddFiles2); + } + + void test_recursiveAddFiles2() + { + std::vector relative, absolute; + FileListerUnix ful; + ful.recursiveAddFiles2(relative, absolute, "."); + + ASSERT(relative.size() != 0); + ASSERT_EQUALS(relative.size(), absolute.size()); + + for (std::vector::const_iterator r = relative.begin(), r_end = relative.end(), + a = absolute.begin(), a_end = absolute.end(); + r != r_end && a != a_end; + ++r, ++a + ) + { + static const size_t start_at_relative = std::string("./").size(); + static const size_t start_at_absolute = std::string("./").size() + a->size() - r->size(); + + ASSERT_EQUALS(r->substr(start_at_relative), a->substr(start_at_absolute)); + } + } +}; + +REGISTER_TEST(TestFileLister) +