Repository navigation
fix(lib): let devices refine the product name in the library too - #603
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
38c3418 to
e0bf2c9
Compare
Since Sapd#575 the CLI asks a device for its metadata the first time it opens a handle and uses the product name it reports, so a Jabra Link dongle shows the headset paired to it. The library never did: Headset::productName() and hsc_get_product_name() kept returning the dongle's USB string. HeadsetImpl now does the same in getConnection(), once, on the first handle it opens, so the name is refined by whatever call first talks to the device and discovery does no extra I/O. The C wrapper picks up the refined name on the next hsc_get_product_name() call. Both headers and LIBRARY_USAGE.md say when the name changes. The test device opens no handle, so this path is only reachable on hardware.
e0bf2c9 to
f255d57
Compare
|
cpp-linter fails in its setup step on this PR, not on the code: the action stops at "No release for Nushell matching version specifier 0.106.1" before the linter runs (latest run). It failed the same way on three pushes with identical content, while #601, #602 and #604 passed it. Could you rerun the job when you get a chance? I can't rerun it from a fork. |
Sapd
left a comment
There was a problem hiding this comment.
productName() was stable for the lifetime of the Headset before this; now it isn't. product_name_ = meta->product invalidates any view handed out earlier, and in C the returned const char* dangles when product_name_str is reassigned — the member commented "Persistent storage for C string".
const char *n = hsc_get_product_name(h);
hsc_get_battery(h, &bat); /* refines the name */
printf("%s", n); /* use-after-free */Keep the refined name in a second string that is assigned once, and return it when it is non-empty. Views taken before refinement keep pointing at the untouched original, views taken after point at the write-once one. The value still changes, the pointer never dangles, and the headers don't have to warn about invalidation at all. Same in the C wrapper: a second std::string rather than overwriting product_name_str.
The refinement itself is unreachable in tests, since getConnection() returns early for the test device — the once-only guard, the empty-name case and the error case are all unverified. Pulling the decision out of getConnection() into something taking the device and a string would make it testable against a mock.
The rest is right: the guard is set before getMetadata(), so a device that reopens a connection can't recurse, and the default getMetadata() only reads HID strings, so nothing changes for devices that don't override it.
Refining the product name overwrote the stored string, so a view from productName() or a pointer from hsc_get_product_name() taken before the first device call dangled afterwards. Keep the refined name in a second, write-once string instead, in both the C++ library and the C wrapper. Earlier views keep the USB string, later ones the refined name, and neither is ever invalidated. Move the refinement into ProductName so it can be tested against a mock device: the once-only guard, the empty name and the error case.
|
Thanks, both addressed. The refined name now goes into a second, write-once string ( |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Sapd
left a comment
There was a problem hiding this comment.
Write-once is the right shape — no dangling is possible by construction now, rather than being something the docs have to warn about.
Checked the new test under ASan, because it passes either way without one. Reverting ProductName to the old single-string behaviour:
ERROR: AddressSanitizer: heap-use-after-free
READ of size 39 at 0x604000088a90
#4 headsetcontrol::testing::testProductNameRefined() test_library_api.cpp:765
39 bytes is "USB Dongle With A Name Too Long For SSO", so picking a string past the SSO limit is what gives that assertion teeth — with a short name the reassignment stays in the inline buffer and nothing is freed. Worth keeping in mind that it only fails under a sanitizer; plain make check reads the freed bytes and compares them equal. The once-only, empty-name and error cases are deterministic, so those three hold on their own.
Restored and re-run: ASan clean, whole suite green.
Merging.
Changes made
Since #575 the CLI asks a device for its metadata the first time it opens a handle and uses the product name it reports, so a Jabra Link dongle shows the headset paired to it. The library never did:
Headset::productName()andhsc_get_product_name()kept returning the dongle's USB string.HeadsetImpl::getConnection()now callsgetMetadata()once, on the first handle it opens, so the name is refined by whatever call first talks to the device. Discovery does no extra I/O.hsc_get_product_name()call.docs/LIBRARY_USAGE.mdsay when the name changes: read it after the first query (e.g.getBattery()) to get the refined one.The test device opens no HID handle, so this path is only reachable on hardware; existing tests pass and still cover the enumeration name.
Checklist
For new features in HeadsetControl: I discussed it beforehand in Issues or Discussions and adhered to the wikiNot applicable: this is a fix and adds no new capability.