Skip to content

Improve print parsed#21790

Merged
stelfrag merged 3 commits intonetdata:masterfrom
stelfrag:improve_print_parsed
Feb 20, 2026
Merged

Improve print parsed#21790
stelfrag merged 3 commits intonetdata:masterfrom
stelfrag:improve_print_parsed

Conversation

@stelfrag
Copy link
Collaborator

@stelfrag stelfrag commented Feb 19, 2026

Summary
  • Improve print_parsed_as_constant (use existing print_netdata_double)
    Prevent possible crash
#7 vsnprintfz [0x5C348B40F483] (/src/libnetdata/libnetdata.c:68)
#8 snprintfz [0x5C348B40F483] (/src/libnetdata/libnetdata.c:81)
#9 print_parsed_as_constant [0x5C348B413A2E] (/src/libnetdata/eval/eval-utils.c:97)
#10 eval_variable [0x5C348B40799A] (/src/libnetdata/eval/eval-evaluate.c:22)
#11 eval_value [0x5C348B40799A] (/src/libnetdata/eval/eval-evaluate.c:46)
#12 eval_nop [0x5C348B40799A] (/src/libnetdata/eval/eval-evaluate.c:207)
#13 eval_node [0x5C348B404A32] (/src/libnetdata/eval/eval-evaluate.c:292)
#14 eval_value [0x5C348B404A32] (/src/libnetdata/eval/eval-evaluate.c:38)
#15 eval_less [0x5C348B404A32] (/src/libnetdata/eval/eval-evaluate.c:112)
#16 eval_node [0x5C348B402226] (/src/libnetdata/eval/eval-evaluate.c:292)
#17 eval_value [0x5C348B402226] (/src/libnetdata/eval/eval-evaluate.c:38)
#18 eval_and [0x5C348B402226] (/src/libnetdata/eval/eval-evaluate.c:72)
#19 eval_node [0x5C348AD66CCB] (/src/libnetdata/eval/eval-evaluate.c:292)
#20 expression_evaluate [0x5C348AD66CCB] (/src/libnetdata/eval/eval-evaluate.c:305)

Summary by cubic

Fixes a potential crash during expression evaluation by using the shared double formatter and adding safety checks in vsnprintfz. Also simplifies number formatting for consistency.

  • Bug Fixes
    • Use print_netdata_double in print_parsed_as_constant with DOUBLE_MAX_LENGTH to avoid manual trimming and buffer risks.
    • Add null checks for dst and fmt in vsnprintfz; zero-terminate dst when fmt is null.

Written for commit 1fe5c57. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Confidence score: 4/5

  • This PR looks safe to merge overall, with only a minor edge-case risk noted.
  • In src/libnetdata/libnetdata.c, if fmt is NULL the guard returns without null-terminating dst, which could leave callers reading stale/unterminated data after a failure.
  • Pay close attention to src/libnetdata/libnetdata.c - ensure dst is set to an empty string when fmt is NULL.
Prompt for AI agents (all 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/libnetdata/libnetdata.c">

<violation number="1" location="src/libnetdata/libnetdata.c:66">
P2: Guarding against NULL fmt returns without null-terminating dst, so callers may read stale/unterminated data after a failure. Ensure dst is set to an empty string when fmt is NULL.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Eval as Expression Evaluator
    participant Utils as Eval Utils
    participant Lib as libnetdata
    participant Buf as Buffer

    Note over Eval,Buf: Expression Evaluation Flow (e.g., eval_variable)

    Eval->>Utils: print_parsed_as_constant(out, value)
    
    rect rgb(23, 37, 84)
    Note right of Utils: NEW: Use standard double formatter
    Utils->>Lib: print_netdata_double(b, value)
    
    Lib->>Lib: vsnprintfz(dst, n, fmt, args)
    alt NEW: Input Validation
        Note right of Lib: Check if dst, fmt, or n is NULL/zero
        Lib-->>Lib: Return 0 early if invalid
    else Valid Inputs
        Lib->>Lib: vsnprintf(dst, n, fmt, args)
        Lib-->>Lib: Force null-termination: dst[n-1] = '\0'
    end
    
    Lib-->>Utils: Formatted string in stack buffer 'b'
    end

    Utils->>Buf: buffer_strcat(out, b)
    Buf-->>Utils: String appended
    Utils-->>Eval: Completion
Loading

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

Copy link
Contributor

Copilot AI left a comment

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 prevents a potential crash in expression evaluation by improving the robustness of numeric formatting functions.

Changes:

  • Added null pointer checks in vsnprintfz() to prevent crashes when called with invalid arguments
  • Refactored print_parsed_as_constant() to use the shared print_netdata_double() function with a larger buffer (DOUBLE_MAX_LENGTH = 512 bytes) instead of custom formatting with a 101-byte buffer

Reviewed changes

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

File Description
src/libnetdata/libnetdata.c Added null checks for dst, fmt, and n parameters in vsnprintfz() to prevent crashes
src/libnetdata/eval/eval-utils.c Replaced custom double formatting logic with print_netdata_double(), using DOUBLE_MAX_LENGTH buffer to handle very large numbers safely

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

@stelfrag stelfrag marked this pull request as ready for review February 19, 2026 22:23
Copy link
Contributor

@thiagoftsm thiagoftsm left a comment

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 locally. LGTM!

@stelfrag stelfrag merged commit 22962fc into netdata:master Feb 20, 2026
144 checks passed
@stelfrag stelfrag deleted the improve_print_parsed branch February 20, 2026 07:59
@stelfrag stelfrag mentioned this pull request Mar 16, 2026
stelfrag added a commit to stelfrag/netdata that referenced this pull request Mar 16, 2026
* Refactor double formatting in eval-utils.c: replace custom logic with `print_netdata_double` for clarity and maintainability.

* Add null checks for `dst` and `fmt` in `vsnprintfz` to prevent potential issues

* Add null check for `fmt` in `vsnprintfz` and ensure `dst` is properly initialized

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants