Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

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

close form
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

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

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: Parsing C++ - 10.10

>
>
>
0,1,2, Freddy came for Blender

0,1,2, Freddy came for Blender

Oct 26 2022
Author:

This article could have been named "How PVS-Studio prevents rash code changes, example N7". However, naming articles like that becomes boring. So today Freddy Krueger's name is in the title, and you'll find out why.

1004_Freddy_And_Blender/image1.png

We're all people, we all make mistakes. Especially when we are in a hurry. Programming is no exception for that. There's suddenly a light-bulb moment, and you rush to write the code while the thought is still clear in your head. Developers do know how hard it is to collect all the puzzle pieces in your head after getting distracted. I have a pic for that:

1004_Freddy_And_Blender/image3.png

Sometimes developers are in a hurry when writing code. Besides, they also may miss something — especially when the code seems simple. I think that's how the error below occurred. And it wasn't noticed during code review — the code is so simple, there's nowhere to go wrong.

One of the Blender developers made a commit shown in the first picture of this article, and PVS-Studio found an error there. So, I decided to write another note about how the static analyzer helps fix errors in code. Providing, of course, if it's used :). I'm not writing about all errors, only about interesting ones.

Incorrect code:

static void initSnapSpatial(TransInfo *t, float r_snap[3],
                            float *r_snap_precision)
{
  /* Default values. */
  r_snap[0] = r_snap[1] = 1.0f;
  r_snap[1] = 0.0f;
  *r_snap_precision = 0.1f;
  ....
}

The PVS-Studio analyzer issued a warning: V519. The 'r_snap[1]' variable is assigned values twice successively. Perhaps this is a mistake. transform.c. Check lines: 1727, 1728.

The developer decided to fill an array with default values, but was in a rush. As a result of a typo, there are two errors at once:

  • The second element of an array (r_snap[1]) is initialized by an incorrect default value. The developer had to write 1.0f there, but wrote 0.0f;
  • The third array element (r_snap[2]) remains uninitialized by default. Then, another value may be written to this element of the array. If the value is not written, the element remains uninitialized, and that leads to undefined behavior.

So, what does Freddy have to do with it? The answer is simple: 0,1,2 attract many errors. I wrote an article about that — "Zero, one, two, Freddy's coming for you".

1004_Freddy_And_Blender/image4.png

From the lyrics of this song we can assume that Freddy's coming for Blender.

How to avoid such errors?

  • Don't rush when writing code.
  • Thoroughly review the code — even if it seems simple/uninteresting.
  • Use PVS-Studio to search for errors.

You don't need to choose only one point. All three are important! Use them all together. Thank you and I wish you bugless code.

Previous articles:



Comments (0)

Next comments next comments
close comment form