>
>
>
WTF?

Andrey Karpov
Articles: 643

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?

Well, there are actually wonderful projects too. Such is, for instance, the library ALGLIB. Source code of this library is very interesting. Developers write code in Pascal and then code translating into C++ and C# automatically. Besides a number of other advantages, this approach allows them to catch quite many diverse bugs, since one and the same program is built by compilers supporting different languages. But this is quite another story, and perhaps we will tell it someday in a joint article by the library's author and 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!