Skip to content

feat: implement sd_bus_read_dict function - #31

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
fly602:master
Nov 13, 2025
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
fly602:master

Conversation

@fly602

@fly602 fly602 commented Nov 12, 2025 •

Copy link
Copy Markdown
Contributor

Added missing sd_bus_read_dict implementation for D-Bus message parsing. This function reads a dictionary from a D-Bus message and stores it in a GHashTable. The implementation handles the D-Bus array of dictionary entries with variant values, properly navigating the message containers and extracting key-value pairs as strings.

The function was missing from the codebase but needed for complete D-Bus functionality. It follows the standard sd-bus API patterns and includes proper error handling for all D-Bus operations.

Influence:

  1. Test with valid D-Bus messages containing dictionary data
  2. Verify error handling with malformed or empty dictionaries
  3. Test memory management and hash table creation
  4. Verify proper container navigation and cleanup
  5. Test with different dictionary sizes and key-value types

feat: 实现sd_bus_read_dict函数

添加了缺失的sd_bus_read_dict实现,用于解析D-Bus消息。该函数从D-Bus消息中
读取字典并将其存储在GHashTable中。实现处理带有变体值的字典条目数组,正确
导航消息容器并提取字符串类型的键值对。

该函数在代码库中缺失但为完整的D-Bus功能所必需。它遵循标准sd-bus API模
式,并包含对所有D-Bus操作的适当错误处理。

Influence:

  1. 测试包含字典数据的有效D-Bus消息
  2. 验证对格式错误或空字典的错误处理
  3. 测试内存管理和哈希表创建
  4. 验证正确的容器导航和清理
  5. 测试不同字典大小和键值类型

Summary by Sourcery

New Features:

  • Add sd_bus_read_dict to parse D-Bus dictionary messages into a GHashTable with variant support

@sourcery-ai

sourcery-ai Bot commented Nov 12, 2025

Copy link
Copy Markdown

Reviewer's Guide

This PR adds the missing sd_bus_read_dict function to service.c, implementing full D-Bus dictionary parsing into a GHashTable by navigating array and dict entry containers, extracting key/value strings, and performing robust error handling.

Class diagram for sd_bus_read_dict addition

classDiagram
class sd_bus_message {
  +enter_container(type, signature)
  +exit_container()
  +read_basic(type, &value)
  +peek_type(&type, &contents)
}
class GHashTable {
  +insert(key, value)
}
class service.c {
  +sd_bus_read_dict(msg, **map)
}
sd_bus_message <.. service.c : uses
GHashTable <.. service.c : uses
Loading

Flow diagram for sd_bus_read_dict dictionary parsing

flowchart TD
    A["Start sd_bus_read_dict(msg, **map)"] --> B["Create new GHashTable"]
    B --> C["Enter ARRAY container '{sv}'"]
    C --> D{"Next DICT_ENTRY?"}
    D -- Yes --> E["Read key (string)"]
    E --> F["Peek variant type"]
    F --> G["Enter VARIANT container"]
    G --> H["Read value (string)"]
    H --> I["Insert key/value into GHashTable"]
    I --> J["Exit VARIANT container"]
    J --> K["Exit DICT_ENTRY container"]
    K --> D
    D -- No --> L["Exit ARRAY container"]
    L --> M["Return result"]
Loading

File-Level Changes

Change Details Files
Implement sd_bus_read_dict to parse D-Bus dictionaries into a GHashTable
  • Allocate a new GHashTable for the map
  • Enter the array container of {sv} entries
  • Loop over each DICT_ENTRY: read the key string
  • Peek and enter the VARIANT container for the value
  • Read the value string and insert key/value into the hash table
  • Exit both variant and dict entry containers
  • Exit the array container and return result
src/plugin-sdbus/plugin-ipwatchd/service.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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Duplicate the key and value strings (e.g. with g_strdup) before inserting into the hash table or ensure you manage the message strings’ lifetimes to prevent dangling pointers.
  • Add error‐path cleanup to free the hash table and exit any open containers on failure to avoid resource leaks.
  • Validate the incoming variant type is SD_BUS_TYPE_STRING (or handle other types) before reading to avoid unexpected type errors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Duplicate the key and value strings (e.g. with g_strdup) before inserting into the hash table or ensure you manage the message strings’ lifetimes to prevent dangling pointers.
- Add error‐path cleanup to free the hash table and exit any open containers on failure to avoid resource leaks.
- Validate the incoming variant type is SD_BUS_TYPE_STRING (or handle other types) before reading to avoid unexpected type errors.

## Individual Comments

### Comment 1
<location> `src/plugin-sdbus/plugin-ipwatchd/service.c:66-75` </location>
<code_context>
+int sd_bus_read_dict(sd_bus_message *msg, GHashTable **map) {
</code_context>

<issue_to_address>
**issue (bug_risk):** Potential memory leak due to missing cleanup of GHashTable on error.

Ensure the hash table is freed if an error occurs before function completion to prevent memory leaks.
</issue_to_address>

### Comment 2
<location> `src/plugin-sdbus/plugin-ipwatchd/service.c:95` </location>
<code_context>
+    r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &value);
+    if (r < 0)
+      return r;
+    g_hash_table_insert(*map, (gpointer)key, (gpointer)value);
+    r = sd_bus_message_exit_container(msg);
+    if (r < 0)
</code_context>

