Dead Code?

J

jaysome

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

cipher

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
 
R

Richard Heathfield

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>
 
R

Richard Heathfield

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.
 
A

Alan Curry

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top