tty: can327, move overflow test inside can327_ldisc_rx()'s loop
authorJiri Slaby (SUSE) <jirislaby@kernel.org>
Thu, 10 Aug 2023 09:14:48 +0000 (11:14 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 11 Aug 2023 19:12:45 +0000 (21:12 +0200)
The 'count' is going to be unsigned and the 'count >= 0' test would be
always true then. Move the condition to the loop where this is easier to
check.

It looks as is easier to follow after all too.

Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
Cc: Max Staudt <max@enpas.org>
Cc: Wolfgang Grandegger <wg@grandegger.com>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: linux-can@vger.kernel.org
Link: https://lore.kernel.org/r/20230810091510.13006-15-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/net/can/can327.c

index 05e9c035e8f6f259d5aa0a1a330001f0737a74a8..4533dc00f215ae4681eaa7c82577419bbcca2ab3 100644 (file)
@@ -901,7 +901,13 @@ static void can327_ldisc_rx(struct tty_struct *tty, const unsigned char *cp,
         */
        first_new_char_idx = elm->rxfill;
 
-       while (count-- && elm->rxfill < CAN327_SIZE_RXBUF) {
+       while (count--) {
+               if (elm->rxfill >= CAN327_SIZE_RXBUF) {
+                       netdev_err(elm->dev,
+                                  "Receive buffer overflowed. Bad chip or wiring? count = %i",
+                                  count);
+                       goto uart_failure;
+               }
                if (fp && *fp++) {
                        netdev_err(elm->dev,
                                   "Error in received character stream. Check your wiring.");
@@ -930,13 +936,6 @@ static void can327_ldisc_rx(struct tty_struct *tty, const unsigned char *cp,
                cp++;
        }
 
-       if (count >= 0) {
-               netdev_err(elm->dev,
-                          "Receive buffer overflowed. Bad chip or wiring? count = %i",
-                          count);
-               goto uart_failure;
-       }
-
        can327_parse_rxbuf(elm, first_new_char_idx);
        spin_unlock_bh(&elm->lock);