Skip to content

Conversation

@LiHua000
Copy link

@LiHua000 LiHua000 commented Feb 11, 2026

Log: as title

Summary by Sourcery

Bug Fixes:

  • Prevent crashes in SDL audio setup callback by checking for null data and player pointers before dereferencing.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 11, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds defensive pointer validation in SdlPlayer::libvlc_audio_setup_cb to avoid dereferencing null data and player pointers, with critical logging and early returns on failure.

Sequence diagram for updated libvlc_audio_setup_cb pointer validation

sequenceDiagram
    participant LibVLC
    participant SdlPlayer_libvlc_audio_setup_cb as libvlc_audio_setup_cb
    participant QLogging as qCritical
    participant SdlPlayer

    LibVLC->>SdlPlayer_libvlc_audio_setup_cb: call with data, format, rate, channels
    SdlPlayer_libvlc_audio_setup_cb->>SdlPlayer_libvlc_audio_setup_cb: resolve SDL_Delay, SDL_OpenAudio, av_log2

    alt data is null
        SdlPlayer_libvlc_audio_setup_cb->>QLogging: log Null data pointer in audio setup
        SdlPlayer_libvlc_audio_setup_cb-->>LibVLC: return -1
    else data is not null
        SdlPlayer_libvlc_audio_setup_cb->>SdlPlayer_libvlc_audio_setup_cb: PauseAudio(1)
        SdlPlayer_libvlc_audio_setup_cb->>SdlPlayer: dereference data to SdlPlayer pointer
        alt sdlMediaPlayer is null
            SdlPlayer_libvlc_audio_setup_cb->>QLogging: log Invalid player instance in audio setup
            SdlPlayer_libvlc_audio_setup_cb-->>LibVLC: return -1
        else sdlMediaPlayer is valid
            SdlPlayer_libvlc_audio_setup_cb->>SdlPlayer: cleanMemCache()
            SdlPlayer_libvlc_audio_setup_cb->>SdlPlayer: set _rate via libvlc_audio_format(format)
            SdlPlayer_libvlc_audio_setup_cb-->>LibVLC: continue with normal setup
        end
    end
Loading

Class diagram for SdlPlayer with updated libvlc_audio_setup_cb

classDiagram
    class SdlPlayer {
        +int libvlc_audio_setup_cb(void** data, char* format, unsigned* rate, unsigned* channels)
        -int _rate
        +void cleanMemCache()
    }

    class VlcDynamicInstance {
        +static VlcDynamicInstance* VlcFunctionInstance()
        +void* resolveSdlSymbol(char* name)
        +void* resolveSymbol(char* name, bool isOptional)
    }

    class SDL_Audio_API {
        +void SDL_Delay(unsigned ms)
        +int SDL_OpenAudio(void* desired, void* obtained)
    }

    class AVUtil_API {
        +int av_log2(unsigned v)
    }

    class QtLogging {
        +void qCritical()
    }

    SdlPlayer ..> VlcDynamicInstance : uses
    SdlPlayer ..> SDL_Audio_API : resolves_and_uses
    SdlPlayer ..> AVUtil_API : resolves_and_uses
    SdlPlayer ..> QtLogging : logs_critical_errors
Loading

File-Level Changes

Change Details Files
Add defensive null checks and logging around audio setup pointer usage to prevent null dereference crashes.
  • Introduce a null check for the data pointer before it is dereferenced, logging a critical error and aborting setup when null.
  • Add a null check for the SdlPlayer instance after casting from the data pointer, logging a critical error and aborting setup when null.
  • Maintain existing audio pause and cache cleanup logic, but ensure it only executes when both pointers are valid.
src/music-player/core/vlc/sdlplayer.cpp

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

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 left some high level feedback:

  • Consider removing or translating the Chinese inline comment to keep code comments linguistically consistent with the surrounding codebase.
  • Emitting qCritical from within the audio setup callback may be too heavy for a real-time/low-latency path; consider using a lighter-weight logging mechanism or deferring logging out of the callback if possible.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider removing or translating the Chinese inline comment to keep code comments linguistically consistent with the surrounding codebase.
- Emitting qCritical from within the audio setup callback may be too heavy for a real-time/low-latency path; consider using a lighter-weight logging mechanism or deferring logging out of the callback if possible.

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.

max-lvs
max-lvs previously approved these changes Feb 11, 2026
@LiHua000
Copy link
Author

/merge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 11, 2026

This pr cannot be merged! (status: unstable)

