[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gnuchessx bug

On Wed, Jul 07, 1999 at 10:37:42AM +0100, Russell Coker wrote:
> Running gdb on a core file after sending a SEGV to it when it's in this state I
> find that it's at line 1102 of dspcom.c, the gets() in the following fragment:
>       s[0] = sx[0] = '\0';
>       while (!sx[0])
>         (void) gets (sx);
> Does anyone know what they are trying to do here?  It seems to be while STDIN
> is closed do infinite loop...

I'd guess the intent is to read a line, but ignore it if it's blank.

A better replacement is something more like:

	int stringlen;
	do {
		if (!fgets(sx, sizeof(sx), stdin)) {
			if (errno != 0) {
			} else {
		stringlen = strlen(sx);
		if (stringlen > 1 && sx[stringlen-1] == '\n')
			sx[--stringlen] = '\0';
	} while (i == 0);

ie, call fgets. if fgets failed, either you're at eof, or there was
a real error. otherwise, it succeeded, so get rid of the trailing \n,
if there was one. keep doing this until you get some actual input.

You can get rid of any of the error checking above that you don't like
to make it more concise as appropriate.

gets() is a buffer overrun waiting to happen, btw, and should never be
used. fgets(buf,length_of_buf,stdin); is generally a drop in replacement.


(You could also do "while(!sx[0] && !feof(stdin)) gets(sx);" but that
would still be icky and buggy. while(!sx[0] && gets(sx)); would probably
work too.)

Anthony Towns <aj@humbug.org.au> <http://azure.humbug.org.au/~aj/>
I don't speak for anyone save myself. PGP encrypted mail preferred.

       ``There's nothing worse than people with a clue.
             They're always disagreeing with you.'' 
                                 -- Andrew Over

Attachment: pgp7pBiZT7TqJ.pgp
Description: PGP signature

Reply to: