Skip to content

field-cache: define string ownership - #136

Merged
mikeller merged 1 commit into
subsurface:Subsurface-DS9from
mikeller:fix_import_crash
Sep 12, 2026
Merged

field-cache: define string ownership#136
mikeller merged 1 commit into
subsurface:Subsurface-DS9from
mikeller:fix_import_crash

Conversation

@mikeller

Copy link
Copy Markdown
Member

DC_FIELD_STRING callers have long owned the returned value, but the field cache returned an alias of its own allocation. Freeing cached strings during parser destruction consequently double-freed values already released by consumers.

Return a duplicate to callers, free cache-owned originals when cache-backed parsers are reset or destroyed, and release strings in dctool after writing them. Document the caller-owned API contract.

Fixes a crash introduced in #130.

DC_FIELD_STRING callers have long owned the returned value, but the field cache returned an alias of its own allocation. Freeing cached strings during parser destruction consequently double-freed values already released by consumers.

Return a duplicate to callers, free cache-owned originals when cache-backed parsers are reset or destroyed, and release strings in dctool after writing them. Document the caller-owned API contract.

Signed-off-by: Michael Keller <github@ike.ch>
Copilot AI lite review requested due to automatic review settings September 11, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The only remaining feedback is a minor documentation nit, not an approval blocker.

Pull request overview

Defines ownership for cached DC_FIELD_STRING values to prevent double frees and leaks.

Changes:

  • Return caller-owned string duplicates.
  • Clean up cache allocations during parser reset/destruction.
  • Document ownership and free returned values in the XML example.
File summaries
File Changes
src/field-cache.c Duplicates cached strings and cleans up cache allocations.
include/libdivecomputer/parser.h Documents caller-owned values; clarify that only value must be freed.
examples/output_xml.c Frees returned string values after use.
Review details

Suppressed comments (1)

include/libdivecomputer/parser.h:365

  • dc_field_string_t has both desc and value, but only value is duplicated for the caller; desc remains a static/cache-owned pointer (src/field-cache.c:11-16, 61-62). This wording can lead callers to free str.desc as well and trigger an invalid free. Please state explicitly that only dc_field_string_t.value must be freed.
/* DC_FIELD_STRING values are caller-owned and must be freed after use. */
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mikeller
mikeller merged commit 145fb69 into subsurface:Subsurface-DS9 Sep 12, 2026
10 checks passed
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