Skip to content

output_xml: add SAMPLE_EVENT_STRING to events table and guard array access - #135

Merged
mikeller merged 2 commits into
subsurface:Subsurface-DS9from
mikeller:fix/output-xml-event-string-oob
Sep 12, 2026
Merged

output_xml: add SAMPLE_EVENT_STRING to events table and guard array access#135
mikeller merged 2 commits into
subsurface:Subsurface-DS9from
mikeller:fix/output-xml-event-string-oob

Conversation

@mikeller

Copy link
Copy Markdown
Member

SAMPLE_EVENT_STRING (value 26) was added to parser_sample_event_t after
SAMPLE_EVENT_GASCHANGE2 (value 25), but the events[] string table in
sample_cb() had only 26 entries (indices 0-25). Accessing events[26]
is undefined behaviour and aborts under UBSan.

Three changes:

  • Append "string" at index 26 so the table covers all currently defined
    event types.
  • Add a bounds guard (sizeof(events)/sizeof(events[0])) before indexing
    events[]. Out-of-range types fall back to "unknown" so future additions
    do not crash before the table is updated.
  • When the event type is SAMPLE_EVENT_STRING and value->event.name is
    non-NULL, use the dynamic name string rather than the static "string"
    label. This preserves the semantic intent of the type (free-form
    annotations such as compass heading and scrubber state from hw_ostc
    and Garmin parsers).

Reproduces with: hw_ostc5-0001.bin, garmin_descent_mk1-0001.bin.

Copilot AI lite review requested due to automatic review settings September 10, 2026 20:27

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.

🟡 Changes recommended

The new use of value->event.name is written into XML without escaping, which can produce malformed XML when the name contains characters like & or <.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes an out-of-bounds access in the XML sample event string table by extending the table for the newly added SAMPLE_EVENT_STRING type and by guarding event-table indexing to avoid UB for future enum additions. It also improves XML output semantics by using value->event.name as the label for SAMPLE_EVENT_STRING when available.

Changes:

  • Extend the events[] lookup table to include index 26 for SAMPLE_EVENT_STRING.
  • Add bounds checking before indexing events[], falling back to "unknown" for out-of-range event types.
  • Prefer value->event.name for SAMPLE_EVENT_STRING events when non-NULL.
File summaries
File Description
examples/output_xml.c Fixes event string table OOB, guards future indexing, and emits dynamic names for string events.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

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

Comment thread examples/output_xml.c Outdated
…ccess

SAMPLE_EVENT_STRING (value 26) was added to parser_sample_event_t after
SAMPLE_EVENT_GASCHANGE2 (value 25), but the events[] string table in
sample_cb() had only 26 entries (indices 0-25). Accessing events[26]
is undefined behaviour and aborts under UBSan.

Three changes:
- Append "string" at index 26 so the table covers all currently defined
  event types.
- Add a bounds guard (sizeof(events)/sizeof(events[0])) before indexing
  events[]. Out-of-range types fall back to "unknown" so future additions
  do not crash before the table is updated.
- When the event type is SAMPLE_EVENT_STRING and value->event.name is
  non-NULL, use the dynamic name string rather than the static "string"
  label. This preserves the semantic intent of the type (free-form
  annotations such as compass heading and scrubber state from hw_ostc
  and Garmin parsers).

Reproduces with: hw_ostc5-0001.bin, garmin_descent_mk1-0001.bin.

Signed-off-by: Michael Keller <github@ike.ch>
@mikeller
mikeller force-pushed the fix/output-xml-event-string-oob branch from 405f98f to 2aea4df Compare September 10, 2026 20:33
Copilot AI review requested due to automatic review settings September 10, 2026 20:33

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.

🟡 Changes recommended

The new XML escaping helper uses fputc(*s, ...) without unsigned-char casting, which can trigger undefined behavior for non-ASCII/UTF-8 bytes, and there’s an unnecessary “AI-generated” attribution comment to clean up.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread examples/output_xml.c Outdated
Comment thread examples/output_xml.c
fputc() requires its int argument to be representable as unsigned char
or EOF (C17 §7.21.7.3p2).  Passing a plain char directly is UB on
signed-char platforms when the value is > 127.

Cast *s to (unsigned char) at the call site to make the argument
well-defined on all platforms.

Signed-off-by: Michael Keller <github@ike.ch>
Copilot AI review requested due to automatic review settings September 10, 2026 22:35

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

No unresolved review issues remain.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@mikeller
mikeller merged commit 16058f9 into subsurface:Subsurface-DS9 Sep 12, 2026
10 checks passed
@mikeller
mikeller deleted the fix/output-xml-event-string-oob branch September 12, 2026 05:51
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