Skip to content

Improve UUID handling in SQLite functions and error reporting - #22233

Merged
stelfrag merged 5 commits into
netdata:masterfrom
stelfrag:fix_verify_uuid_blobs_from_db
Apr 20, 2026
Merged

Improve UUID handling in SQLite functions and error reporting#22233
stelfrag merged 5 commits into
netdata:masterfrom
stelfrag:fix_verify_uuid_blobs_from_db

Conversation

@stelfrag

@stelfrag stelfrag commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator
Summary
  • Add validation for uuids while loading from db

Summary by cubic

Validates and safely reads UUID blobs from SQLite across health, context, ACLK, and metadata. Adds strict checks for alert transition/config UUIDs and chart/dimension IDs, skipping bad rows and logging clear errors (with host GUID where helpful).

  • New Features

    • Added sqlite3_column_uuid_ptr, sqlite3_column_uuid_copy, and sqlite3_column_uuid_unparse_lower for safe UUID retrieval and formatting.
  • Bug Fixes

    • Replaced direct BLOB casts with validation in health logs/exports; alert transitions and configuration loads (host_id, config_hash_id, transition_id, machine GUID); context chart/dimension lists (errors include host GUID); and metadata node ID load, cleanup loops, and metric population. Invalid rows are skipped, with totals logged where relevant.
    • sqlite3_uuid_unparse_strdupz now returns empty strings for invalid UUIDs and logs the issue to prevent bad data propagation.

Written for commit 6a8780b. Summary will update on new commits.

@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 5 files

Confidence score: 4/5

  • This PR is likely safe to merge, but there is a mild logic-risk issue (severity 4/10, high confidence) that keeps it from a full low-risk score.
  • In src/database/sqlite/sqlite_health.c, sql_find_alert_transition() may return false even after valid rows were processed, because later invalid rows can reset ok to false, which could suppress expected transition detection.
  • Pay close attention to src/database/sqlite/sqlite_health.c - ensure ok is not overwritten by invalid-row handling after a successful match.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/database/sqlite/sqlite_health.c">

<violation number="1" location="src/database/sqlite/sqlite_health.c:1391">
P2: `sql_find_alert_transition()` can incorrectly return false after successfully processing valid rows because invalid rows reset `ok` back to false.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant App as Subsystems (Health, Context, Metadata)
    participant Help as NEW: SQLite UUID Helpers
    participant SQL as SQLite Statement (Result Row)
    participant Log as Error Reporting

    Note over App, SQL: Data Retrieval Loop
    
    App->>SQL: sqlite3_step()
    SQL-->>App: SQLITE_ROW
    
    rect rgb(240, 240, 240)
        Note over App, Help: NEW: Validation Flow
        App->>Help: sqlite3_column_uuid_copy / ptr / unparse
        Help->>SQL: sqlite3_column_type(iCol)
        SQL-->>Help: column type (BLOB vs other)
        
        alt Column is BLOB
            Help->>SQL: sqlite3_column_bytes(iCol)
            SQL-->>Help: size in bytes
            
            alt Size == sizeof(nd_uuid_t)
                Help->>SQL: sqlite3_column_blob(iCol)
                SQL-->>Help: raw pointer
                Help-->>App: SUCCESS: (Valid UUID data)
            else Invalid Size or NULL
                Help-->>App: FAILURE: NULL / false
            end
        else Not a BLOB
            Help-->>App: FAILURE: NULL / false
        end
    end

    alt SUCCESS
        App->>App: Process row with valid UUID
    else FAILURE (NEW Handling)
        App->>Log: NEW: error_report(Invalid UUID)
        Note right of App: CHANGED: Skip row or return empty string
    end
    
    App->>SQL: Continue to next row
Loading

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/database/sqlite/sqlite_health.c Outdated
@stelfrag
stelfrag marked this pull request as ready for review April 20, 2026 10:22
Copilot AI review requested due to automatic review settings April 20, 2026 10:22
@stelfrag
stelfrag marked this pull request as draft April 20, 2026 10:22

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.

Pull request overview

This PR hardens UUID handling when reading UUIDs stored as SQLite BLOBs, replacing direct sqlite3_column_blob() casts with validated helpers to prevent crashes/undefined behavior from malformed rows. It centralizes safe UUID extraction/copying/unparsing and updates several SQLite-backed flows (metadata, health, contexts, ACLK alerts) to skip invalid UUID rows while emitting clearer errors.

Changes:

  • Introduces sqlite3_column_uuid_ptr(), sqlite3_column_uuid_copy(), and sqlite3_column_uuid_unparse_lower() helpers for validated UUID BLOB access.
  • Updates metadata cleanup/loading paths to copy/validate UUIDs before passing them to callbacks and other logic.
  • Updates health/context/ACLK alert code paths to validate UUIDs, skip malformed rows, and improve error reporting.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/database/sqlite/sqlite_functions.h Adds centralized, validated UUID helpers for SQLite column access/copy/unparse.
