Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Dead Code? (http://www.velocityreviews.com/forums/t543187-dead-code.html)

jaysome 10-10-2007 06:30 AM

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_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/
*/

cipher 10-10-2007 06:52 AM

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


Richard Heathfield 10-10-2007 08:30 AM

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

Richard Heathfield 10-10-2007 08:32 AM

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

Alan Curry 10-10-2007 08:36 AM

Re: Dead Code?
 
In article <tkrog35qppudp9lqhfrugj8n6bd1abjg7u@4ax.com>,
jaysome <jaysome@hotmail.com> 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
pacman@world.std.com


All times are GMT. The time now is 12:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.