serial: protect access to serial rx buffer
authorPatrick Delaunay <patrick.delaunay@st.com>
Fri, 3 Aug 2018 11:38:43 +0000 (13:38 +0200)
committerTom Rini <trini@konsulko.com>
Tue, 11 Sep 2018 00:20:34 +0000 (20:20 -0400)
Add test to avoid access to rx buffer when this buffer is empty.
In this case directly call getc() function to avoid issue when tstc()
is not called.

Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
drivers/serial/serial-uclass.c

index 321d23ee93bf5c8d909e2309742eac28eb776671..4121a37aa80c661bbdcf46da8794e376206843f0 100644 (file)
@@ -228,6 +228,9 @@ static int _serial_getc(struct udevice *dev)
        struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
        char val;
 
+       if (upriv->rd_ptr == upriv->wr_ptr)
+               return __serial_getc(dev);
+
        val = upriv->buf[upriv->rd_ptr++];
        upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;