scanf() acting strange
Paul Breed
Paul@rasdoc.com
Tue Jul 7 07:59:00 GMT 1998
I fixed this bug for the coldfire.
Since they use the same library source, you probably have the same bug.
The bug is in read.c,
The fix is to replace the code in the LIBGLOSS read.c with:
/*
* read -- read bytes from the serial port. Ignore fd, since
* we only have stdin.
*/
int read(int fd ,char * buf,int nbytes)
{
int i = 0;
for (i = 0; i < nbytes; i++) {
*(buf + i) = inbyte();
if ((*(buf + i) == '\r') || (*(buf + i) == '\n')) {
*(buf +i)='\n';
outbyte('\r');
outbyte('\n');
return i+1;
}
else
outbyte(*(buf+i));
}
return (i);
}
The origional code was:
/*
* read -- read bytes from the serial port. Ignore fd, since
* we only have stdin.
*/
int
_DEFUN (read, (fd, buf, nbytes),
int fd _AND
char *buf _AND
int nbytes)
{
int i = 0;
for (i = 0; i < nbytes; i++) {
*(buf + i) = inbyte();
if ((*(buf + i) == '\n') || (*(buf + i) == '\r')) {
(*(buf + i + 1)) = 0;
break;
}
}
return (i);
}
When it reads a \n or \r it breaks out of the loop.
i never gots incrmented.
So the number of chars read is reported as one too few.
The good old off by one bug ;-)
More information about the crossgcc
mailing list