Bug Buster 002: Can You Bust the Bug? | CodeGuru

Bug Buster 002: Can You Bust the Bug?

Sponsored by Rogue Wave, maker of Klocwork How good are you at finding errors in code? This is our second bug for you to track down and eradicate. 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 9, 2015
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Bug Buster

Sponsored by Rogue Wave, maker of Klocwork

How good are you at finding errors in code? This is our second bug for you to track down and eradicate. 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 problem. To make it easier, we’ve listed five options. One of those options is correct while the other four are not.

Can you find the error without looking at the choices?

Bug Buster #2

Here is the Bug Buster:

The code:

#include <stdio.h>
#include <stdlib.h>

/*======================================*/
/* Print "words" made of random letters */
/*======================================*/

int main()
{
    int len = 10;    /* length of each string */
    int amount = 5;  /* number of strings */
    int x, y;        /* simple counters */
    char * buffer;   /* buffer to store random string */

    /* create the random strings */
    for(x = 0; x < amount; x++)
    {
        /* allocate memory for string */
        buffer = (char*)malloc(len + 1);
        if (buffer == NULL) exit(1);

        /* build the random string */
        for (y = 0; y < len; y++)
            buffer[y] = rand() % 26 + 'a';
        buffer[len] = '\0';

        /* print the random string*/
        printf("Random string: %s\n", buffer);
    }

    return 0;
}
Advertisement

Your choices:

You can’t legally compare buffer to NULL without throwing an exception.
The buffer will never be allocated, so the program will always exist.
There is a buffer overflow for the buffer array.
The program has a memory leak.
Nothing is wrong. The code works fine.

How quickly can you find the issue? Feel free to comment on how quickly you found the issue! 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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.