libpcp_web: fix double-free in pmwebapi_labelsetdup - #2687
Conversation
pmwebapi_labelsetdup() performs a shallow struct copy (*dup = *lp) and then selectively deep-copies json and labels — but only when nlabels > 0. Two problems: 1. When nlabels <= 0 (or json is NULL), the dup shares the json pointer with the source. When pmDiscoverInvokeLabelsCallBacks frees the source labelset after invoking callbacks, the stored duplicate retains the dangling pointer. The next label discovery cycle frees the old duplicate, double-freeing the json allocation. 2. The compound flag and hash pointer were never handled — if set, the dup shares the opaque hash with the source, again leading to a double-free via pmFreeLabelSets. Fix both by clearing compound/hash immediately after the struct copy and, for the nlabels <= 0 / NULL-json case, NULLing json, jsonlen and labels before returning. This matches what __pmDupLabelSets in libpcp already does correctly for the same cases. Resolves a pmproxy SIGABRT (double-free) in archive discovery: pmFreeLabelSets ← pmSeriesDiscoverLabels ← pmDiscoverInvokeLabelsCallBacks Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Fixes: performancecopilot#2686
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited) Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
Walkthrough
ChangesLabel set duplication
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/libpcp_web/src/util.c`:
- Around line 885-890: Reset dup->nlabels to 0 in the empty-duplicate branch of
the label duplication function before returning dup, alongside clearing json,
jsonlen, and labels. This must cover both lp->nlabels <= 0 and lp->json == NULL
cases so the returned duplicate cannot appear populated when labels is NULL.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)
Review profile: CHILL
Plan: Pro Plus
Run ID: bcecb745-92eb-4948-88cd-6d7a38a58af4
📒 Files selected for processing (1)
src/libpcp_web/src/util.c
When lp->json is NULL but lp->nlabels > 0, the shallow copy left dup->nlabels positive with dup->labels set to NULL. Reset nlabels to 0 so consumers cannot dereference a NULL labels pointer. Reported-by: coderabbitai Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
pmwebapi_labelsetdup() performs a shallow struct copy (*dup = *lp) and then selectively deep-copies json and labels — but only when nlabels > 0. Two problems:
When nlabels <= 0 (or json is NULL), the dup shares the json pointer with the source. When pmDiscoverInvokeLabelsCallBacks frees the source labelset after invoking callbacks, the stored duplicate retains the dangling pointer. The next label discovery cycle frees the old duplicate, double-freeing the json allocation.
The compound flag and hash pointer were never handled — if set, the dup shares the opaque hash with the source, again leading to a double-free via pmFreeLabelSets.
Fix both by clearing compound/hash immediately after the struct copy and, for the nlabels <= 0 / NULL-json case, NULLing json, jsonlen and labels before returning. This matches what __pmDupLabelSets in libpcp already does correctly for the same cases.
Resolves a pmproxy SIGABRT (double-free) in archive discovery:
pmFreeLabelSets ← pmSeriesDiscoverLabels ← pmDiscoverInvokeLabelsCallBacks
Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com
Fixes: #2686