Dead Code?

Discussion in 'C Programming' started by jaysome, Oct 10, 2007.

  1. jaysome

    jaysome Guest

    /*
    Does main1() have dead code that can never achieve 100% decision
    coverage? And is main2() a valid way of fixing it so that there is no
    dead code and the assert() never fires off and 100% decision coverage
    can be achieved?

    My answers are YES and YES. What are yours?
    */

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

    #define INPUT_FILENAME "foo.dat"

    static int main1(void);
    static int main2(void);

    static int main1(void)
    {
    FILE *fp;
    char line[132];

    fp = fopen(INPUT_FILENAME, "r");
    if ( !fp )
    {
    fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
    return EXIT_FAILURE;
    }

    if ( fgets(line, sizeof line, fp) )
    {
    size_t length = strlen(line);
    if ( length > 0 )
    {
    printf("length == %lu\n", (unsigned long)length);
    }
    else
    {
    /* can this line ever be reached?*/
    printf("length == 0\n");
    }
    }

    return EXIT_SUCCESS;
    }

    static int main2(void)
    {
    FILE *fp;
    char line[132];

    fp = fopen(INPUT_FILENAME, "r");
    if ( !fp )
    {
    fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
    return EXIT_FAILURE;
    }

    if ( fgets(line, sizeof line, fp) )
    {
    size_t length = strlen(line);
    assert(length > 0);/*always true*/
    printf("length == %lu\n", (unsigned long)length);
    }

    return EXIT_SUCCESS;
    }

    int main(void)
    {
    int status;
    status = main1();
    printf("main1() returned %d\n", status);
    status = main2();
    printf("main2() returned %d\n", status);
    return 0;
    }

    /*
    Regards
    --
    jay

    Using of this superb tool today prompted my question:
    http://www.bullseye.com/
    */
    jaysome, Oct 10, 2007
    #1
    1. Advertising

  2. jaysome

    cipher Guest

    Hi!

    I would sy *no* to both questions. As you can see in the man-page of
    "fgets", fgets reads characters from file until the max. allowed
    number is reached or a newline or end-of-file character is read. Plain
    text files can contain empty lines: open your favorite text editor,
    press "Return" a few times and save that file.
    According to man-page, fgets should only return a null pointer if eof
    is reached without reading a character from the file. I think, it will
    return a non-null pointer, if a newline is reading without transfering
    a character.

    Greetings,

    Markus
    cipher, Oct 10, 2007
    #2
    1. Advertising

  3. jaysome said:

    > /*
    > Does main1() have dead code that can never achieve 100% decision
    > coverage? And is main2() a valid way of fixing it so that there is no
    > dead code and the assert() never fires off and 100% decision coverage
    > can be achieved?
    >
    > My answers are YES and YES. What are yours?


    The fgets function will return a null pointer if no characters were read
    from the stream, so the answer to the first part is YES. The answer to the
    second part kind of depends. *As written*, your program attempts to open
    "foo.dat" twice without an intervening fclose. Whether this can succeed is
    implementation-defined, so you might well have dead code in main2 - but of
    course that wouldn't be an issue if it replaced main1 rather than
    following it, which seems to be your intent.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 10, 2007
    #3
  4. cipher said:

    > Hi!
    >
    > I would sy *no* to both questions. As you can see in the man-page of
    > "fgets", fgets reads characters from file until the max. allowed
    > number is reached or a newline or end-of-file character is read. Plain
    > text files can contain empty lines: open your favorite text editor,
    > press "Return" a few times and save that file.


    Yes, do that. Then read that file using fgets, and see how long your empty
    lines are. Then you'll realise that you were mistaken.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 10, 2007
    #4
  5. jaysome

    Alan Curry Guest

    In article <>,
    jaysome <> wrote:
    >/*
    >Does main1() have dead code that can never achieve 100% decision
    >coverage? And is main2() a valid way of fixing it so that there is no
    >dead code and the assert() never fires off and 100% decision coverage
    >can be achieved?


    Maybe. On my system I can the assertion to fail by feeding it a file that
    starts with a '\0' byte. Given a proper text file, the assertion would never
    fail. But if a user can give your program the wrong kind of file, through
    accident or malice, you better be prepared for weird things like that.

    [snip]
    >
    > if ( fgets(line, sizeof line, fp) )
    > {
    > size_t length = strlen(line);
    > assert(length > 0);/*always true*/
    > printf("length == %lu\n", (unsigned long)length);
    > }


    --
    Alan Curry
    Alan Curry, Oct 10, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. secret

    finding dead code...

    secret, Oct 4, 2003, in forum: Java
    Replies:
    7
    Views:
    1,738
    evernat
    Sep 9, 2009
  2. Geronimo W. Christ Esq

    Removing dead code and unused functions

    Geronimo W. Christ Esq, Jun 19, 2005, in forum: C Programming
    Replies:
    39
    Views:
    2,242
    Dave Thompson
    Jul 4, 2005
  3. Dan Henry
    Replies:
    0
    Views:
    382
    Dan Henry
    Jun 21, 2005
  4. Greg
    Replies:
    10
    Views:
    1,509
    Dave Thompson
    Jul 4, 2005
  5. sanjay
    Replies:
    5
    Views:
    656
    James Kanze
    Aug 1, 2008
Loading...

Share This Page