Skip to content

Commit

Permalink
fix(serial_v2): tx_fifo->rb.buffer_ptr not init when RT_DEVICE_FLAG_D…
Browse files Browse the repository at this point in the history
…MA_TX and RT_SERIAL_TX_BLOCKING

为什么提交这份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。
  • Loading branch information
ComerLater authored Feb 21, 2024
1 parent c6bdee3 commit 82bb104
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions components/drivers/serial/serial_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion components/drivers/usb/usbdevice/class/cdc_vcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

2 comments on commit 82bb104

@illusionkiller
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个疑问请教一下大佬:
1、上述serial_v2提交引入的代码(tx_fifo->rb.buffer_ptr = RT_NULL)感觉和700行的代码(rt_memset(&tx_fifo->rb, RT_NULL, sizeof(tx_fifo->rb)))有重复?
2、如果是为了保证初始化,是否可以考虑使用rt_calloc

@mysterywolf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好建议~ 欢迎提交PR~

Please sign in to comment.