diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index ed56198d6..a65b5eb1b 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -262,7 +262,7 @@ std::string Preprocessor::read(std::istream &istr) ++lineno; // 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 = ' '; // Skip spaces after ' ' and after '#' @@ -274,7 +274,7 @@ std::string Preprocessor::read(std::istream &istr) { if (ch == '(') code << " "; - else if (! std::isalpha(ch)) + else if ((ch > 0) && ! std::isalpha(ch)) needSpace = false; } if (ch == '#') @@ -287,7 +287,7 @@ std::string Preprocessor::read(std::istream &istr) while (true) { chNext = (char)istr.peek(); - if (chNext != '\n' && chNext != '\r' && + if (chNext != '\n' && chNext != '\r' && (chNext > 0) && (std::isspace(chNext) || std::iscntrl(chNext))) { // Skip whitespace between and diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 73d5470ab..56beff398 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -126,7 +126,9 @@ private: TEST_CASE(endifsemicolon); TEST_CASE(missing_doublequote); - TEST_CASE(unicode1); + TEST_CASE(unicodeInCode); + TEST_CASE(unicodeInComment); + TEST_CASE(unicodeInString); TEST_CASE(define_part_of_func); TEST_CASE(conditionalDefine); 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); 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() { errout.str("");