RT-Thread / RT-Thread/rt-thread

[Bug] SDIO 驱动注销失败与节点内存泄漏风险

Open
#11,801 3 comments 0 reactions 1 assignee View on GitHub

@Huoyanlifusu is already working on this.

Since Sep 15, 2026.

bug Component component: drivers in progress
Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

RT-Thread Version

master e538ad76e475ba6026e563a9c605b58a1f2f9cc0

Affected area

Device drivers

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

无特定版型要求

Develop Toolchain

GCC

Describe the bug

尽管多数 sdio 设备(比如 mmc/sd卡)不会热插拔,出于代码稳定角度考虑,建议进行这几处优化。

  1. 系统加载多个 sdio 驱动,在卸载驱动时,链表未及时退出,sd 可能会被下一轮循环覆盖为 NULL
    for (l = (&sdio_drivers)->next; l != &sdio_drivers; l = l->next)
    {
        sd = (struct sdio_driver *)rt_list_entry(l, struct sdio_driver, list);
        if (sd->drv != driver)
        {
            sd = RT_NULL;
        }
    }
  1. 目前释放 sd 指针动作在软件找到卡后才进行,有内存泄漏风险,这个指针的申请动作不依赖 SDIO 卡,因此释放也不需要依赖 SDIO 卡匹配到与否。
if (card != RT_NULL)
{
    driver->remove(card);
    rt_list_remove(&sd->list);
    rt_free(sd);
}
  1. 可选优化:使用 rt_list_for_each_entry 替代 rt_list_entry + for 的写法
Other additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.