refactor: use String for TransactionRecord::label#624
Conversation
Instead of `Option<String>`. Empty string represents "no label".
📝 WalkthroughWalkthroughRefactors label handling: Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v0.42-dev #624 +/- ##
=============================================
+ Coverage 67.62% 67.66% +0.03%
=============================================
Files 321 318 -3
Lines 68253 67826 -427
=============================================
- Hits 46159 45896 -263
+ Misses 22094 21930 -164
|
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@dash-spv-ffi/src/callbacks.rs`:
- Around line 733-737: The code builds c_label from record.label using
CString::new(...).unwrap_or_default(), which silently masks interior NUL errors;
replace that with a fallible conversion so failures become None: call
CString::new(record.label.as_str()).ok() and map that into the Option<CString>
(i.e., use Some(...) only when ok() yields Some), removing unwrap_or_default()
and any unwrap/expect usage around CString::new to properly propagate conversion
failure for c_label and preserve safety in marshaling.
In `@key-wallet-ffi/src/managed_account.rs`:
- Around line 781-785: The code currently uses unwrap_or_default() when
converting record.label to a CString, which silently coerces invalid input
(e.g., interior NULs) into an empty string; change the conversion for
ffi_record.label to use
CString::new(record.label.as_str()).ok().map_or(std::ptr::null_mut(), |c|
c.into_raw()) so invalid labels return null instead of being treated as empty,
and apply the same pattern to the address conversion noted (replace its
unwrap_or_default path with ok().map_or(std::ptr::null_mut(), |c|
c.into_raw())); this preserves the FFI contract and matches the empty-label
handling.
In `@key-wallet/src/managed_account/transaction_record.rs`:
- Around line 158-165: The set_label method currently only checks length and
allows strings containing NUL ('\0') bytes which later get lost when converted
for FFI; update the set_label(&mut self, label: String) -> Result<(), Error> in
transaction_record.rs to also scan the input for any NUL bytes (e.g.,
label.contains('\0')) and return Err(Error::InvalidParameter("Label contains NUL
byte".to_string())) if found, before assigning self.label = label; keep the
existing MAX_LABEL_LENGTH check and error behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 72fb5c48-8dea-4bb7-828b-708a19242d49
📒 Files selected for processing (3)
dash-spv-ffi/src/callbacks.rskey-wallet-ffi/src/managed_account.rskey-wallet/src/managed_account/transaction_record.rs
| None | ||
| } else { | ||
| Some(CString::new(record.label.as_str()).unwrap_or_default()) | ||
| }; |
There was a problem hiding this comment.
line 764 we are mapping empty (or None after this step) to a null pointer and Some to a not null ptr, we can skip this mapping to an Option enum:
This is line 764
label: c_label
.as_ref()
.map_or(std::ptr::null_mut(), |l| l.as_ptr() as *mut _),
ZocoLini
left a comment
There was a problem hiding this comment.
I missclicked, I dint mean to approve xd
Addresses ZocoLini review comment on PR dashpay#624 dashpay#624 (comment)
Addresses ZocoLini review comment on PR dashpay#624 dashpay#624 (comment)
Addresses ZocoLini review comment on PR #624 #624 (comment)
There was a problem hiding this comment.
♻️ Duplicate comments (1)
dash-spv-ffi/src/callbacks.rs (1)
733-738:⚠️ Potential issue | 🟠 MajorStop masking
CString::newfailures forlabel.
unwrap_or_default()hides interior-NUL conversion errors and may pass a non-null empty C string for an invalid non-empty label. Use a fallible conversion and map failures toNULLexplicitly.Suggested fix
- let c_label = CString::new(record.label.as_str()).unwrap_or_default(); - let c_label_ptr = if record.label.is_empty() { - std::ptr::null_mut() - } else { - c_label.as_ptr() as *mut _ - }; + let c_label = if record.label.is_empty() { + None + } else { + CString::new(record.label.as_str()).ok() + }; + let c_label_ptr = c_label + .as_ref() + .map_or(std::ptr::null_mut(), |label| label.as_ptr() as *mut _);As per coding guidelines, "
**/*.rs: Use proper error types with thiserror crate and propagate errors appropriately."Also applies to: 765-765
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dash-spv-ffi/src/callbacks.rs` around lines 733 - 738, The code is masking CString::new failures for record.label by using unwrap_or_default; instead perform a fallible conversion and set c_label_ptr to NULL on error or empty input: call CString::new(record.label.as_str()) and match the Result, treating Err(_) the same as an empty label by using std::ptr::null_mut() for c_label_ptr; ensure you don’t produce a pointer to an empty internal CString created by unwrap_or_default and apply the same fix to the other occurrence handling record.label (the one using c_label and c_label_ptr).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@dash-spv-ffi/src/callbacks.rs`:
- Around line 733-738: The code is masking CString::new failures for
record.label by using unwrap_or_default; instead perform a fallible conversion
and set c_label_ptr to NULL on error or empty input: call
CString::new(record.label.as_str()) and match the Result, treating Err(_) the
same as an empty label by using std::ptr::null_mut() for c_label_ptr; ensure you
don’t produce a pointer to an empty internal CString created by
unwrap_or_default and apply the same fix to the other occurrence handling
record.label (the one using c_label and c_label_ptr).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 13b1e164-6a86-4f3b-a411-a03592e3375b
📒 Files selected for processing (1)
dash-spv-ffi/src/callbacks.rs
Instead of
Option<String>. Empty string represents "no label".Summary by CodeRabbit