C++
Raiting:
42

WTF?


I'm currently experiencing a strong cognitive dissonance, and it won't let me go. You see, I visit various programmers' forums and see topics where people discuss noble ideas about how to write super-reliable classes; somebody tells he has his project built with the switches -Wall -Wextra -pedantic -Weffc++, and so on. But, God, where are all these scientific and technological achievements? Why do I come across most silly mistakes again and again? Perhaps something is wrong with me?



Such wonderful exceptions to the common state of things only enhance my cognitive dissonance. Now try to imagine what I feel. Say, I take a complex package of computer simulation software and see not a single bug there. I'm glad about the high-quality code and just a bit sad because the package's author will never buy PVS-Studio. Well, never mind. Then I take the project OpenCOLLADA and check it. WTF? I have no other words to express my feelings. What do you think about constructors like the ones below?

struct short2
{
short values[2];
short2(short s1, short s2)
{
values[0] = s1;
values[2] = s2;
}
....
};

struct double2
{
double values[2];
double2( double d1, double d2)
{
values[0]=d1;
values[0]=d2;
}
....
}

The programmer missed the array in the first constructor and forgot to change the index in the copied-and-pasted line in the second constructor.

I'm sorry for posting this picture, guys, but it shows quite exactly what I feel.

Other constructors are also a source of much wonder and fun. For example, these ones are very nice:

struct ParserString : public UnionString
{
ParserString()
{
UnionString::str = 0;
UnionString::length = 0;
}

ParserString(const int& val)
{
ParserString();
}
};

Instead of calling another constructor, a temporary object is created and gets destroyed at once, whereas the class members are left uninitialized. More about it.

Oh my God, where are all those people who with so much zeal write about C++11, lambdas, Boost.Asio, shared_ptr, constexpr, LINQ? How could the following code have been written:

struct ObjectGroups{
componentList objectGrpCompList;
int objectGroupId;
short objectGrpColor;
void write(FILE* file) const;
}* objectGroups;

void write(FILE* file) const
{
size_t size = sizeof(objectGroups)/sizeof(ObjectGroups);
for(size_t i=0; i<size; ++i)
{
objectGroups[i].write(file);
if(i+1<size) fprintf(file," ");
}
}

The programmer just divided the pointer size by the structure's size and got 0. What the hell did he mean to do? WTF?

Well, even when you can guess what and how the programmer wanted to write into a file, you feel no better.

void write(FILE* file) const
{
fprintf(file,"%i %i %i %i ",
sDivisionCount, tDivisionCount, uDivisionCount, pointCount);
size_t size = pointCount*3;
for(size_t i; i<size; ++i)
{
fprintf(file, "%f", points[i]);
if(i+1<size) fprintf(file, " ");
}
}

If you don't see the bug, I'll prompt. The variable 'i' is not initialized: for(size_t i; i<size; ++i).


Sorry for sharing all this with you - it just makes me feel better somehow. And I also use this opportunity to remind you that all these bugs were found by the PVS-Studio static code analyzer. The locations of the above mentioned and some other notable bugs are listed in this text file. As usual, if somebody wants to check this project more thoroughly, ask me for a key.

Good luck and may your code stay bugless!
Andrey2008 3 april 2014, 10:29
Vote for this post
Bring it to the Main Page
 

Comments

Leave a Reply

B
I
U
S
Help
Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute