Dead Code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jaysome

    #1

    Dead Code?

    /*
    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_FIL ENAME, "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_FIL ENAME, "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:

    */
  • cipher

    #2
    Re: Dead Code?

    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

    Comment

    • Richard Heathfield

      #3
      Re: Dead Code?

      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

      Comment

      • Richard Heathfield

        #4
        Re: Dead Code?

        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

        Comment

        • Alan Curry

          #5
          Re: Dead Code?

          In article <tkrog35qppudp9 lqhfrugj8n6bd1a bjg7u@4ax.com>,
          jaysome <jaysome@hotmai l.comwrote:
          >/*
          >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
          pacman@world.st d.com

          Comment

          Working...