Skip to content

Conversation

@lidaobing
Copy link
Member

@lidaobing lidaobing commented Jan 25, 2026

Summary by Sourcery

Refactor PalKey to wrap a GSocketAddress instead of raw IPv4/port data and expose socket-address-based accessors while maintaining equality and string representation behavior.

New Features:

  • Add a PalKey constructor that accepts a GSocketAddress and a getter to retrieve the underlying GSocketAddress.

Enhancements:

  • Manage PalKey lifetime via GObject reference counting with explicit copy/move constructors and assignment operators.
  • Derive IPv4, port, and string representations from the underlying GSocketAddress instead of stored primitive fields.

Tests:

  • Add unit tests validating PalKey construction from GSocketAddress, access to the underlying socket address, and equality with instances constructed from in_addr.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 25, 2026

Reviewer's Guide

Refactors PalKey to wrap a reference-counted GSocketAddress instead of raw IPv4/port fields, adds full value-semantic management (copy/move), updates methods to delegate to GSocketAddress/GInetAddress, and introduces tests validating construction, accessors, equality, and address retrieval via GLib networking APIs.

Updated class diagram for PalKey using GSocketAddress

classDiagram
class PalKey {
  - GSocketAddress* address_
  + PalKey(in_addr ipv4, int port)
  + PalKey(GSocketAddress* address)
  + ~PalKey()
  + PalKey(const PalKey& other)
  + PalKey& operator=(const PalKey& other)
  + PalKey(PalKey&& other) noexcept
  + PalKey& operator=(PalKey&& other) noexcept
  + bool operator==(const PalKey& rhs) const
  + in_addr GetIpv4() const
  + string GetIpv4String() const
  + int GetPort() const
  + string ToString() const
  + GSocketAddress* GetSocketAddress() const
}

PalKey --> GSocketAddress
PalKey --> GInetAddress
Loading

File-Level Changes

Change Details Files
Refactor PalKey to be backed by GSocketAddress and GLib networking primitives instead of raw ipv4/port fields.
  • Replace PalKey’s in_addr/port data members with a GSocketAddress* and adjust the constructor taking in_addr/port to build a GInetSocketAddress via g_inet_address_new_from_bytes and g_inet_socket_address_new.
  • Add an explicit constructor from GSocketAddress that refs the input address and store it in the PalKey.
  • Implement manual destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator to manage the GObject reference-counted GSocketAddress pointer correctly and avoid leaks/double-frees.
  • Reimplement GetIpv4, GetIpv4String, GetPort, ToString, and operator== to access and compare underlying GInetSocketAddress/GInetAddress objects, using GLib helpers for address/port conversion and equality.
  • Expose a GetSocketAddress accessor that returns a reffed GSocketAddress* to callers, documenting its refcount behavior.
  • Include the required GIO and C string headers to support the new GLib APIs and memcpy/memset usage.
src/iptux-core/Models.cpp
src/api/iptux-core/Models.h
Extend unit tests to cover the new GSocketAddress-based PalKey behavior.
  • Add a test that constructs PalKey from a GSocketAddress, verifies IPv4 string, port, and ToString outputs, and checks that GetSocketAddress returns an equivalent GSocketAddress.
  • Validate that a PalKey constructed from GSocketAddress compares equal to one constructed from in_addr/port, ensuring backward-compatible equality semantics.
  • Ensure proper cleanup of GLib objects in the new test by unref’ing addresses and socket addresses.
src/iptux-core/ModelsTest.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

