Skip to content

Commit 22962fc

Browse files
authored
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
1 parent 9fbbef5 commit 22962fc

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/libnetdata/eval/eval-utils.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,8 @@ void print_parsed_as_constant(BUFFER *out, NETDATA_DOUBLE n) {
9393
return;
9494
}
9595

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);
10898
buffer_strcat(out, b);
10999
}
110100

src/libnetdata/libnetdata.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
6363

6464
// vsnprintfz() returns the number of bytes actually written - after possible truncation
6565
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+
}
6772

6873
int size = vsnprintf(dst, n, fmt, args);
6974
dst[n - 1] = '\0';

0 commit comments

Comments
 (0)