@LiHua000 LiHua000 force-pushed the release/eagle branch 2 times, most recently from e1f58ee to cd76f3e Compare February 11, 2026 05:31
max-lvs
max-lvs previously approved these changes Feb 11, 2026
@LiHua000 LiHua000 force-pushed the release/eagle branch 4 times, most recently from 8f012a2 to cd76f3e Compare February 11, 2026 06:06
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LiHua000, 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

@LiHua000
Copy link
Author

/forcemerge

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要是增加了对空指针的防御性检查,这是一个很好的改进。以下是对这段代码的详细审查和改进意见:

1. 语法逻辑审查

当前代码逻辑:

if (!data) {
    qCritical() << "Null data pointer in audio setup";
    return -1;
}
PauseAudio(1);
SdlPlayer *sdlMediaPlayer = *(SdlPlayer **)data;
if (!sdlMediaPlayer) {
    qCritical() << "Invalid player instance in audio setup";
    return -1;
}

问题分析:

  • 逻辑顺序正确,先检查 data 是否为空,再解引用
  • 检查 sdlMediaPlayer 是否为空也是必要的
  • 错误处理使用了 qCritical() 记录日志,这是好的做法

改进建议:

  1. 错误码 -1 可能不够明确,建议定义明确的错误码枚举
  2. 可以考虑在日志中包含更多上下文信息

2. 代码质量审查

优点:

  • 增加了防御性检查
  • 使用了 Qt 的日志系统
  • 代码缩进和格式规范

改进建议:

  1. 考虑使用更具体的错误类型:
enum class AudioSetupError {
    Success = 0,
    NullDataPointer = -1,
    InvalidPlayerInstance = -2,
    // 其他错误...
};
  1. 可以添加更多上下文信息到日志中:
qCritical() << "Null data pointer in audio setup. Function:" << __FUNCTION__;

3. 代码性能审查

当前实现:

  • 增加的检查不会对性能产生显著影响
  • 这些检查只在音频设置时执行,不是热路径

改进建议:

  • 无需优化,当前实现已经足够高效

4. 代码安全审查

安全问题:

  1. 空指针解引用:已通过检查解决
  2. 类型转换*(SdlPlayer **)data 的类型转换需要确保 data 确实指向正确的类型
  3. 资源泄漏:在错误返回前可能需要清理资源

改进建议:

  1. 添加类型安全检查:
// 确保data指向的是有效的SdlPlayer指针
if (!data || !(*data)) {
    qCritical() << "Invalid data pointer in audio setup";
    return -1;
}
  1. 考虑使用 static_cast 替代 C 风格转换:
SdlPlayer *sdlMediaPlayer = static_cast<SdlPlayer *>(*data);

综合改进后的代码建议:

int SdlPlayer::libvlc_audio_setup_cb(void **data, char *format, unsigned *rate,
                                     unsigned *channels) {
    // 获取函数指针
    SDL_PauseAudio_function PauseAudio = (SDL_PauseAudio_function)VlcDynamicInstance::VlcFunctionInstance()->resolveSdlSymbol("SDL_PauseAudio");
    SDL_Delay_function Delay = (SDL_Delay_function)VlcDynamicInstance::VlcFunctionInstance()->resolveSdlSymbol("SDL_Delay");
    SDL_OpenAudio_function OpenAudio = (SDL_OpenAudio_function)VlcDynamicInstance::VlcFunctionInstance()->resolveSdlSymbol("SDL_OpenAudio");
    av_log2_function Log2 = (av_log2_function)VlcDynamicInstance::VlcFunctionInstance()->resolveSymbol("av_log2", true);

    // 防御性编程:检查指针有效性
    if (!data || !(*data)) {
        qCritical() << "Invalid data pointer in audio setup. Function:" << __FUNCTION__;
        return -1;
    }

    PauseAudio(1);
    
    // 使用static_cast进行类型安全的转换
    SdlPlayer *sdlMediaPlayer = static_cast<SdlPlayer *>(*data);
    
    if (!sdlMediaPlayer) {
        qCritical() << "Invalid player instance in audio setup. Function:" << __FUNCTION__;
        return -1;
    }
    
    sdlMediaPlayer->cleanMemCache();
    sdlMediaPlayer->_rate = libvlc_audio_format(format);
    
    // ... 其余代码 ...
    
    return 0;
}

总结

原始修改已经很好地解决了空指针解引用的安全问题。主要改进点包括:

  1. 保持了原有的防御性检查
  2. 增强了类型安全性
  3. 改进了日志记录
  4. 使代码更加健壮和可维护

这些改进不会影响性能,同时提高了代码的安全性和可维护性。

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 11, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit c65a2d0 into linuxdeepin:release/eagle Feb 11, 2026
38 of 41 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