summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCXSforHPU <[email protected]>2026-11-30 22:35:52 +0800
committerRbb666 <[email protected]>2026-12-01 10:43:59 +0800
commitfbdb9711db8be41fe84f92a2f983c048c11f9a47 (patch)
tree3c5a0b9a39b1c67ab40ef20c55626dc621d8997b
parent75b53af8ccd14047b836488869e75a3f0be27f4e (diff)
[components][drivers][spi] dev_spi_flash_sfud: 使用 rt_strdup() 复制设备名HEADmaster
rt_sfud_flash_probe_ex() 通过 rt_strncpy(dst, src, rt_strlen(src)) 复制两个 设备名。第三个参数表示的是源字符串长度,而不是目标缓冲区容量;且此时 strncpy() 不会复制结尾的 '\0',正确性依赖紧随其后的两行 "make string end sign" 手工补终。当 rt_strncpy() 映射到 libc 内建实现时,GCC 会以 -Wstringop-overflow("specified bound depends on the length of the source argument")报出该写法,对应 issue #6668。 改用 rt_strdup():一次性分配 strlen() + 1 字节并连同 '\0' 一起拷贝,因此删除 strncpy() 与手工补终。所有权不变:两个缓冲区仍交给 SFUD,由 rt_sfud_flash_delete() 以及本函数的错误路径通过 rt_free() 释放;rt_strdup() 分配失败返回 NULL,已有的判空逻辑已覆盖。 Fixes #6668
-rw-r--r--components/drivers/spi/dev_spi_flash_sfud.c10
1 files changed, 3 insertions, 7 deletions
diff --git a/components/drivers/spi/dev_spi_flash_sfud.c b/components/drivers/spi/dev_spi_flash_sfud.c
index 7efb6e594b..cb349d1599 100644
--- a/components/drivers/spi/dev_spi_flash_sfud.c
+++ b/components/drivers/spi/dev_spi_flash_sfud.c
@@ -7,6 +7,7 @@
* Date Author Notes
* 2016-09-28 armink first version.
* 2025-10-30 wdfk-prog enable interrupt-safe operations using spinlocks
+ * 2026-11-30 CXSforHPU use rt_strdup() to copy SFUD device names
*/
#include <stdint.h>
@@ -345,8 +346,8 @@ rt_spi_flash_device_t rt_sfud_flash_probe_ex(const char *spi_flash_dev_name, con
rtt_dev = (rt_spi_flash_device_t) rt_malloc(sizeof(struct spi_flash_device));
sfud_dev = (sfud_flash_t) rt_malloc(sizeof(sfud_flash));
- spi_flash_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_flash_dev_name) + 1);
- spi_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_dev_name) + 1);
+ spi_flash_dev_name_bak = rt_strdup(spi_flash_dev_name);
+ spi_dev_name_bak = rt_strdup(spi_dev_name);
if (rtt_dev) {
rt_memset(rtt_dev, 0, sizeof(struct spi_flash_device));
@@ -359,11 +360,6 @@ rt_spi_flash_device_t rt_sfud_flash_probe_ex(const char *spi_flash_dev_name, con
if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) {
rt_memset(sfud_dev, 0, sizeof(sfud_flash));
- rt_strncpy(spi_flash_dev_name_bak, spi_flash_dev_name, rt_strlen(spi_flash_dev_name));
- rt_strncpy(spi_dev_name_bak, spi_dev_name, rt_strlen(spi_dev_name));
- /* make string end sign */
- spi_flash_dev_name_bak[rt_strlen(spi_flash_dev_name)] = '\0';
- spi_dev_name_bak[rt_strlen(spi_dev_name)] = '\0';
/* SPI configure */
{
/* RT-Thread SPI device initialize */