Fixed ticket #417 (Crashes in windows because of invalid char value)

negative char values are allowed in strings and comments. but not in other code.
This commit is contained in:
Daniel Marjamäki 2009-06-19 15:43:46 +02:00
parent f523b118ab
commit 58c7414ce0
2 changed files with 23 additions and 6 deletions

View File

@ -262,7 +262,7 @@ std::string Preprocessor::read(std::istream &istr)
++lineno; ++lineno;
// Replace assorted special chars with spaces.. // Replace assorted special chars with spaces..
if ((ch != '\n') && (std::isspace(ch) || std::iscntrl(ch))) if ((ch > 0) && (ch != '\n') && (std::isspace(ch) || std::iscntrl(ch)))
ch = ' '; ch = ' ';
// Skip spaces after ' ' and after '#' // Skip spaces after ' ' and after '#'
@ -274,7 +274,7 @@ std::string Preprocessor::read(std::istream &istr)
{ {
if (ch == '(') if (ch == '(')
code << " "; code << " ";
else if (! std::isalpha(ch)) else if ((ch > 0) && ! std::isalpha(ch))
needSpace = false; needSpace = false;
} }
if (ch == '#') if (ch == '#')
@ -287,7 +287,7 @@ std::string Preprocessor::read(std::istream &istr)
while (true) while (true)
{ {
chNext = (char)istr.peek(); chNext = (char)istr.peek();
if (chNext != '\n' && chNext != '\r' && if (chNext != '\n' && chNext != '\r' && (chNext > 0) &&
(std::isspace(chNext) || std::iscntrl(chNext))) (std::isspace(chNext) || std::iscntrl(chNext)))
{ {
// Skip whitespace between <backspace> and <newline> // Skip whitespace between <backspace> and <newline>

View File

@ -126,7 +126,9 @@ private:
TEST_CASE(endifsemicolon); TEST_CASE(endifsemicolon);
TEST_CASE(missing_doublequote); TEST_CASE(missing_doublequote);
TEST_CASE(unicode1); TEST_CASE(unicodeInCode);
TEST_CASE(unicodeInComment);
TEST_CASE(unicodeInString);
TEST_CASE(define_part_of_func); TEST_CASE(define_part_of_func);
TEST_CASE(conditionalDefine); TEST_CASE(conditionalDefine);
TEST_CASE(multiline_comment); TEST_CASE(multiline_comment);
@ -996,13 +998,28 @@ private:
} }
} }
void unicode1() void unicodeInCode()
{ {
const char filedata[] = {'a', (char)200, 0}; const std::string filedata(std::string("a") + char(200));
std::istringstream istr(filedata); std::istringstream istr(filedata);
ASSERT_THROW(Preprocessor::read(istr), std::runtime_error); ASSERT_THROW(Preprocessor::read(istr), std::runtime_error);
} }
void unicodeInComment()
{
const std::string filedata(std::string("//") + char(200));
std::istringstream istr(filedata.c_str());
ASSERT_EQUALS("", Preprocessor::read(istr));
}
void unicodeInString()
{
const std::string filedata(std::string("\"") + char(200) + "\"");
std::istringstream istr(filedata.c_str());
ASSERT_EQUALS(filedata, Preprocessor::read(istr));
}
void define_part_of_func() void define_part_of_func()
{ {
errout.str(""); errout.str("");