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

Fernando Nasser
2001-05-11T11:31:00Z

Well, I have already addressed JT and Eli's concerns. Eli has reviewed
this last version and said it is OK.

I could not find a specific maintainer for ser-unix.c to get a formal
approval. This has been around for a while and no one besides JT and Eli
had any objection, so I guess it is time to check it in. We can't leave
GDB dumping core for such a simple user input error.

I will commit the change.

Fernando

Fernando Nasser wrote:
>
> This patch implements what was discussed with JT and Eli. An error is
> now being issued when the set baud rate is invalid. Here is the output:
>
> ./gdb -nw -b 30
>
> (gdb) target remote /dev/ttyS0
> warning: Invalid baud rate 30. Minimum value is 50.
> /dev/ttyS0: Invalid argument.
> (gdb)
>
> ./gdb -nw -b 200000
>
> (gdb) target remote /dev/ttyS0
> warning: Invalid baud rate 200000. Closest values are 115200 and
> 230400.
> /dev/ttyS0: Invalid argument.
> (gdb)
>
> ./gdb -nw -b 500000
>
> (gdb) target remote /dev/ttyS0
> warning: Invalid baud rate 500000. Maximum value is 460800.
> /dev/ttyS0: Invalid argument.
> (gdb)
>
> The patch is attached. With it, I will fix the calls to
> SERIAL_SETBAUDRATE in the few targets that "forgot" to check for the
> return code:
> nindy-share/nindy.c
> remote-e7000.c
> remote-st.c
>
> OK to commit now?
>
> ChangeLog:
>
> * ser-unix.c (rate_to_code): Issue warning if baud rate is invalid.
> (hardwire_setbaudrate): Set errno to EINVAl and return with error
> if the conversion of the baud rate to code fails.
>
> --
> 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/05/10 18:54:36
> *************** rate_to_code (int rate)
> *** 741,749 ****
> int i;
>
> for (i = 0; baudtab[i].rate != -1; i++)
> ! if (rate == baudtab[i].rate)
> ! return baudtab[i].code;
> !
> return -1;
> }
>
> --- 741,773 ----
> 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)
> ! {
> ! if (i)
> ! {
> ! warning ("Invalid baud rate %d. Closest values are %d and %d.",
> ! rate, baudtab[i - 1].rate, baudtab[i].rate);
> ! }
> ! else
> ! {
> ! warning ("Invalid baud rate %d. Minimum value is %d.",
> ! rate, baudtab[0].rate);
> ! }
> ! return -1;
> ! }
> ! }
> ! }
> !
> ! /* The requested speed was too large. */
> ! warning ("Invalid baud rate %d. Maximum value is %d.",
> ! rate, baudtab[i - 1].rate);
> return -1;
> }
>
> *************** static int
> *** 751,763 ****
> hardwire_setbaudrate (serial_t scb, int rate)
> {
> struct hardwire_ttystate state;
>
> if (get_tty_state (scb, &state))
> return -1;
>
> #ifdef HAVE_TERMIOS
> ! cfsetospeed (&state.termios, rate_to_code (rate));
> ! cfsetispeed (&state.termios, rate_to_code (rate));
> #endif
>
> #ifdef HAVE_TERMIO
> --- 775,796 ----
> hardwire_setbaudrate (serial_t scb, int rate)
> {
> struct hardwire_ttystate state;
> + int baud_code = rate_to_code (rate);
> +
> + if (baud_code < 0)
> + {
> + /* The baud rate was not valid.
> + A warning has already been issued. */
> + errno = EINVAL;
> + return -1;
> + }
>
> if (get_tty_state (scb, &state))
> return -1;
>
> #ifdef HAVE_TERMIOS
> ! cfsetospeed (&state.termios, baud_code);
> ! cfsetispeed (&state.termios, baud_code);
> #endif
>
> #ifdef HAVE_TERMIO
> *************** hardwire_setbaudrate (serial_t scb, int
> *** 766,777 ****
> #endif
>
> state.termio.c_cflag &= ~(CBAUD | CIBAUD);
> ! state.termio.c_cflag |= rate_to_code (rate);
> #endif
>
> #ifdef HAVE_SGTTY
> ! state.sgttyb.sg_ispeed = rate_to_code (rate);
> ! state.sgttyb.sg_ospeed = rate_to_code (rate);
> #endif
>
> return set_tty_state (scb, &state);
> --- 799,810 ----
> #endif
>
> state.termio.c_cflag &= ~(CBAUD | CIBAUD);
> ! state.termio.c_cflag |= baud_code;
> #endif
>
> #ifdef HAVE_SGTTY
> ! state.sgttyb.sg_ispeed = baud_code;
> ! state.sgttyb.sg_ospeed = baud_code;
> #endif
>
> return set_tty_state (scb, &state);