Webinar: Parsing C++ - 10.10
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.
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:
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:
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".
From the lyrics of this song we can assume that Freddy's coming for Blender.
How to avoid such 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:
0