Bug Buster 003: Can You Bust the Bug?

Bug Buster 003: Can You Bust the Bug?

How good are you at finding bugs in code? If our first two didn't stump you, then see how quickly you can find the issue in Bug Buster 3!

Bug Buster

This is our third bug for you to track down and eradicate. You can find the first bug here! How good are you at finding bugs in code? The following is a code snippet compiles fine. Unfortunately, it contains an error. Take a look at the code listing and see if you can find the error. To make it easier, several options are listed. One of the options is correct, while the others are not.

Can you find the error without looking at the choices?

Bug Buster #3

Here is the Bug Buster:

The code:

#include <algorithm>
#include <vector>
#include <array>
#include <iostream>

int main()
   std::array<int, 10> input =
   { 1, 2, 3, 3, 5,
     6, 7, 8, 9, 10 }; // 'random' input
   // copy odd numbers in new vector
   std::vector<int> odds(5); // new container for odd numbers
   auto it = std::copy_if(input.begin(),
      /*copy odd nbrs */   [](int i) { return i % 2; });
   // shrink container:
   odds.resize(std::distance(odds.begin(), it));
   for (const auto& x : odds)
      std::cout << x << std::endl;
   return 0;

Your choices:

The input array will throw an exception due to the duplicate data being added.
You can't use a mod operator (%) in the embedded return statement.
This code will throw exception on the odds vector when the resize is attempted.
The odds array will throw an exception when the numbers are being added.
The program runs fine, but the odds array will end up with even numbers.
Nothing is wrong. The code works fine.

How quickly can you find the issue? Feel free to comment on how quick you were! We will be posting additional snippets over the coming weeks with additional bugs for you to bust. You can click to the next page to find the solution.

There are six options to pick from regarding this listing. Because of the way this listing was set to be self contained, if you compile and run it, you will actually quickly get the answer.  If this were a more "real life" scenario, the answer would have been much harder.

In the real world, the input array would not have the ten hard-coded values included in an array, but rather would get those values from another source or from user input. As such, you could have ran this listing many, many times and never ran into the issue. In fact, if this listing's data for the input array were corrected to be 1 through 10 instead of having the double 3s, then the listing would run just fine.

Although we are taking about the input values, to be clear, the issue is not with the input array, but rather with the odds vector where the numbers that are odd are stored.

Simply put, the odds vector is not big enough to hold the number of items being added to it. It was created too small and needs to be resized before adding a sixth value. When the sixth value is added, an exception is thrown. This means the correct answer for the bug buster was the fourth choice: The odds array will throw an exception when the numbers are being added.

While this should have been extremely easy to figure out in this short listing, this problem was based on a real world bug that someone recently ran into. As mentioned, when you make the input dynamic, this type of bug can be extremely tricky to find if your test data never pushes the hard coded boundaries.

One of the best ways to insure solid code is by using a code profiling tool like Rogue Wave's Klocwork, which can find issues quickly, thus possibly saving you embarrassment and time later.

# # #

This article was originally published on Tuesday Sep 22nd 2015
Mobile Site | Full Site