Skip to content

Fix coverity issues - #22835

Merged
stelfrag merged 3 commits into
netdata:masterfrom
stelfrag:coverity-c-bugfixes
Jun 30, 2026
Merged

Fix coverity issues#22835
stelfrag merged 3 commits into
netdata:masterfrom
stelfrag:coverity-c-bugfixes

Conversation

@stelfrag

@stelfrag stelfrag commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator
Summary
  • stream-receiver: guard rpt->host in disconnect log field; it can be NULL on early handshake-failure removal (consistent with the host checks below) [CID 503806]
  • cgroup_ebpfgo_cachestat: drop redundant path_buf null-checks; the sole caller passes a stack array, so the checks (not the derefs) were the defect [CID 503817]
  • ml: catch dlib exceptions from ml_kmeans_train so a numerical fatal_error skips the model for one round (clearing training_in_progress) instead of terminating the training thread or leaving the dimension stuck [CID 503827]

Summary by cubic

Fixes three Coverity findings to improve stability: guards a potential null dereference in stream receiver logs, treats ML KMeans numerical errors as transient to avoid aborts and stuck state, and removes redundant checks in cgroup cache stat.

  • Bug Fixes
    • stream-receiver: Guard rpt->host in disconnect logs to avoid null deref on early handshake failures.
    • cgroup_ebpfgo_cachestat: Remove redundant path_buf null checks; caller always passes a stack buffer.
    • ML: Catch dlib::error from ml_kmeans_train, log it, clear training_in_progress, and skip the model for this round; let non-dlib exceptions propagate.

Written for commit dc8034e. Summary will update on new commits.

Review in cubic

…ion on numerical edge cases.

- Simplify `snprintfz` logic by removing redundant null checks in cgroup cache stat handling.
- Add null check in stream receiver logging to safeguard against early handshake failures.
@github-actions github-actions Bot added area/collectors Everything related to data collection area/streaming area/ml Machine Learning Related Issues collectors/cgroups labels Jun 23, 2026

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 3 files

Confidence score: 2/5

  • In src/ml/ml.cc, the catch path does not reset dim->training_in_progress under slock, so any exception can leave that dimension permanently stuck as “training,” blocking all future training attempts for that dimension after merge — add the same state cleanup used by other early-return paths in the catch block before merging.
Architecture diagram
sequenceDiagram
    participant SR as Stream Receiver
    participant Log as Log System
    participant ML as ML Training Worker
    participant Dlib as Dlib KMeans
    participant CG as Cgroup Ebpf Cachestat
    participant Proc as /proc Filesystem

    Note over SR,Proc: Runtime interaction flows for Coverity fixes

    SR->>SR: stream_receiver_remove_internal()
    alt rpt->host is NULL (early handshake failure)
        SR->>Log: ND_LOG_FIELD_STR(NDF_NIDL_NODE, NULL)
        Note over SR,Log: CHANGED: Guarded dereference - passes NULL instead of crashing
    else rpt->host exists
        SR->>Log: ND_LOG_FIELD_STR(NDF_NIDL_NODE, rpt->host->hostname)
    end
    SR->>Log: ND_LOG_FIELD_TXT(NDF_SRC_IP, rpt->remote_ip)
    SR->>Log: ND_LOG_FIELD_TXT(NDF_SRC_PORT, rpt->remote_port)

    Note over ML,Dlib: ML training dimension flow

    ML->>ML: ml_dimension_train_model()
    ML->>Dlib: ml_kmeans_init()
    ML->>Dlib: ml_kmeans_train()
    alt Training succeeds
        Dlib-->>ML: model trained
        ML->>ML: update models
    else Dlib throws exception (numerical edge case)
        Dlib-->>ML: std::exception (e.g. dlib::fatal_error)
        Note over ML: CHANGED: Catch exception, log error, skip model for round
        ML->>ML: return ML_WORKER_RESULT_NOT_ENOUGH_COLLECTED_VALUES
    end

    Note over CG,Proc: Cgroup ebpf cachestat flow

    CG->>CG: cgroup_ebpfgo_open_nonempty_procs_file(path_buf, ...)
    Note over CG: path_buf is always a stack array (caller guarantee)
    CG->>Proc: try to open /proc files for cgroup
    alt Non-empty proc file found
        Proc-->>CG: procfile read
        CG->>CG: snprintfz(best_path, ..., path_buf)
        Note over CG: CHANGED: Removed redundant null checks on path_buf
        CG->>CG: snprintfz(path_buf, ..., best_path)
    else Empty or no file
        Proc-->>CG: null or empty
        CG->>CG: continue iteration
    end
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/ml/ml.cc Outdated
@stelfrag
stelfrag marked this pull request as ready for review June 29, 2026 21:12

@thiagoftsm thiagoftsm 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.

PR is working as expected. LGTM!

@stelfrag
stelfrag merged commit 0b0ed42 into netdata:master Jun 30, 2026
159 checks passed
@stelfrag
stelfrag deleted the coverity-c-bugfixes branch June 30, 2026 05:22
@sonarqubecloud

Copy link
Copy Markdown

stelfrag added a commit to stelfrag/netdata that referenced this pull request Jul 12, 2026
Backport of netdata#22835 to 2.10.x, excluding the cgroup_ebpfgo_cachestat.c
snprintf hunk (that file does not exist in this branch).

- ml.cc: guard ml_kmeans_train() with try/catch so a dlib exception on
  numerical edge cases skips the dimension instead of terminating netdata.
- stream-receiver.c: null-check rpt->host in the log stack on early
  handshake failure.
@stelfrag stelfrag mentioned this pull request Jul 13, 2026
Ferroin pushed a commit that referenced this pull request Jul 15, 2026
Backport of #22835 to 2.10.x, excluding the cgroup_ebpfgo_cachestat.c
snprintf hunk (that file does not exist in this branch).

- ml.cc: guard ml_kmeans_train() with try/catch so a dlib exception on
  numerical edge cases skips the dimension instead of terminating netdata.
- stream-receiver.c: null-check rpt->host in the log stack on early
  handshake failure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/collectors Everything related to data collection area/ml Machine Learning Related Issues area/streaming collectors/cgroups

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants