← Back to Pull requests

refactor: split decodeDiagnostics and decodeFeature #246

Closed
unknown closed this pull request without merging on Nov 16, 2026 13:23 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 10:56 UTC
Author

Why

maat flags decodeDiagnostics (CC 17) and decodeFeature (CC 12) in decoder.go. This is item #4 of the DD-29 assessment. This PR splits them. The highest CC goes from 17 to 12 (decodeDiagnosticsEntry), and every other function has CC ≤ 10. The decoded output, the error texts and the allocations do not change.

What changed

Commit 1 test: pin decoder behavior before the refactor adds tests that pass on the old decoder.go, and they pass unchanged on the new one:

  • decoder_test.go (new): 2 golden tests (every key goes to the right field, unknown keys are ignored), 2 error tables with exact error texts (14 + 9 cases), and BenchmarkDecodeDiagnostics. Two cases need a map with size 1 and a nil pointer. The public setters cannot build it, so the test writes the size field with unsafe (offset 2 of the C ddwaf_object). It passes under -race.

  • decoder_unknown_field_test.go: comment fix only. The old text said that decodeFeature returns an error on an unknown field. It does not.
  • Commit 2 refactor: split decodeDiagnostics and decodeFeature changes decoder.go only. Commit 3 refactor: use one switch for the diagnostics keys merges the 2 diagnostics switch helpers into 1 (review feedback). Result:

    | Function | CC | Role |
    |---|---|---|
    | mapEntries (new) | 3 | Shared IsMap + MapEntries guard, same error texts |
    | decodeDiagnostics | 5 (was 17) | Loop, key decode, error wrap |
    | decodeDiagnosticsEntry (new) | 12 | One switch on the 10 feature keys + ruleset_version, unknown keys ignored |
    | decodeFeature | 2 (was 12) | mapEntries, then decodeFeatureEntries |
    | decodeFeatureEntries (new) | 10 | The old loop and 6-case switch, unchanged |

    No exported symbol changes. No new type, package variable or function value.

    Why a switch and not a table

    A first design used a map of setters and one helper for each feature field. Measured on this benchmark, it made the decode about 15 % slower. The map lookup and indirect call cost most of it (+14.5 %). One extra call for each of the 50 feature fields cost +2.2 %. This is why the code uses switch statements with direct calls only, and a loop by index. Please do not add a table or a per-field helper without a new benchmark.

    Performance

    BenchmarkDecodeDiagnostics, -count=10, -benchmem. The "before" and "after" test binaries ran one after the other, in turns, so machine load affects both in the same way.

    Final head (3 commits) vs main's decoder (the commit 1 binary):

    DecodeDiagnostics-18   sec/op     1.313µ ± 12%  →  1.267µ ± 9%    ~ (p=0.393 n=10)
    DecodeDiagnostics-18   B/op       2.234Ki ± 0%  →  2.242Ki ± 1%   ~ (p=0.450 n=10)
    DecodeDiagnostics-18   allocs/op  102.0 ± 0%    →  102.0 ± 0%     ~ (p=1.000 n=10)
    

    Commit 3 (1 switch) vs commit 2 (2 switch helpers):
    DecodeDiagnostics-18   sec/op     1.207µ ± 4%   →  1.194µ ± 4%    ~ (p=0.725 n=10)
    DecodeDiagnostics-18   B/op       2.234Ki ± 0%  →  2.234Ki ± 0%   ~ (p=1.000 n=10)
    DecodeDiagnostics-18   allocs/op  102.0 ± 0%    →  102.0 ± 0%     ~ (p=1.000 n=10)
    

    Before commit 3, commit 2 vs main's decoder also had no significant difference in 2 full runs (sec/op ~, p=0.393 and p=0.529).

    No significant difference in sec/op, B/op or allocs/op.

    go build -gcflags=-m .: the only moved to heap line in decoder.go is feature (decoder.go:129). It is the same as on main.

    maat

    maat analyze ./... --detail full --output json, main → this PR:

    | Scope | Before | After |
    |---|---|---|
    | Overall | 90 (base_score 89.54) | 90 (base_score 89.80) |
    | Root module github.com/DataDog/go-libddwaf/v5 | 79 (81.65) | 80 (82.19) |
    | decoder.go | 87 | 98 |
    | decoder_test.go | – | 100, 0 hits |

    The 8 in-scope rule hits of main are gone:

  • decodeDiagnostics (line 49): MAAT-CC-001 (CC 17), MAAT-FUNC-002 (CC 17), MAAT-FUNC-003 (38 SLOC), MAAT-SLOC-001 (38 SLOC), MAAT-MI-001 (MI 42.8, error).

  • decodeFeature (line 100): MAAT-CC-001 (CC 12), MAAT-FUNC-002 (CC 12), MAAT-MI-001 (MI 47.4, error).
  • 4 warnings remain in decoder.go (9 hits → 4, and no error). 3 are in scope, and 1 is out of scope and does not change:

  • MAAT-CC-001 warning on decodeDiagnosticsEntry (CC 12, threshold 10), and MAAT-MI-001 warning on the same function (MI 50.4, threshold 53). They come from the single switch with 11 cases. This is a choice made after review: 1 switch is easier to read than 2. decodeDiagnostics stays at CC 5.

  • MAAT-MI-001 warning on decodeFeatureEntries (MI 50.1). The old decodeFeature had an error here (MI 47.4). To remove the hit, the switch must leave the loop, which needs one call for each field (+2.6 % to +6.4 % time, measured). Romain chose to keep the hit and have no time regression.

  • MAAT-MI-001 warning on decodeStringArray (MI 52.4). It is out of scope for DD-33 and does not change.
  • Notes


  • golangci-lint run ./...: 0 issues.

  • Follow-up: DD-49 looks at a key accessor that does not allocate. In this benchmark, WAFObject.StringValue makes 66 % of the allocated objects, and it needs a change outside decoder.go.
  • Diff: 644 lines (+483 / -161) across 8 files Reviewer: eliottness Review duration: 4466m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 644 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.387
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #246"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.0% (MEDIUM) · Higher = larger blast radius
    52.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%