RFA: [ser-unix.c] Fix handling of baud rates

Fernando Nasser
2001-04-25T15:39:00Z

If the user requests an invalid baud rate (with -b or set remotebaud),
GDB will try to set the serial port speed to -1 and eventually dump
core.

The following patch will cause GDB to use only legal values for the
serial line speed. If the speed requested is not one of the valid
speeds, the best speed that is less than the value requested will be
used. However, if the requested speed is less than 50, 50 bauds will be
used.

OK to commit?

--
Fernando Nasser
Red Hat Canada Ltd. E-Mail: fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario M4P 2C9
Index: ser-unix.c
===================================================================
RCS file: /cvs/src/src/gdb/ser-unix.c,v
retrieving revision 1.12
diff -c -p -r1.12 ser-unix.c
*** ser-unix.c 2001/03/06 08:21:16 1.12
--- ser-unix.c 2001/04/25 21:54:59
*************** rate_to_code (int rate)
*** 741,750 ****
int i;

for (i = 0; baudtab[i].rate != -1; i++)
! if (rate == baudtab[i].rate)
! return baudtab[i].code;
!
! return -1;
}

static int
--- 741,768 ----
int i;

for (i = 0; baudtab[i].rate != -1; i++)
! {
! /* test for perfect macth. */
! if (rate == baudtab[i].rate)
! return baudtab[i].code;
! else
! {
! /* check if it is in between valid values. */
! if (rate < baudtab[i].rate)
! {
! /* set to the valid speed immediately below
! or to B50 if the value requested was less
! than 50 bauds. */
! if (i)
! return baudtab[i - 1].code;
! else
! return baudtab[0].code;
! }
! }
! }
!
! /* If the requested speed was too large, set to the best we can do. */
! return baudtab[i - 1].code;
}

static int
From jtc@redback.com Wed Apr 25 16:44:00 2001
From: jtc@redback.com (J.T. Conklin)
To: Fernando Nasser <fnasser@redhat.com>
Cc: gdb-patches@sources.redhat.com
Subject: Re: RFA: [ser-unix.c] Fix handling of baud rates
Date: Wed, 25 Apr 2001 16:44:00 -0000
Message-id: <5mu23c4kao.fsf@jtc.redback.com>
References: <3AE751A5.AE7633E4@redhat.com>
X-SW-Source: 2001-04/msg00242.html
Content-length: 1261

>>>>> "Fernando" == Fernando Nasser <fnasser@redhat.com> writes:
Fernando> If the user requests an invalid baud rate (with -b or set
Fernando> remotebaud), GDB will try to set the serial port speed to -1
Fernando> and eventually dump core.

Fernando> The following patch will cause GDB to use only legal values
Fernando> for the serial line speed. If the speed requested is not
Fernando> one of the valid speeds, the best speed that is less than
Fernando> the value requested will be used. However, if the requested
Fernando> speed is less than 50, 50 bauds will be used.

Fernando> OK to commit?

I'm not sure I like the behavior of selecting a rate different than
what the user selected is a good idea.

It appears that the higher layers of the code handle the case when
SERIAL_SETBAUDRATE() returns non-zero, so all that needs to be done is
to return -1 in hardwire_setbaudrate() when rate_to_code returns -1
instead of trying to using that value with cfset{i,o}speed, or masking
it in to termio.c_flag, or setting sgttyb.sg_{i,o}speed. The only
additional thing we might want to do is to set errno to an appropriate
value so the perror_with_name() calls in the upper layers output a
reasonable message.

--jtc

--
J.T. Conklin
RedBack Networks