<issue_to_address>
**issue (bug_risk):** Possible lifetime issue with key and value pointers.

Since the hash table stores pointers managed by sd_bus_message, freeing or reusing the message may invalidate them. Duplicate the strings before insertion to avoid dangling pointers.
</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.

r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &value);
if (r < 0)
return r;
g_hash_table_insert(*map, (gpointer)key, (gpointer)value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Possible lifetime issue with key and value pointers.

Since the hash table stores pointers managed by sd_bus_message, freeing or reusing the message may invalidate them. Duplicate the strings before insertion to avoid dangling pointers.

robertkill
robertkill previously approved these changes Nov 12, 2025
Added missing sd_bus_read_dict implementation for D-Bus message parsing.
This function reads a dictionary from a D-Bus message and stores it in
a GHashTable. The implementation handles the D-Bus array of dictionary
entries with variant values, properly navigating the message containers
and extracting key-value pairs as strings.

The function was missing from the codebase but needed for complete D-Bus
functionality. It follows the standard sd-bus API patterns and includes
proper error handling for all D-Bus operations.

Influence:
1. Test with valid D-Bus messages containing dictionary data
2. Verify error handling with malformed or empty dictionaries
3. Test memory management and hash table creation
4. Verify proper container navigation and cleanup
5. Test with different dictionary sizes and key-value types

feat: 实现sd_bus_read_dict函数

添加了缺失的sd_bus_read_dict实现,用于解析D-Bus消息。该函数从D-Bus消息中
读取字典并将其存储在GHashTable中。实现处理带有变体值的字典条目数组,正确
导航消息容器并提取字符串类型的键值对。

该函数在代码库中缺失但为完整的D-Bus功能所必需。它遵循标准sd-bus API模
式,并包含对所有D-Bus操作的适当错误处理。

Influence:
1. 测试包含字典数据的有效D-Bus消息
2. 验证对格式错误或空字典的错误处理
3. 测试内存管理和哈希表创建
4. 验证正确的容器导航和清理
5. 测试不同字典大小和键值类型
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我来对这个diff进行仔细的审查:

  1. .reuse/dep5 文件变更:
  • 在Files模式中添加了*.desktop文件,这是一个合理的更新,用于包含桌面配置文件
  • 使用CC0-1.0许可证,这是合适的,因为这些通常是配置文件
  1. service.c 新增的sd_bus_read_dict函数:

代码质量方面的问题:

  1. 内存泄漏风险:
*map = g_hash_table_new(g_str_hash, g_str_equal);

如果在后续操作中出现错误,这个hash_table不会被释放。应该在错误处理中添加清理代码。

  1. 错误处理不完整:
  • 函数在遇到错误时直接返回,没有清理已分配的资源
  • 没有验证输入参数msg和map是否为NULL
  1. 代码重复:
  • 两次调用sd_bus_message_exit_container的代码重复,可以考虑使用goto统一错误处理

建议改进后的代码:

int sd_bus_read_dict(sd_bus_message *msg, GHashTable **map) {
    int r = 0;
    GHashTable *temp_map = NULL;

    if (!msg || !map) {
        return -EINVAL;
    }

    temp_map = g_hash_table_new(g_str_hash, g_str_equal);
    if (!temp_map) {
        return -ENOMEM;
    }

    r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_ARRAY, "{sv}");
    if (r < 0)
        goto cleanup;

    while ((r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_DICT_ENTRY,
                                             "sv")) > 0) {
        const char *key;
        const char *value;
        const char *contents;

        r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &key);
        if (r < 0)
            goto cleanup;

        r = sd_bus_message_peek_type(msg, NULL, &contents);
        if (r < 0)
            goto cleanup;

        r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_VARIANT, contents);
        if (r < 0)
            goto cleanup;

        r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &value);
        if (r < 0)
            goto cleanup;

        g_hash_table_insert(temp_map, (gpointer)key, (gpointer)value);
        
        r = sd_bus_message_exit_container(msg);
        if (r < 0)
            goto cleanup;

        r = sd_bus_message_exit_container(msg);
        if (r < 0)
            goto cleanup;
    }

    r = sd_bus_message_exit_container(msg);
    if (r >= 0)
        *map = temp_map;
    
cleanup:
    if (r < 0 && temp_map)
        g_hash_table_destroy(temp_map);
    return r;
}

安全性方面:

  1. 使用了g_hash_table_insert直接存储原始指针,这可能存在安全隐患。建议使用g_strdup复制字符串:
g_hash_table_insert(temp_map, g_strdup(key), g_strdup(value));
  1. 没有对输入的字符串长度进行检查,可能导致内存问题。

性能方面:

  1. 代码结构合理,没有明显的性能问题
  2. 使用了GHashTable,这是一个高效的哈希表实现

其他建议:

  1. 添加函数文档说明,包括参数说明和返回值说明
  2. 考虑添加日志记录,便于调试
  3. 可以考虑使用更具体的错误代码,而不是直接返回sd-bus的错误码

这些改进将使代码更加健壮、安全和可维护。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, yixinshark

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

@fly602

fly602 commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 13, 2025

Copy link
Copy Markdown

This pr force merged! (status: blocked)

@deepin-bot
deepin-bot Bot merged commit 5f42d31 into linuxdeepin:master Nov 13, 2025
7 of 8 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.

4 participants