@lidaobing lidaobing linked an issue Jan 25, 2026 that may be closed by this pull request
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:

  • The move operations leave address_ as nullptr but methods like operator==, GetIpv4, and GetIpv4String unconditionally dereference it; consider guarding against nullptr (or clearly documenting that moved-from PalKey must not be used) to avoid potential crashes.
  • GetSocketAddress() blindly calls g_object_ref(address_); it would be safer to handle the address_ == nullptr case explicitly (either returning nullptr without calling g_object_ref or asserting non-null) to avoid undefined behavior.
  • Managing GSocketAddress* manually across copy/move constructors and assignment is error-prone; consider wrapping the pointer in a small RAII helper that handles g_object_ref/unref to centralize and simplify the ownership logic.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The move operations leave `address_` as `nullptr` but methods like `operator==`, `GetIpv4`, and `GetIpv4String` unconditionally dereference it; consider guarding against `nullptr` (or clearly documenting that moved-from `PalKey` must not be used) to avoid potential crashes.
- `GetSocketAddress()` blindly calls `g_object_ref(address_)`; it would be safer to handle the `address_ == nullptr` case explicitly (either returning `nullptr` without calling `g_object_ref` or asserting non-null) to avoid undefined behavior.
- Managing `GSocketAddress*` manually across copy/move constructors and assignment is error-prone; consider wrapping the pointer in a small RAII helper that handles `g_object_ref/unref` to centralize and simplify the ownership logic.

## Individual Comments

### Comment 1
<location> `src/api/iptux-core/Models.h:83` </location>
<code_context>
   std::string ToString() const;
+  
+  // Get the underlying GSocketAddress (increases reference count)
+  GSocketAddress* GetSocketAddress() const;

  private:
</code_context>

<issue_to_address>
**suggestion:** Clarify and enforce ownership and lifetime guarantees around the returned GSocketAddress.

Since this returns `G_SOCKET_ADDRESS(g_object_ref(address_))`, the caller receives a new ref and assumes `address_` is always valid. To avoid crashes from a moved-from or otherwise invalid `PalKey`, ensure `address_` is either a valid non-null `GSocketAddress*` or `nullptr`, and in the null case return `nullptr` without calling `g_object_ref`.

Suggested implementation:

```c
  std::string GetIpv4String() const;
  int GetPort() const;
  std::string ToString() const;

  // Returns a new reference to the underlying GSocketAddress, or nullptr if none is set.
  // Caller owns the returned reference and must unref it with g_object_unref().
  GSocketAddress* GetSocketAddress() const;

```

In the corresponding implementation file (likely `src/api/iptux-core/Models.cpp`), update `PalKey::GetSocketAddress()` to enforce the ownership and nullability rules:

1. Guard against a null `address_`:
   ```c++
   GSocketAddress* PalKey::GetSocketAddress() const {
     if (!address_) {
       return nullptr;
     }
     return G_SOCKET_ADDRESS(g_object_ref(address_));
   }
   ```
2. Ensure all code that constructs or moves `PalKey` instances initializes `address_` to either a valid `GSocketAddress*` or `nullptr`, and that moved-from objects set `address_` to `nullptr` so the above check is reliable.
</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.

@github-actions
Copy link

github-actions bot commented Jan 25, 2026

Test Results

70 tests  +1   70 ✅ +1   3s ⏱️ ±0s
32 suites ±0    0 💤 ±0 
 1 files   ±0    0 ❌ ±0 

Results for commit 6aead8d. ± Comparison against base commit 6ea96a6.

♻️ This comment has been updated with latest results.

@codecov
Copy link

codecov bot commented Jan 25, 2026

Codecov Report

❌ Patch coverage is 75.34247% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.20%. Comparing base (6ea96a6) to head (6aead8d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/iptux-core/Models.cpp 80.23% 17 Missing ⚠️
src/iptux-core/internal/UdpData.cpp 65.11% 15 Missing ⚠️
src/iptux-core/internal/UdpDataService.cpp 66.66% 3 Missing ⚠️
src/iptux/UiHelper.cpp 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #704      +/-   ##
==========================================
+ Coverage   52.00%   52.20%   +0.20%     
==========================================
  Files          64       63       -1     
  Lines        8599     8679      +80     
==========================================
+ Hits         4472     4531      +59     
- Misses       4127     4148      +21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

refactor: change PalKey to depends on GSocketAddress

2 participants