Skip to content

fix: audio_free_buffers() add a NULL check.#441

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:release/eaglefrom
lichaofan2008:release/eagle1
Dec 26, 2025
Merged

fix: audio_free_buffers() add a NULL check.#441
deepin-bot[bot] merged 1 commit intolinuxdeepin:release/eaglefrom
lichaofan2008:release/eagle1

Conversation

@lichaofan2008
Copy link
Contributor

@lichaofan2008 lichaofan2008 commented Dec 25, 2025

On the Zhaoxin processor, there is a probabilistic occurrence of null pointer/double-free issues with audio_buffers[i].data, so a NULL check needs to be added.
在兆芯处理器上,audio_buffers[i].data 概率性出现空指针/重复释放的情况,所以需要增加 NULL 判断。

Bug: https://pms.uniontech.com/bug-view-345713.html

Summary by Sourcery

Bug Fixes:

  • Prevent potential null pointer dereference and double-free when releasing audio buffer data in audio_free_buffers().

On the Zhaoxin processor, there is a probabilistic occurrence of null pointer/double-free issues with `audio_buffers[i].data`, so a NULL check needs to be added.
在兆芯处理器上,audio_buffers[i].data 概率性出现空指针/重复释放的情况,所以需要增加 NULL 判断。

Bug: https://pms.uniontech.com/bug-view-345713.html
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 25, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds defensive null checking and pointer reset to the audio buffer cleanup routine to prevent null pointer and double-free issues observed on Zhaoxin processors.

Flow diagram for updated audio_free_buffers logic

flowchart TD
    A[start_audio_free_buffers] --> B[check_audio_buffers_not_null]
    B --> C[initialize_i_to_0]
    C --> D{is_i_less_than_AUDBUFF_NUM}
    D -->|no| H[free_audio_buffers_and_return]
    D -->|yes| E[check_audio_buffers_i_data_is_null]
    E -->|yes| F[increment_i]
    E -->|no| G[free_audio_buffers_i_data_and_set_to_null]
    G --> F
    F --> D

    subgraph loop_over_audio_buffers
        C
        D
        E
        F
        G
    end
Loading

File-Level Changes

Change Details Files
Harden audio_free_buffers() against null and double-free issues when releasing audio buffer data.
  • Add a NULL pointer guard before freeing each audio buffer's data pointer in the cleanup loop
  • Set the audio buffer data pointer to NULL immediately after freeing it to avoid potential double-free on subsequent calls
libcam/libcam_audio/audio.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查分析:

  1. 语法逻辑:
  • 代码语法正确,增加了空指针检查和置NULL操作,符合C语言的内存管理规范
  • 逻辑上处理了audio_buffers[i].data可能为空的情况,避免了重复释放
  1. 代码质量:
  • 优点:

    • 增加了防御性编程,提高了代码健壮性
    • 在释放内存后立即将指针置为NULL,这是良好的编程习惯
    • 注释说明了修改原因,便于代码维护
  • 建议:

    • 可以考虑在audio_buffers结构体初始化时就确保data指针初始化为NULL
    • 建议在audio_free_buffers函数开始处增加对audio_buffers本身的空指针检查
  1. 代码性能:
  • 增加的判断操作对性能影响极小,可以忽略不计
  • 这种检查是必要的,相比可能的程序崩溃,这点性能开销是值得的
  1. 代码安全:
  • 修改有效防止了空指针解引用和重复释放的问题
  • 建议补充:
    static void audio_free_buffers()
    {
        int i;
        
        if (audio_buffers == NULL) {
            return;
        }
        
        for(i = 0; i < AUDBUFF_NUM; ++i)
        {
            if (audio_buffers[i].data != NULL) {
                free(audio_buffers[i].data);
                audio_buffers[i].data = NULL;
            }
        }
        
        free(audio_buffers);
        audio_buffers = NULL;  // 建议也加上这行
    }
  1. 其他建议:
  • 考虑添加日志记录,当检测到空指针时记录相关调试信息
  • 可以考虑在代码中添加断言(ASSERT)来帮助调试
  • 建议在代码中添加更多的注释说明内存管理策略

总体来说,这个修改是合理且必要的,有效提高了代码的安全性和稳定性。建议按照上述补充建议进一步完善代码。

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Instead of just guarding against NULL/double-free here, consider tracing and fixing the underlying lifecycle issue that leaves audio_buffers[i].data in an inconsistent state, otherwise the bug may reappear in other call sites.
  • If the project convention is to keep comments in a single language, you may want to either translate the new comment to English or mirror it with an English version for consistency.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of just guarding against NULL/double-free here, consider tracing and fixing the underlying lifecycle issue that leaves `audio_buffers[i].data` in an inconsistent state, otherwise the bug may reappear in other call sites.
- If the project convention is to keep comments in a single language, you may want to either translate the new comment to English or mirror it with an English version for consistency.

## Individual Comments

### Comment 1
<location> `libcam/libcam_audio/audio.c:134-135` </location>
<code_context>

 	for(i = 0; i < AUDBUFF_NUM; ++i)
 	{
+		// 在兆芯处理器上,audio_buffers[i].data 概率性出现空指针/重复释放的情况,所以这里需要增加判断
+		if (audio_buffers[i].data == NULL) {
+			continue;
+		}
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Silently skipping NULL entries may hide the underlying lifecycle issue.

If `audio_buffers[i].data` is unexpectedly NULL, we now just continue, which may hide a real lifecycle bug on that platform. Please at least log or count these occurrences so you can tell the difference between "never allocated" and "became NULL due to a bug" and make future debugging feasible.

Suggested implementation:

```c
	for(i = 0; i < AUDBUFF_NUM; ++i)
	{
		// 在兆芯处理器上,audio_buffers[i].data 概率性出现空指针/重复释放的情况,所以这里需要增加判断
		if (audio_buffers[i].data == NULL) {
			// 避免静默跳过 NULL 指针,记录一次以便后续排查生命周期问题
			fprintf(stderr,
			        "libcam_audio: audio_buffers[%d].data is NULL while releasing buffers\n",
			        i);
			continue;
		}

		free(audio_buffers[i].data);
		audio_buffers[i].data = NULL;
	}

	free(audio_buffers);

```

1. At the top of `libcam/libcam_audio/audio.c`, ensure `#include <stdio.h>` is present so that `fprintf` is available.
2. If the project uses its own logging abstraction (e.g. `LOGE`, `CAM_ERR`, etc.), replace `fprintf(stderr, ...)` with the appropriate logging macro to be consistent with the rest of the codebase.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, max-lvs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lichaofan2008
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Dec 26, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 4c8d011 into linuxdeepin:release/eagle Dec 26, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants