Skip to content

Commit b49997e

Browse files
author
Daniel Marjamäki
committed
bail out instead of crash when parsing unicode code (cppcheck-opensource#207)
1 parent 7b58e09 commit b49997e

5 files changed

Lines changed: 62 additions & 32 deletions

File tree

‎src/cppcheck.cpp‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <cstring>
3333
#include <fstream>
3434
#include <map>
35+
#include <stdexcept>
3536

3637
//---------------------------------------------------------------------------
3738

@@ -297,44 +298,53 @@ unsigned int CppCheck::check()
297298
if (_settings._errorsOnly == false)
298299
_errorLogger->reportOut(std::string("Checking ") + fname + std::string("..."));
299300

300-
Preprocessor preprocessor;
301-
std::list<std::string> configurations;
302-
std::string filedata = "";
303-
if (_fileContents.size() > 0 && _fileContents.find(_filenames[c]) != _fileContents.end())
301+
try
304302
{
305-
// File content was given as a string
306-
std::istringstream iss(_fileContents[ _filenames[c] ]);
307-
preprocessor.preprocess(iss, filedata, configurations, fname, _includePaths);
308-
}
309-
else
310-
{
311-
// Only file name was given, read the content from file
312-
std::ifstream fin(fname.c_str());
313-
preprocessor.preprocess(fin, filedata, configurations, fname, _includePaths);
314-
}
303+
Preprocessor preprocessor;
304+
std::list<std::string> configurations;
305+
std::string filedata = "";
315306

316-
int checkCount = 0;
317-
for (std::list<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it)
318-
{
319-
// Check only 12 first configurations, after that bail out, unless --force
320-
// was used.
321-
if (!_settings._force && checkCount > 11)
307+
if (_fileContents.size() > 0 && _fileContents.find(_filenames[c]) != _fileContents.end())
322308
{
323-
if (_settings._errorsOnly == false)
324-
_errorLogger->reportOut(std::string("Bailing out from checking ") + fname + ": Too many configurations. Recheck this file with --force if you want to check them all.");
325-
326-
break;
309+
// File content was given as a string
310+
std::istringstream iss(_fileContents[ _filenames[c] ]);
311+
preprocessor.preprocess(iss, filedata, configurations, fname, _includePaths);
327312
}
313+
else
314+
{
315+
// Only file name was given, read the content from file
316+
std::ifstream fin(fname.c_str());
317+
preprocessor.preprocess(fin, filedata, configurations, fname, _includePaths);
318+
}
319+
320+
int checkCount = 0;
321+
for (std::list<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it)
322+
{
323+
// Check only 12 first configurations, after that bail out, unless --force
324+
// was used.
325+
if (!_settings._force && checkCount > 11)
326+
{
327+
if (_settings._errorsOnly == false)
328+
_errorLogger->reportOut(std::string("Bailing out from checking ") + fname + ": Too many configurations. Recheck this file with --force if you want to check them all.");
329+
330+
break;
331+
}
328332

329-
cfg = *it;
330-
std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, fname, _errorLogger);
333+
cfg = *it;
334+
std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, fname, _errorLogger);
331335

332-
// If only errors are printed, print filename after the check
333-
if (_settings._errorsOnly == false && it != configurations.begin())
334-
_errorLogger->reportOut(std::string("Checking ") + fname + ": " + cfg + std::string("..."));
336+
// If only errors are printed, print filename after the check
337+
if (_settings._errorsOnly == false && it != configurations.begin())
338+
_errorLogger->reportOut(std::string("Checking ") + fname + ": " + cfg + std::string("..."));
335339

336-
checkFile(codeWithoutCfg, _filenames[c].c_str());
337-
++checkCount;
340+
checkFile(codeWithoutCfg, _filenames[c].c_str());
341+
++checkCount;
342+
}
343+
}
344+
catch (std::runtime_error &e)
345+
{
346+
// Exception was thrown when checking this file..
347+
_errorLogger->reportOut("Bailing out from checking " + fname + ": " + e.what());
338348
}
339349

340350
_errorLogger->reportStatus(c + 1, _filenames.size());

‎src/preprocessor.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "token.h"
2424

2525
#include <algorithm>
26+
#include <stdexcept>
2627
#include <sstream>
2728
#include <fstream>
2829
#include <iostream>
@@ -71,7 +72,7 @@ std::string Preprocessor::read(std::istream &istr)
7172
for (char ch = readChar(istr); istr.good(); ch = readChar(istr))
7273
{
7374
if (ch < 0)
74-
continue;
75+
throw std::runtime_error("The code contains characters that are unhandled");
7576

7677
if (ch == '\n')
7778
++lineno;

‎test/testpreprocessor.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "../src/tokenize.h"
2828
#include <map>
2929
#include <string>
30+
#include <sstream>
31+
#include <stdexcept>
3032

3133
extern std::ostringstream errout;
3234

@@ -115,6 +117,8 @@ class TestPreprocessor : public TestFixture
115117
TEST_CASE(pragma);
116118
TEST_CASE(endifsemicolon);
117119
TEST_CASE(missing_doublequote);
120+
121+
TEST_CASE(unicode1);
118122
}
119123

120124

@@ -850,6 +854,13 @@ class TestPreprocessor : public TestFixture
850854
ASSERT_EQUALS("[abc.h:2]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
851855
}
852856
}
857+
858+
void unicode1()
859+
{
860+
const char filedata[] = {'a', (char)200, 0};
861+
std::istringstream istr(filedata);
862+
ASSERT_THROW(Preprocessor::read(istr), std::runtime_error);
863+
}
853864
};
854865

855866
REGISTER_TEST(TestPreprocessor)

‎test/testsuite.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ void TestFixture::assertEquals(const char *filename, int linenr, unsigned int ex
123123
assertEquals(filename, linenr, ostr1.str(), ostr2.str());
124124
}
125125

126+
void TestFixture::assertThrowFail(const char *filename, int linenr)
127+
{
128+
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl
129+
<< "The expected exception was not thrown" << std::endl;
130+
}
131+
126132
void TestFixture::printTests()
127133
{
128134
const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();

‎test/testsuite.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestFixture : public ErrorLogger
4141

4242
void assertEquals(const char *filename, int linenr, const std::string &expected, const std::string &actual);
4343
void assertEquals(const char *filename, int linenr, unsigned int expected, unsigned int actual);
44+
void assertThrowFail(const char *filename, int linenr);
4445

4546
public:
4647
virtual void reportOut(const std::string &outmsg);
@@ -58,6 +59,7 @@ class TestFixture : public ErrorLogger
5859

5960
#define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME ();
6061
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
62+
#define ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (EXCEPTION &) { } catch (...) { assertThrowFail(__FILE__, __LINE__); }
6163
#define TODO_ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED==ACTUAL) assertEquals(__FILE__, __LINE__, "TODO assertion", "The assertion succeeded")
6264
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; }
6365

0 commit comments

Comments
 (0)