Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dead Code?

Reply
Thread Tools

Dead Code?

 
 
jaysome
Guest
Posts: n/a
 
      10-10-2007
/*
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/
*/
 
Reply With Quote
 
 
 
 
cipher
Guest
Posts: n/a
 
      10-10-2007
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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      10-10-2007
In article <(E-Mail Removed)>,
jaysome <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dead TCP/IP Stack = DEAD VISTA !! Skybuck Flying Windows 64bit 15 09-23-2007 07:54 PM
AdBlock Dead Firefox 3 12-09-2005 11:54 PM
Dead Firefox windows Doug Cunningly Firefox 3 07-22-2005 08:32 AM
Firefox cache glitch - the dead rise again! Jim Firefox 1 03-22-2005 09:48 PM
Dead... the damn thing's dead... David Dean Computer Support 9 01-30-2004 02:54 AM



Advertisments