- PVSM.RU - https://www.pvsm.ru -

How the CSS markup fragment broke the C++ compiler

Picture 1

Static analysis methodology involves various technologies. One of them is preprocessing files right before analyzing them. Preprocessed files are created by the compiler that runs in a special working mode. Unfortunately, our long-standing experience of developing a static analyzer shows that this mode is not great for testing. In this note, I'll give the example of a fresh bug in the C++ compiler from Microsoft.

Introduction

To demonstrate capabilities of the PVS-Studio static analyzer our team checks code of open source projects. This is a significant contribution [1] to the quality of open source software, extra publicity and testing of the analyzer. Sometimes we reveal some very unusual problems in compilers, which are difficult to deal with on the part of the analyzer. Thus, my colleague has recently written the article "The file with the 'import' directive is no longer working (compiler internal error 'msc1.cpp'). What shall we do? [2]" to help our users in solving «someone else's» problem.

What's connection with CSS?

I've just found a no less interesting bug when checking a large project. The Micfosoft compiler for C/C++ of the 19.16.27027.1 (Visual Studio v15.9.9) version has just issued such an error when analyzing several files:

fatal error C1021: invalid preprocessor command 'tooltiphint'

Obviously, it is not a preprocessor directive, but what is it? This is a fragment of CSS code:

#tooltiphint {
  position: fixed;
  width: 50em;
  margin-left: -25em;
  left: 50%;
  padding: 10px;
  border: 1px solid #b0b0b0;
  border-radius: 2px;
  box-shadow: 1px 1px 7px black;
  background-color: #c0c0c0;
  z-index: 2;
}

After taking a close look at the fragment, it became clear that the compiler gets wrong when preprocessing the file, whereas the code is compiled successfully. The fragment of the CSS code is a part of the C++ code string literal. Here is the example of code, enough to reproduce the error:

std::string test = R"<<<(
<style type="text/css">
body { color:#000000; background-color:#ffffff }
body { font-family:Helvetica, sans-serif; font-size:10pt }

#tooltiphint {
  position: fixed;
  width: 50em;
  margin-left: -25em;
  left: 50%;
  padding: 10px;
  border: 1px solid #b0b0b0;
  border-radius: 2px;
  box-shadow: 1px 1px 7px black;
  background-color: #c0c0c0;
  z-index: 2;
}
.macro {
  color: darkmagenta;
  background-color:LemonChiffon;
  /* Macros are position: relative to provide base for expansions. */
  position: relative;
}

</style>
</head>
<body>)<<<";

The above code fragment does not prevent successful compilation but, at the same time, an error will occur in the preprocessing mode (/P [3] flag).

As you can see, the life of static analyzer developers is anything but simple :). It seems like PVS-Studio is not to blame, but we still have to deal with such problems. Well, no fresh news. You can check out some other similar cases in the article "PVS-Studio and Hostile Environment [4]".

Conclusion

This issue will be sent to the official bug-tracker, but a quick solution of the problem is hardly possible. For example, the problem with the #import directive, mentioned at the beginning, which we detected several months ago, will be fixed only in the next Visual Studio release. Since the release of the new Visual Studio 2019 will be in a week, most likely they will not manage to fix this bug by that date. Users of PVS-Studio, we recommend that you also use the macro PVS_STUDIO [5].

Автор: SvyatoslavMC

Источник [6]


Сайт-источник PVSM.RU: https://www.pvsm.ru

Путь до страницы источника: https://www.pvsm.ru/microsoft/312743

Ссылки в тексте:

[1] contribution: https://www.viva64.com/en/inspections/

[2] The file with the 'import' directive is no longer working (compiler internal error 'msc1.cpp'). What shall we do?: https://www.viva64.com/en/b/0613/

[3] /P: https://docs.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=vs-2017

[4] PVS-Studio and Hostile Environment: https://www.viva64.com/en/b/0303/

[5] PVS_STUDIO: https://www.viva64.com/en/m/0020/

[6] Источник: https://habr.com/ru/post/445496/?utm_campaign=445496