src/database/sqlite/sqlite_metadata.c Switches multiple UUID reads from raw BLOB casts to validated pointer/copy helpers.
src/database/sqlite/sqlite_health.c Validates UUID columns (transition/config IDs) during load/export/lookups; skips invalid rows with logs.
src/database/sqlite/sqlite_context.c Validates chart/dimension UUIDs when building context lists and skips invalid rows safely.
src/database/sqlite/sqlite_aclk_alert.c Makes UUID-to-string conversion robust by validating blobs and returning empty strings on invalid values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/database/sqlite/sqlite_metadata.c Outdated

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Improves robustness of UUID handling when reading from SQLite by introducing centralized helpers that validate UUID BLOB sizes/types before copying or formatting, and updates multiple SQLite call sites to skip malformed UUID rows with clearer logging.

Changes:

  • Added sqlite3_column_uuid_ptr, sqlite3_column_uuid_copy, and sqlite3_column_uuid_unparse_lower helpers for validating/reading UUID BLOB columns.
  • Replaced direct sqlite3_column_blob UUID casts across health, context, ACLK alert, and metadata paths with validated copy/unparse logic.
  • Improved error reporting and prevented propagation of invalid UUIDs by skipping malformed rows.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/database/sqlite/sqlite_metadata.c Uses validated UUID copy for node-id load, cleanup loops, dimension cleanup, and metric population.
src/database/sqlite/sqlite_health.c Validates UUIDs during health log load/export and transitions processing; adds targeted error logs and invariants.
src/database/sqlite/sqlite_functions.h Introduces reusable UUID extraction/copy/unparse helpers for SQLite statements.
src/database/sqlite/sqlite_context.c Adds UUID validation for chart/dimension list retrieval and logs invalid IDs.
src/database/sqlite/sqlite_aclk_alert.c Makes UUID stringification resilient to invalid UUID blobs by returning empty strings with an error log.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/database/sqlite/sqlite_health.c
Comment thread src/database/sqlite/sqlite_health.c
Comment thread src/database/sqlite/sqlite_context.c Outdated
Comment thread src/database/sqlite/sqlite_functions.h

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.

Pull request overview

This PR improves robustness of UUID handling when reading SQLite result columns by introducing centralized UUID validation/copy/unparse helpers and applying them across SQLite health, context, ACLK alert, and metadata code paths to safely skip malformed rows and emit clearer error logs.

Changes:

  • Added sqlite3_column_uuid_ptr(), sqlite3_column_uuid_copy(), and sqlite3_column_uuid_unparse_lower() helpers for strict UUID BLOB validation and safe use.
  • Replaced direct sqlite3_column_blob() UUID casts with validated UUID reads in health log load/export, alert transitions/config loading, context chart/dimension listing, and metadata cleanup/metric population.
  • Improved error reporting and resilience by skipping invalid rows and logging aggregated counts where relevant.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/database/sqlite/sqlite_functions.h Adds reusable UUID column validation/copy/unparse helpers for SQLite statements.
src/database/sqlite/sqlite_health.c Validates UUID columns for health log load/export and alert transition/config queries; skips malformed rows with clearer logs.
src/database/sqlite/sqlite_context.c Validates chart/dimension UUIDs when building context lists; logs invalid IDs with host GUID context.
src/database/sqlite/sqlite_metadata.c Validates UUID reads for node_id load, cleanup loops, chart dimension cleanup, and metric prepopulation.
src/database/sqlite/sqlite_aclk_alert.c Makes UUID string formatting resilient to malformed UUID blobs (returns empty string + log).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@stelfrag
stelfrag marked this pull request as ready for review April 20, 2026 12:20

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

After few hours running, everything is working as expected. LGTM!

@stelfrag
stelfrag merged commit ea07b4c into netdata:master Apr 20, 2026
153 of 155 checks passed
@stelfrag
stelfrag deleted the fix_verify_uuid_blobs_from_db branch April 20, 2026 20:48
nedi-app Bot pushed a commit that referenced this pull request Apr 24, 2026
* fix: improve UUID handling in SQLite functions and error reporting

* fix: ensure valid machine GUID handling in alert transition lookup

* fix: correct UUID handling in SQLite metadata retrieval

* fix: enhance UUID validation and error reporting in alert transition and configuration loading

* fix: improve error reporting for invalid chart and dimension IDs with host GUID context
stelfrag added a commit to stelfrag/netdata that referenced this pull request Jun 22, 2026
…a#22233)

* fix: improve UUID handling in SQLite functions and error reporting

* fix: ensure valid machine GUID handling in alert transition lookup

* fix: correct UUID handling in SQLite metadata retrieval

* fix: enhance UUID validation and error reporting in alert transition and configuration loading

* fix: improve error reporting for invalid chart and dimension IDs with host GUID context

(cherry picked from commit ea07b4c)
@stelfrag stelfrag mentioned this pull request Jun 22, 2026
Ferroin pushed a commit that referenced this pull request Jul 15, 2026
* fix: improve UUID handling in SQLite functions and error reporting

* fix: ensure valid machine GUID handling in alert transition lookup

* fix: correct UUID handling in SQLite metadata retrieval

* fix: enhance UUID validation and error reporting in alert transition and configuration loading

* fix: improve error reporting for invalid chart and dimension IDs with host GUID context

(cherry picked from commit ea07b4c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants