diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 0bb1abf97..7981d0f52 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -508,8 +508,11 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri // Ticket 4873: Extract comments from the __asm / __asm__'s content std::string asmBody; while (i < str.size() && str[i] != '}') { - if (str[i] == ';') - i = str.find("\n", i); + if (str[i] == ';') { + std::string::size_type backslashN = str.find("\n", i); + if (backslashN != std::string::npos) // Ticket #4922: Don't go in infinite loop or crash if there is no '\n' + i = backslashN; + } asmBody += str[i++]; } code << removeComments(asmBody, filename); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 0c744fb17..89a216329 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -149,6 +149,7 @@ private: TEST_CASE(if_macro_eq_macro); // #3536 TEST_CASE(ticket_3675); TEST_CASE(ticket_3699); + TEST_CASE(ticket_4922); // #4922 TEST_CASE(multiline1); TEST_CASE(multiline2); @@ -1704,6 +1705,17 @@ private: ASSERT_EQUALS("\n\n\n\n\n$$$__forceinline $$inline $$__forceinline\n", actual[""]); } + void ticket_4922() {// #4922 + const std::string code("__asm__ \n" + "{ int extern __value) 0; (double return (\"\" } extern\n" + "__typeof __finite (__finite) __finite __inline \"__GI___finite\");"); + Settings settings; + Preprocessor preprocessor(&settings, this); + std::istringstream istr(code); + std::map actual; + preprocessor.preprocess(istr, actual, "file.cpp"); + } + void multiline1() { const char filedata[] = "#define str \"abc\" \\\n" " \"def\" \n"