Improve print parsed (#21790)
* 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
Stelios Fragkakis committed
Feb 20, 2026 at 09:59 UTC
22962fcffaad907440346954bbe4d81c7e44ce04
2 files changed
+8
-13
src/libnetdata/eval/eval-utils.c
+2
-12
@@ -93,18 +93,8 @@ void print_parsed_as_constant(BUFFER *out, NETDATA_DOUBLE n) {
93
return;
94
}
95
96
- char b[100+1], *s;
97
- snprintfz(b, sizeof(b) - 1, NETDATA_DOUBLE_FORMAT, n);
98
-
99
- s = &b[strlen(b) - 1];
100
- while(s > b && *s == '0') {
101
- *s ='\0';
102
- s--;
103
- }
104
-
105
- if(s > b && *s == '.')
106
- *s = '\0';
107
-
96
+ char b[DOUBLE_MAX_LENGTH];
97
+ print_netdata_double(b, n);
98
buffer_strcat(out, b);
99
}
100
src/libnetdata/libnetdata.c
+6
-1
@@ -63,7 +63,12 @@ char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
63
64
// vsnprintfz() returns the number of bytes actually written - after possible truncation
65
int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
66
- if(unlikely(!n)) return 0;
66
+ if(unlikely(!dst || !n)) return 0;
67
+
68
+ if(unlikely(!fmt)) {
69
+ dst[0] = '\0';
70
+ return 0;
71
+ }
72
73
int size = vsnprintf(dst, n, fmt, args);
74
dst[n - 1] = '\0';