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 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?" 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 Microsoft 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 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".

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.

UPD: A fix for this issue has been released in VS 2019 16.0 Preview 4 and VS 2019 16.0 RC.



Use PVS-Studio to search for bugs in C, C++, C# and Java

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.

goto PVS-Studio;


Bugs Found

Checked Projects
346
Collected Errors
13 188