Skip to content

fix: prevent memory leak in otel schema_version decode branch#458

Open
jbachorik wants to merge 1 commit intomainfrom
fix/prof-14142-otel-schema-version-leak
Open

fix: prevent memory leak in otel schema_version decode branch#458
jbachorik wants to merge 1 commit intomainfrom
fix/prof-14142-otel-schema-version-leak

Conversation

@jbachorik
Copy link
Copy Markdown
Collaborator

@jbachorik jbachorik commented Apr 10, 2026

What does this PR do?:
Guards the threadlocal.schema_version decode branch in otel_process_ctx_decode_payload against leaking the existing thread_ctx_config allocation. When data_out->thread_ctx_config is already non-NULL, the fix reuses the existing struct instead of allocating a new one and silently losing the old pointer. It also frees the old schema_version value before assigning a new one (handles duplicate keys in a single payload).

Motivation:
The schema_version branch at lines 808-812 always executed calloc + unconditional assign, meaning every re-decode (or any payload that contained both threadlocal.attribute_key_map and threadlocal.schema_version) would leak the previously allocated otel_thread_ctx_config_data struct. The attribute_key_map branch directly above it already used the correct null-guard pattern; this fix mirrors that pattern for schema_version.

Additional Notes:
The fix is symmetric with the existing attribute_key_map branch (lines ~790-800) which already checked !data_out->thread_ctx_config before allocating. No new allocation strategy is introduced — the same calloc + null-guard pattern is applied consistently.

How to test the change?:
Acceptance test scenarios (manual or via a C unit test harness):

  • Decode a payload containing threadlocal.schema_version twice — second decode must not leak the first allocation
  • Decode a payload with attribute_key_map first, then schema_versionthread_ctx_config must be allocated exactly once
  • Decode a payload with schema_version first, then attribute_key_map — same single-allocation requirement
  • Run under ASan/Valgrind with a payload that exercises both keys; expect zero leak reports

For Datadog employees:

  • This PR doesn't touch any of that.
  • JIRA: PROF-14142

When thread_ctx_config is already allocated, reuse the existing struct
instead of allocating a new one and leaking the old pointer. Also free
the old schema_version value before reassignment if it was already set.

Fixes: PROF-14142
@jbachorik jbachorik added the AI label Apr 10, 2026
@jbachorik jbachorik marked this pull request as ready for review April 10, 2026 06:30
@jbachorik jbachorik requested a review from a team as a code owner April 10, 2026 06:30
@jbachorik jbachorik requested a review from zhengyu123 April 10, 2026 06:49
@dd-octo-sts
Copy link
Copy Markdown

dd-octo-sts bot commented Apr 10, 2026

CI Test Results

Run: #24229769402 | Commit: e389034 | Duration: 26m 59s (longest job)

All 32 test jobs passed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - - -
8-ibm - - -
8-j9 - -
8-librca - -
8-orcl - - -
11 - - -
11-j9 - -
11-librca - -
17 - -
17-graal - -
17-j9 - -
17-librca - -
21 - -
21-graal - -
21-librca - -
25 - -
25-graal - -
25-librca - -

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Summary: Total: 32 | Passed: 32 | Failed: 0


Updated: 2026-04-10 06:59:16 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant