Browse Source

coverity fixes about statsd; removal of strsame (#13049)

Costa Tsaousis 2 years ago
parent
commit
81832edce2
3 changed files with 38 additions and 29 deletions
  1. 2 2
      collectors/cgroups.plugin/sys_fs_cgroup.c
  2. 36 14
      collectors/statsd.plugin/statsd.c
  3. 0 13
      libnetdata/inlined.h

+ 2 - 2
collectors/cgroups.plugin/sys_fs_cgroup.c

@@ -3659,7 +3659,7 @@ static inline void update_cpu_limits2(struct cgroup *cg) {
         cg->cpuset_cpus = get_system_cpus();
 
         char *s = "max\n\0";
-        if(strsame(s, procfile_lineword(ff, 0, 0)) == 0){
+        if(strcmp(s, procfile_lineword(ff, 0, 0)) == 0){
             cg->cpu_cfs_quota = cg->cpu_cfs_period * cg->cpuset_cpus;
         } else {
             cg->cpu_cfs_quota = str2ull(procfile_lineword(ff, 0, 0));
@@ -3707,7 +3707,7 @@ static inline int update_memory_limits(char **filename, RRDSETVAR **chart_var, u
                     return 0;
                 }
                 char *s = "max\n\0";
-                if(strsame(s, buffer) == 0){
+                if(strcmp(s, buffer) == 0){
                     *value = UINT64_MAX;
                     rrdsetvar_custom_chart_variable_set(*chart_var, (calculated_number)(*value / (1024 * 1024)));
                     return 1;

+ 36 - 14
collectors/statsd.plugin/statsd.c

@@ -774,10 +774,9 @@ static void statsd_process_metric(const char *name, const char *value, const cha
             statsd_parse_field_trim(tagkey, tagkey_end);
             statsd_parse_field_trim(tagvalue, tagvalue_end);
 
-            if(tagkey && tagkey && tagvalue && *tagvalue) {
-                if (!m->units && strcmp(tagkey, "units") == 0) {
+            if(tagkey && *tagkey && tagvalue && *tagvalue) {
+                if (!m->units && strcmp(tagkey, "units") == 0)
                     m->units = strdupz(tagvalue);
-                }
 
                 if (!m->dimname && strcmp(tagkey, "name") == 0)
                     m->dimname = strdupz(tagvalue);
@@ -1546,23 +1545,46 @@ static inline void statsd_readdir(const char *user_path, const char *stock_path,
 
 // extract chart type and chart id from metric name
 static inline void statsd_get_metric_type_and_id(STATSD_METRIC *m, char *type, char *id, char *context, const char *metrictype, size_t len) {
-    char *s = NULL;
 
-    snprintfz(type, len, "%s_%s", STATSD_CHART_PREFIX, m->name);
-    if(sizeof(STATSD_CHART_PREFIX) + 2 < len)
-        for(s = &type[sizeof(STATSD_CHART_PREFIX) + 2]; *s ;s++)
-            if(unlikely(*s == '.' || *s == '_')) break;
+    // The full chart type.id looks like this:
+    // ${STATSD_CHART_PREFIX} + "_" + ${METRIC_NAME} + "_" + ${METRIC_TYPE}
+    //
+    // where:
+    // STATSD_CHART_PREFIX = "statsd" as defined above
+    // METRIC_NAME = whatever the user gave to statsd
+    // METRIC_TYPE = "gauge", "counter", "meter", "timer", "histogram", "set", "dictionary"
+
+    // for chart type, we want:
+    // ${STATSD_CHART_PREFIX} + "_" + the first word of ${METRIC_NAME}
+
+    // find the first word of ${METRIC_NAME}
+    char firstword[len + 1], *s = "";
+    strncpyz(firstword, m->name, len);
+    for (s = firstword; *s ; s++) {
+        if (unlikely(*s == '.' || *s == '_')) {
+            *s = '\0';
+            s++;
+            break;
+        }
+    }
+    // firstword has the first word of ${METRIC_NAME}
+    // s has the remaining, if any
+
+    // create the chart type:
+    snprintfz(type, len, STATSD_CHART_PREFIX "_%s", firstword);
 
-    if(s && (*s == '.' || *s == '_')) {
-        *s++ = '\0';
+    // for chart id, we want:
+    // the remaining of the words of ${METRIC_NAME} + "_" + ${METRIC_TYPE}
+    // or the ${METRIC_NAME} has no remaining words, the ${METRIC_TYPE} alone
+    if(*s)
         snprintfz(id, len, "%s_%s", s, metrictype);
-    }
-    else {
+    else
         snprintfz(id, len, "%s", metrictype);
-    }
 
-    snprintfz(context, RRD_ID_LENGTH_MAX, "statsd_%s.%s", metrictype, m->name);
+    // for the context, we want the full of both the above, separated with a dot (type.id):
+    snprintfz(context, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
 
+    // make sure they don't have illegal characters
     netdata_fix_chart_id(type);
     netdata_fix_chart_id(id);
     netdata_fix_chart_id(context);

+ 0 - 13
libnetdata/inlined.h

@@ -222,19 +222,6 @@ static inline long double str2ld(const char *s, char **endptr) {
     }
 }
 
-#ifdef NETDATA_STRCMP_OVERRIDE
-#ifdef strcmp
-#undef strcmp
-#endif
-#define strcmp(a, b) strsame(a, b)
-#endif // NETDATA_STRCMP_OVERRIDE
-
-static inline int strsame(const char *a, const char *b) {
-    if(unlikely(a == b)) return 0;
-    while(*a && *a == *b) { a++; b++; }
-    return *a - *b;
-}
-
 static inline char *strncpyz(char *dst, const char *src, size_t n) {
     char *p = dst;