To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
A bug caused by the #line directive in …

A bug caused by the #line directive in the Visual C++ compiler

May 20 2020

The #line directive is added by the preprocessor and can then be used to help the developer understand which file and line a particular code fragment in the preprocessed file refers to. The #line directive tells code-processing tools to change the compiler's internally stored line number and filename to a given line number and filename. Subsequent lines will be numbered relative to that position. Explicit preprocessing is mostly used for debugging or by various generators. In any case, a bug breaking this functionality may have a variety of negative effects. One of our users was faced with such a problem in Visual Studio 2019.

0736_VisualCppLineBug/image1.png

The background of the bug

The #line directive is an important part of preprocessed files used by PVS-Studio to issue warnings on problem code lines in users' programs. This is where the story of hunting down the bug begins.

One of our users, who had a large code base, started moving his project from Platform Toolset v141 to v142 in Visual Studio 2019. After that, he got a pile of V002 warnings and several dozens of other warnings pointing at wrong code lines (1 line ahead of the actual one).

Incorrect line numbering is exactly the type of issues that diagnostic V002 detects. Such bugs typically have to do with multiline macros or other preprocessor directives (such as #pragma). It's not a common problem, but it's mostly seen with Microsoft's compiler. We were surprised to see so many warnings issued on one project as our testing system had never revealed any serious problems like that. So we started debugging the analyzer based on the user's preprocessed file.

A few hours later, we finally found that PVS-Studio was already working with incorrect line information, and were able to write a test case:

#define MACRO(a, b)              // line1
                                 // line2
int main(int argc, char* argv[]) // line3
{                                // line4
    MACRO("Some string",         // line5
        42);                     // line6
    return 0;                    // line7 <=
}

This is what we get as a preprocessed file:

#line 1 "D:\\_Tests\\TestApp\\Project2\\rnd_bs.cpp"


int main(int argc, char* argv[])
{
  ;
#line 8 "D:\\_Tests\\TestApp\\Project2\\rnd_bs.cpp"
  return 0;
}

The return statement is at line 7 in the source file, while the preprocessor points at line 8 instead. This bug in the preprocessor was presumably caused by a multiline macro.

Release of a fixed version

As of this writing, the problem exists in the recent Visual Studio versions up to 16.4.3 (released on January 14, 2020). We submitted a bug report "Invalid #line directive after preprocessing (VS2019, v142)" and got a curious reply four days later:

This issue has been identified as a duplicate of an earlier reported feedback linked here: https://developercommunity.visualstudio.com/content/problem/738407/preprocessor-generates-incorrect-line-information.html. If you reported or voted here, your vote has been applied to the original issue. Everyone else can add their vote directly at the above linked feedback. Voting helps increase the priority of the issue by consolidating customer impact under one feedback. Thank you!

It turns out this problem was already reported on September 18, 2019 – almost half a year ago: "Preprocessor generates incorrect line information". The user included similar test cases to reproduce the bug. According to him, it goes back to at least Visual Studio 2019 16.2.

I'm not sure if this is a coincidence or not, but a few days after our activity, the bug was assigned the status Fixed - Pending Release.

On May 19, 2020, a fixed Visual Studio 2019 16.6.0 was released. It is recommended that all PVS-Studio users update to that version.

Popular related articles
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept