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

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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.

February 31

February 31

Jan. 5, 2018

I'm currently studying a report by PVS-Studio analyzer with the results of a fresh check of the Chromium project and the libraries it employs. Based on these results, I'm going to write a series of articles discussing some types of bugs and ways to avoid them. But there was one bug that I liked so much that I decided to tell you about it right off in this small blog post.


Our team already posted 5 articles (1, 2, 3, 4, 5) about the search of bugs in open-source project Chromium, and it seems there'll be a few more soon.

I'm currently studying a fresh report by PVS-Studio and just noting down the warnings to use later when writing the posts, which is the next step. I prefer to look through the report first and only then decide which of the defects and in what form to describe. But one bug was especially nice, so I decided to tell you about it right off.

This error is found in the Protocol Buffers (protobuf) library used by Chromium. Protocol Buffers is a protocol for serializing structured data developed by Google as a smaller and faster binary alternative to the XML text format.

If I had come across that bug a couple of months ago, I'd have paid it no attention. It's just an ordinary bug like many others. But when I saw it the other day, I immediately recalled the recent epic failure of cash registers in Russia. On December 20, the major retailers and gas-station chains all over Russia were faced with a glitch in the new model of cash registers. The first to suffer was Vladivostok; then it spread across the country as the new day was breaking and affected Novosibirsk, Barnaul, Krasnoyarsk, Kemerovo, and other large cities.

The bug in the cash registers and the bug in Protocol Buffers are different bugs that are not linked in any way. But I wanted to show you how errors of this type occur. After all, defects often stem from banal typos rather than tricky algorithms. I don't know what exactly was wrong with the code of the cash registers, but I do know how a silly typo breaks the ValidateDateTime function used for date validation in the Protocol Buffers library. Let's look into the code of the function.

static const int kDaysInMonth[13] = {
  0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

bool ValidateDateTime(const DateTime& time) {
  if (time.year < 1 || time.year > 9999 ||
      time.month < 1 || time.month > 12 || < 1 || > 31 ||
      time.hour < 0 || time.hour > 23 ||
      time.minute < 0 || time.minute > 59 ||
      time.second < 0 || time.second > 59) {
    return false;
  if (time.month == 2 && IsLeapYear(time.year)) {
    return time.month <= kDaysInMonth[time.month] + 1;
  } else {
    return time.month <= kDaysInMonth[time.month];

ValidateDateTime receives a date as an argument and must find out if that date is correct or not. Basic checks are performed first and involve checking that the date components are within the corresponding ranges: [1..12] for the month, [1..31] for the day, [0..59] for the minutes, and so on. The code is clear enough and we don't need to go into it.

This is followed by a more complicated check where the function checks if the given day exists in the given month. For example, December consists of 31 days, while there is no such date as November 31-st as there are only 30 days in that month.

To check the validity of the day value without using multiple if statements or a long switch, the programmer used a helper array called kDaysInMonth, which stores the number of days in every month. The function refers to this array and looks up the maximum number of days in the given month and checks the given day value against it.

It also takes into account whether the year is a leap year, in which case February has one extra day.

So, the function is all neat and nice. Yet faulty.

Its code contains a typo that makes the check of the day value incorrect. If you look closely, you'll notice that what is compared with the maximum number of days in the month is the month value, not day value, from the date passed to the function.

Here it is again:

if (time.month == 2 && IsLeapYear(time.year)) {
  return time.month <= kDaysInMonth[time.month] + 1;
} else {
  return time.month <= kDaysInMonth[time.month];

What should be used in the comparison "time.month <=" is the structure member day, not month. It means the correct version should look like this:

if (time.month == 2 && IsLeapYear(time.year)) {
  return <= kDaysInMonth[time.month] + 1;
} else {
  return <= kDaysInMonth[time.month];

The month value (1 through 12) is, of course, always less than the number of days in any month.

Because of that, such dates as February 31 or November 31 will be treated as correct.

Nice bug, isn't it? It could make processing of incorrect dates possible, which in theory could be used for hacker attacks. Well, perhaps I'm exaggerating a bit, but that's what vulnerabilities usually look like: some input data goes unchecked and someone happens to be smart enough to exploit that.

This error (two errors, to be precise) is detected by the following PVS-Studio diagnostics:

  • V547 / CWE-571 Expression 'time.month <= kDaysInMonth[time.month] + 1' is always true. 83
  • V547 / CWE-571 Expression 'time.month <= kDaysInMonth[time.month]' is always true. 85

As you can see, PVS-Studio now identifies programming issues according to Common Weakness Enumeration (CWE) as well.

Another thing I'd like to point out is that PVS-Studio is learning to analyze code at an even deeper level. The V547 diagnostic as such is an old-timer (it dates back to 2010), but it wouldn't have found this bug, say, a year ago. Now the analyzer can look into the array and notice that values within the range [28..31] are extracted. Besides, it understands that the value 0 shouldn't be taken into account in the array since the range of time.month is [1..12]. If the month value were 100, for example, the function would return - and the analyzer can figure that.

As a result, it sees that the following comparisons of ranges take place:

  • [2.. 2] <= [28..31]
  • [1..12] <= [29..32]

The conditions are therefore always true, and this is what the analyzer warns us about. That's how deep we can reach now. So, we not only add new diagnostics to PVS-Studio but also improve the Data-Flow analysis, which boosts the quality of the existing diagnostics.

Why is the range [2, 2] presented only with the number 2? The fact of the matter is that the specifying condition time.month == 2 is taken into account.

Now, the following question arises: "How can we improve our style to ward off errors like that?"

I have no answer to that. The function we've discussed is simple and well-written. It's just that human is prone to errors, and it's natural for us to make typos like that every now and then. Even experienced programmers aren't safe from that.

The only piece of advice I could give is to be especially careful when writing unit tests and use professional static code analyzers such as PVS-Studio.

Thank you for reading. And I'll go on with the report.


The bug was fixed and new tests were added just one hour after we posted the article.

Popular related articles
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: 07.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 Ultimate Question of Programming, Refactoring, and Everything

Date: 04.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…
PVS-Studio for Java

Date: 01.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 Evil within the Comparison Functions

Date: 05.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: 03.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…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: 11.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…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: 10.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 ROI

Date: 01.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…
Static analysis as part of the development process in Unreal Engine

Date: 06.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…
Appreciate Static Code Analysis!

Date: 10.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…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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 →