From 82bb104f650011684515508b846a7a7f9c7b229d Mon Sep 17 00:00:00 2001 From: LaterComer <12015333+Later-Comer@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:29:53 +0800 Subject: [PATCH] fix(serial_v2): tx_fifo->rb.buffer_ptr not init when RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为什么提交这份PR (why to submit this PR) serial_v2驱动框架,rt_serial_tx_enable()函数,在 DMA阻塞发送(RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING) 的时候,使用rt_malloc分配了tx_fifo内存,但是并未对tx_fifo->rb.buffer_ptr指针进行初始化(rt_malloc不会自动初始化内存),因此可能导致buffer_ptr是一个随机指针。但是rt_serail_write()函数需要根据tx_fifo->rb.buffer_ptr是否为RT_NULL来判断是调用_serial_fifo_tx_blocking_nbuf()还是_serial_fifo_tx_blocking_buf()。 由于tx_fifo->rb.buffer_ptr可能是一个随机值(不一定为RT_NULL=0),导致RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING模式下调用了_serial_fifo_tx_blocking_buf(),但是rb没有真正开辟内存空间,导致内存异常 你的解决方案是什么 (what is your solution) rt_serial_tx_enable(),在DMA阻塞发送分支下,对tx_fifo->rb.buffer_ptr进行初始化,赋值为RT_NULL。 --- components/drivers/serial/serial_v2.c | 3 +++ components/drivers/usb/usbdevice/class/cdc_vcom.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/components/drivers/serial/serial_v2.c b/components/drivers/serial/serial_v2.c index 1d7a556bf2..5d6f2a8be3 100644 --- a/components/drivers/serial/serial_v2.c +++ b/components/drivers/serial/serial_v2.c @@ -684,6 +684,9 @@ static rt_err_t rt_serial_tx_enable(struct rt_device *dev, (sizeof(struct rt_serial_tx_fifo)); RT_ASSERT(tx_fifo != RT_NULL); + /* Init rb.buffer_ptr to RT_NULL, in rt_serial_write() need check it + * otherwise buffer_ptr maybe a random value, as rt_malloc not init memory */ + tx_fifo->rb.buffer_ptr = RT_NULL; serial->serial_tx = tx_fifo; #ifndef RT_USING_DEVICE_OPS diff --git a/components/drivers/usb/usbdevice/class/cdc_vcom.c b/components/drivers/usb/usbdevice/class/cdc_vcom.c index 0d65b0cdc5..b513394364 100644 --- a/components/drivers/usb/usbdevice/class/cdc_vcom.c +++ b/components/drivers/usb/usbdevice/class/cdc_vcom.c @@ -755,7 +755,11 @@ static rt_ssize_t _vcom_rb_block_put(struct vcom *data, const rt_uint8_t *buf, r return size; } -static rt_ssize_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction) +#ifdef RT_USING_SERIAL_V1 +static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction) +#else +static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, rt_uint32_t tx_flag) +#endif { struct ufunction *func; struct vcom *data;