Thursday 11 November 2010

Silly programming errors

Here's a list of silly programming mistakes I've made recently with the appropriate line highlighted. You can clearly see from the context given that it was not intended, but you must look carefully.

This one was caught by valgrind, about 6 frames deeper as being uninitialized:
struct CpfsTime ct[2];
timespec_cpfstime(&tv[0], &ct[0]);
timespec_cpfstime(&tv[0], &ct[0]);

This one typically gives a compiler warning. However this one I found after my filesystem started gaining free space when I copied to it.
if (g_cpfs->freeblks = -1) {
    g_cpfs->freeblks = bitmap_avail_raw();
}

This one was the hardest to find. I did not even know it was the cause of the problem. In Windows there is a special form of "stat" used in their equivalent to "readdir". The erroneous file sizes would only cause errors in applications that depended on the readdir() form, such as the builtin Windows explorer and image viewer.
split_to_DWORDs(cpfsStat.size, to.nFileIndexLow, to.nFileSizeHigh);
to.nNumberOfLinks = cpfsStat.nlink;
split_to_DWORDs(cpfsStat.ino, to.nFileIndexLow, to.nFileIndexHigh);

Are these the kind of "problems" with using C, and allegedly to a lesser degree (but still present) C++? The general opinion is that these are typing errors, but languages like Python would never pick them up, and in statically typed languages the use of primitive types would still prevent any checks by the compiler.

No comments: