tty: n_tty: invert the condition in copy_from_read_buf()
authorJiri Slaby (SUSE) <jirislaby@kernel.org>
Tue, 19 Sep 2023 08:51:45 +0000 (10:51 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 3 Oct 2023 12:31:15 +0000 (14:31 +0200)
Make "no numbers available" a fast quit from the function. And do the
heavy work outside the 'if'. This makes the code more understandable and
conforming to the common kernel coding style.

Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20230919085156.1578-5-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/n_tty.c

index 6a11291..922fb61 100644 (file)
@@ -1966,24 +1966,26 @@ static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
        size_t tail = MASK(ldata->read_tail);
 
        n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
-       if (n) {
-               u8 *from = read_buf_addr(ldata, tail);
-               memcpy(*kbp, from, n);
-               is_eof = n == 1 && *from == EOF_CHAR(tty);
-               tty_audit_add_data(tty, from, n);
-               zero_buffer(tty, from, n);
-               smp_store_release(&ldata->read_tail, ldata->read_tail + n);
-               /* Turn single EOF into zero-length read */
-               if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
-                   (head == ldata->read_tail))
-                       return false;
-               *kbp += n;
-               *nr -= n;
-
-               /* If we have more to copy, let the caller know */
-               return head != ldata->read_tail;
-       }
-       return false;
+       if (!n)
+               return false;
+
+       u8 *from = read_buf_addr(ldata, tail);
+       memcpy(*kbp, from, n);
+       is_eof = n == 1 && *from == EOF_CHAR(tty);
+       tty_audit_add_data(tty, from, n);
+       zero_buffer(tty, from, n);
+       smp_store_release(&ldata->read_tail, ldata->read_tail + n);
+
+       /* Turn single EOF into zero-length read */
+       if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
+           head == ldata->read_tail)
+               return false;
+
+       *kbp += n;
+       *nr -= n;
+
+       /* If we have more to copy, let the caller know */
+       return head != ldata->read_tail;
 }
 
 /**