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.

>
>
>
What comments hide

What comments hide

Sep 20 2012

Much is said about good and harm of comments in program code and a single opinion hasn't been worked out yet. However, we've decided to take a look at comments from a different viewpoint. Can comments serve as an indication of hidden errors for a programmer studying the code?

When investigating different projects concerning errors, we noticed that programmers sometimes see defects but cannot find out all their causes. Suspicion falls on the compiler: my colleague has recently discussed this effect in the article "The compiler is to blame for everything". As a result, programmers make crutches in the code and leave some comments. These are often obscene.

We decided it was an interesting subject to investigate. Manual review of files or usual word-by-word search is long and tiresome. That's why we wrote a utility that searches for suspicious comments in ".c" and ".cpp" files relying on its dictionary of "suspicious words". This dictionary includes, for example, such words as fuck, bug, stupid, compiler.

We've got a lot of lines with comments of that kind. Picking out fragments really worth considering was a hard and tiresome task. We have found little of interest - much less than we expected.

The task of our search was to find new patterns of possible mistakes made by programmers. Unfortunately, all the found defects either cannot be diagnosed by static code analysis at all or are already successfully detectable by PVS-Studio.

But a bad result is a result too. Most likely we will come to the conclusion that the method of searching for strange comments is a dead-end. It's too labor-intensive while allowing you to catch too few bugs.

But since the investigation has been carried out, we've decided to show you a couple of examples.

For example, consider this code:

// Search for EOH (CRLFCRLF)
const char* pc = m_pStrBuffer;
int iMaxOff = m_iStrBuffSize - sizeof(DWORD);
for (int i = 0; i <= iMaxOff; i++) {
  if (*(DWORD*)(pc++) == 0x0A0D0A0D) {
    // VC-BUG?: '\r\n\r\n' results in 0x0A0D0A0D too,
    //although it should not!
    bFoundEOH = true;
    break;
  }
}

As you can see from the comment "// Search for EOH (CRLFCRLF)", the programmer wanted to find the sequence of bytes 0D,0A,0D,0A (CR == 0x0D, LF == 0x0A). Since the bytes are arranged in a reverse order, the search constant equals 0x0A0D0A0D.

This program doesn't seem to be quite successful at handling a different sequence of carriage return and line folding. This is the cause of the author's misunderstanding, which is indicated by the comment: " // VC-BUG?: '\r\n\r\n' results in 0x0A0D0A0D too, although it should not!". So why does the algorithm find not only the {0D,0A,0D,0A} sequence, but the {0A,0D,0A,0D} sequence too?

Everything's simple. The search algorithm is moving through the array byte-by-byte. That's why if it comes across a long sequence like {0A,0D,0A,0D,0A,0D,0A,...}, it will skip the first symbol 0A and move on to find quite different things than the programmer wanted.

Unfortunately, such defects are impossible to catch by static analysis.

Here is one more example of strange code:

TCHAR szCommand[_MAX_PATH * 2];
LPCTSTR lpsz = (LPCTSTR)GlobalLock(hData);
int commandLength = lstrlen(lpsz);
if (commandLength >= _countof(szCommand))
{
  // The command would be truncated.
  //This could be a security problem
  TRACE(_T("Warning: ........\n"));
  return 0;
}
// !!! MFC Bug Fix
_tcsncpy(szCommand, lpsz, _countof(szCommand) - 1);
szCommand[_countof(szCommand) - 1] = '\0';
// !!!

In this case "MFC Bug Fix" is absolutely untrue because there is no error in MFC here. The code cannot cause errors being written in this form, but maybe its earlier version contained only this line: '_tcsncpy(szCommand, lpsz, _countof(szCommand) - 1);'. In this case the error did exist. However, you can implement correct string copying in a shorter way:

_tcsncpy(szCommand, lpsz, _countof(szCommand));

Functions like 'strncpy' add the terminal null at the end of the string automatically if the source string is not longer than the value specified in the counter. This is exactly so in our case, as there is a check for this written above. Cases of incorrect string copying are well detectable by PVS-Studio, so we haven't learned anything new.

Conclusion

We haven't managed to find any new error patterns for further including them into the database of errors detected by our static analyzer. However, this is a good experience in investigating alternative methods of software defect detection. We will for some time continue studying comments in new projects we'll get for analysis. We also plan to make some improvements to the search utility:

  • implement a simple syntactic analysis to decrease detections of "uninteresting" lines;
  • extend the dictionary with new expressions.

Perhaps this program can be useful when you "inherit" a large project with a long code history and would like to see what your predecessors didn't like there.

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…
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…
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…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
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…
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 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…
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…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
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…

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