-
Notifications
You must be signed in to change notification settings - Fork 135
PalKey should use GSocketAddress #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideRefactors 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 GSocketAddressclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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_asnullptrbut methods likeoperator==,GetIpv4, andGetIpv4Stringunconditionally dereference it; consider guarding againstnullptr(or clearly documenting that moved-fromPalKeymust not be used) to avoid potential crashes. GetSocketAddress()blindly callsg_object_ref(address_); it would be safer to handle theaddress_ == nullptrcase explicitly (either returningnullptrwithout callingg_object_refor 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 handlesg_object_ref/unrefto 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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:
Enhancements:
Tests: