[PATCH 04/20] libchip/serial/ns16550* and z8530*: Assert on baud number to avoid divide by 0

Joel Sherrill joel.sherrill at oarcorp.com
Tue Nov 25 23:02:32 UTC 2014


From: Josh Oguin <josh.oguin at oarcorp.com>

This was flagged by CodeSonar. It should be impossible to get an
incorrect baud number back but ensure this in debug mode. The _Assert()
keeps their scanner from evaluating for divide by 0 past this point.
---
 c/src/libchip/serial/ns16550-context.c | 4 ++++
 c/src/libchip/serial/ns16550.c         | 3 +++
 c/src/libchip/serial/z85c30.c          | 8 +++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/c/src/libchip/serial/ns16550-context.c b/c/src/libchip/serial/ns16550-context.c
index 00ad89c..087627a 100644
--- a/c/src/libchip/serial/ns16550-context.c
+++ b/c/src/libchip/serial/ns16550-context.c
@@ -592,9 +592,13 @@ static bool ns16550_set_attributes(
 
   /*
    *  Calculate the baud rate divisor
+   *
+   *  Assert ensures there is no division by 0.
    */
 
   baud_requested = rtems_termios_baud_to_number(t->c_cflag);
+  _Assert( baud_requested != 0 );
+
   ulBaudDivisor = NS16550_GetBaudDivisor(ctx, baud_requested);
 
   ucLineControl = 0;
diff --git a/c/src/libchip/serial/ns16550.c b/c/src/libchip/serial/ns16550.c
index 2c3bc94..801533b 100644
--- a/c/src/libchip/serial/ns16550.c
+++ b/c/src/libchip/serial/ns16550.c
@@ -532,9 +532,12 @@ int ns16550_set_attributes(
 
   /*
    *  Calculate the baud rate divisor
+   *
+   *  Assert ensures there is no division by 0.
    */
 
   baud_requested = rtems_termios_baud_to_number(t->c_cflag);
+  _Assert( baud_requested != 0 );
   ulBaudDivisor = NS16550_GetBaudDivisor(c, baud_requested);
 
   ucLineControl = 0;
diff --git a/c/src/libchip/serial/z85c30.c b/c/src/libchip/serial/z85c30.c
index 4b1b422..1f43271 100644
--- a/c/src/libchip/serial/z85c30.c
+++ b/c/src/libchip/serial/z85c30.c
@@ -437,6 +437,7 @@ Z85C30_STATIC int z85c30_set_attributes(
   uint32_t               wr4;
   uint32_t               wr5;
   int                    baud_requested;
+  uint32_t               baud_number;
   setRegister_f          setReg;
   rtems_interrupt_level  Irql;
 
@@ -445,15 +446,20 @@ Z85C30_STATIC int z85c30_set_attributes(
 
   /*
    *  Calculate the baud rate divisor
+   *
+   *  Assert ensures there is no division by 0.
    */
 
   baud_requested = t->c_cflag & CBAUD;
   if (!baud_requested)
     baud_requested = B9600;              /* default to 9600 baud */
 
+  baud_number = (uint32_t) rtems_termios_baud_to_number( baud_requested );
+  _Assert( baud_number != 0 );
+
   ulBaudDivisor = Z85C30_Baud(
     (uint32_t) Console_Port_Tbl[minor]->ulClock,
-    (uint32_t) rtems_termios_baud_to_number( baud_requested )
+    baud_number
   );
 
   wr3 = SCC_WR3_RX_EN;
-- 
1.9.3




More information about the devel mailing list