Skip to content

fix(lib): let devices refine the product name in the library too - #603

Merged
Sapd merged 2 commits into
Sapd:masterfrom
ChrisLauinger77:feat/lib-refined-product-name
Oct 22, 2026
Merged

Sapd merged 2 commits into
Sapd:masterfrom
ChrisLauinger77:feat/lib-refined-product-name

Conversation

@ChrisLauinger77

Copy link
Copy Markdown
Contributor

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() and hsc_get_product_name() kept returning the dongle's USB string.

  • HeadsetImpl::getConnection() now calls getMetadata() 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.
  • The C wrapper picks up the refined name on the next hsc_get_product_name() call.
  • Both headers and docs/LIBRARY_USAGE.md say 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

  • I adjusted the README (if needed)
  • For new features in HeadsetControl: I discussed it beforehand in Issues or Discussions and adhered to the wiki Not applicable: this is a fix and adds no new capability.

@github-actions github-actions Bot added the awaiting-maintainer The author replied or pushed - our move label Oct 22, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T19:59:13.695410Z a0c9d42 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ChrisLauinger77
ChrisLauinger77 force-pushed the feat/lib-refined-product-name branch 2 times, most recently from 38c3418 to e0bf2c9 Compare October 22, 2026 15:36
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.
@ChrisLauinger77
ChrisLauinger77 force-pushed the feat/lib-refined-product-name branch from e0bf2c9 to f255d57 Compare October 22, 2026 15:37
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

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 Sapd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

Thanks, both addressed. The refined name now goes into a second, write-once string (ProductName in C++, refined_product_name_str in the C wrapper), so earlier views and pointers keep the USB string and never dangle, and the headers no longer warn about invalidation. The refinement is pulled out into ProductName::refine(HIDDevice&, hid_device*) and tested against a mock device for the once-only guard, the empty name and the error case.

@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: a0c9d42d41

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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 Sapd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@Sapd
Sapd merged commit b68f56b into Sapd:master Oct 22, 2026
7 of 8 checks passed
@github-actions github-actions Bot removed the awaiting-maintainer The author replied or pushed - our move label Oct 22, 2026
@ChrisLauinger77
ChrisLauinger77 deleted the feat/lib-refined-product-name branch October 22, 2026 20:05
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.

2 participants