@cryptotaxi247 / netdata-1 / commits / 25a62da4f

trim-all (#20029)

* make trim_all() never return null * no need to memmove() after trim_all()

Costa Tsaousis committed Apr 1, 2025 at 23:28 UTC 25a62da4f16ee6799fd117d43507924cc2f3b909
5 files changed +18 -18
src/collectors/windows-events.plugin/windows-events-sources.c
+3 -2
@@ -580,8 +580,9 @@ void wevt_sources_scan(void) {
580 char buf[sizeof(WEVT_SOURCE_ALL_OF_PROVIDER_PREFIX) + strlen(provider)]; // sizeof() includes terminator
581 snprintf(buf, sizeof(buf), WEVT_SOURCE_ALL_OF_PROVIDER_PREFIX "%s", provider);
582
583 - if(trim_all(buf) != NULL) {
584 - for (size_t i = 0; i < sizeof(buf) - 1; i++) {
583 + trim_all(buf);
584 + if(buf[0]) {
585 + for (size_t i = 0; i < sizeof(buf) - 1 && buf[i]; i++) {
586 // remove character that may interfere with our parsing
587 if (isspace((uint8_t) buf[i]) || buf[i] == '%' || buf[i] == '+' || buf[i] == '|' || buf[i] == ':')
588 buf[i] = '_';
src/daemon/status-file.c
+7 -9
@@ -127,11 +127,7 @@ static void dmi_info(const char *file, const char *alt, char *dst, size_t dst_si
127 return;
128 }
129
130 - char *s = trim_all(dst);
131 - if(!s)
132 - dst[0] = '\0';
133 - else if(s != dst)
134 - memmove(dst, s, strlen(s) + 1);
130 + trim_all(dst);
131
132 struct {
133 const char *found;
@@ -145,10 +141,12 @@ static void dmi_info(const char *file, const char *alt, char *dst, size_t dst_si
141 {"System UUID", ""},
142 };
143
148 - for(size_t i = 0; i < _countof(replacements) ;i++) {
149 - if(strcasecmp(dst, replacements[i].found) == 0) {
150 - strncpyz(dst, replacements[i].replace, dst_size - 1);
151 - break;
144 + if(dst[0]) {
145 + for (size_t i = 0; i < _countof(replacements); i++) {
146 + if (strcasecmp(dst, replacements[i].found) == 0) {
147 + strncpyz(dst, replacements[i].replace, dst_size - 1);
148 + break;
149 + }
150 }
151 }
152 }
src/health/health_config.c
+2 -2
@@ -622,7 +622,7 @@ int health_readfile(const char *filename, void *data __maybe_unused, bool stock_
622 key = trim_all(key);
623 value = trim_all(value);
624
625 - if(!key) {
625 + if(!key || !*key) {
626 netdata_log_error(
627 "Health configuration has invalid line %zu of file '%s'. Keyword is empty. Ignoring it.",
628 line, filename);
@@ -630,7 +630,7 @@ int health_readfile(const char *filename, void *data __maybe_unused, bool stock_
630 continue;
631 }
632
633 - if(!value) {
633 + if(!value || !*value) {
634 netdata_log_error(
635 "Health configuration has invalid line %zu of file '%s'. value is empty. Ignoring it.",
636 line, filename);
src/libnetdata/inlined.h
+5 -4
@@ -687,16 +687,18 @@ static inline char *trim(char *s) {
687 return s;
688 }
689
690 -// like trim(), but also remove duplicate spaces inside the string; may return NULL
690 +// like trim(), but also remove duplicate spaces inside the string
691 static inline char *trim_all(char *buffer) {
692 char *d = buffer, *s = buffer;
693
694 // skip spaces
695 - while(isspace((uint8_t)*s)) s++;
695 + while(isspace((uint8_t)*s))
696 + s++;
697
698 while(*s) {
699 // copy the non-space part
699 - while(*s && !isspace((uint8_t)*s)) *d++ = *s++;
700 + while(*s && !isspace((uint8_t)*s))
701 + *d++ = *s++;
702
703 // add a space if we have to
704 if(*s && isspace((uint8_t)*s)) {
@@ -715,7 +717,6 @@ static inline char *trim_all(char *buffer) {
717 if(isspace((uint8_t)*d)) *d = '\0';
718 }
719
718 - if(!buffer[0]) return NULL;
720 return buffer;
721 }
722
src/libnetdata/parsers/timeframe.c
+1 -1
@@ -12,7 +12,7 @@ TIMEFRAME timeframe_parse(const char *txt) {
12 char buf[strlen(txt) + 1];
13 memcpy(buf, txt, strlen(txt) + 1);
14 char *s = trim_all(buf);
15 -if(!s)
15 +if(!s || !*s)
16 return TIMEFRAME_INVALID;
17
18 while(isspace(*s)) s++;