coverity fixes about statsd; removal of strsame (#13049)
Costa Tsaousis committed
Jun 1, 2022 at 23:38 UTC
81832edce2b2c0cb8a76ae1d69b99086c974005c
3 files changed
+38
-29
collectors/cgroups.plugin/sys_fs_cgroup.c
+2
-2
@@ -3659,7 +3659,7 @@ static inline void update_cpu_limits2(struct cgroup *cg) {
3659
cg->cpuset_cpus = get_system_cpus();
3660
3661
char *s = "max\n\0";
3662
- if(strsame(s, procfile_lineword(ff, 0, 0)) == 0){
3662
+ if(strcmp(s, procfile_lineword(ff, 0, 0)) == 0){
3663
cg->cpu_cfs_quota = cg->cpu_cfs_period * cg->cpuset_cpus;
3664
} else {
3665
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
3707
return 0;
3708
}
3709
char *s = "max\n\0";
3710
- if(strsame(s, buffer) == 0){
3710
+ if(strcmp(s, buffer) == 0){
3711
*value = UINT64_MAX;
3712
rrdsetvar_custom_chart_variable_set(*chart_var, (calculated_number)(*value / (1024 * 1024)));
3713
return 1;
collectors/statsd.plugin/statsd.c
+36
-14
@@ -774,10 +774,9 @@ static void statsd_process_metric(const char *name, const char *value, const cha
774
statsd_parse_field_trim(tagkey, tagkey_end);
775
statsd_parse_field_trim(tagvalue, tagvalue_end);
776
777
- if(tagkey && tagkey && tagvalue && *tagvalue) {
778
- if (!m->units && strcmp(tagkey, "units") == 0) {
777
+ if(tagkey && *tagkey && tagvalue && *tagvalue) {
778
+ if (!m->units && strcmp(tagkey, "units") == 0)
779
m->units = strdupz(tagvalue);
780
- }
780
781
if (!m->dimname && strcmp(tagkey, "name") == 0)
782
m->dimname = strdupz(tagvalue);
@@ -1546,23 +1545,46 @@ static inline void statsd_readdir(const char *user_path, const char *stock_path,
1545
1546
// extract chart type and chart id from metric name
1547
static inline void statsd_get_metric_type_and_id(STATSD_METRIC *m, char *type, char *id, char *context, const char *metrictype, size_t len) {
1549
- char *s = NULL;
1548
1551
- snprintfz(type, len, "%s_%s", STATSD_CHART_PREFIX, m->name);
1552
- if(sizeof(STATSD_CHART_PREFIX) + 2 < len)
1553
- for(s = &type[sizeof(STATSD_CHART_PREFIX) + 2]; *s ;s++)
1554
- if(unlikely(*s == '.' || *s == '_')) break;
1549
+ // The full chart type.id looks like this:
1550
+ // ${STATSD_CHART_PREFIX} + "_" + ${METRIC_NAME} + "_" + ${METRIC_TYPE}
1551
+ //
1552
+ // where:
1553
+ // STATSD_CHART_PREFIX = "statsd" as defined above
1554
+ // METRIC_NAME = whatever the user gave to statsd
1555
+ // METRIC_TYPE = "gauge", "counter", "meter", "timer", "histogram", "set", "dictionary"
1556
+
1557
+ // for chart type, we want:
1558
+ // ${STATSD_CHART_PREFIX} + "_" + the first word of ${METRIC_NAME}
1559
+
1560
+ // find the first word of ${METRIC_NAME}
1561
+ char firstword[len + 1], *s = "";
1562
+ strncpyz(firstword, m->name, len);
1563
+ for (s = firstword; *s ; s++) {
1564
+ if (unlikely(*s == '.' || *s == '_')) {
1565
+ *s = '\0';
1566
+ s++;
1567
+ break;
1568
+ }
1569
+ }
1570
+ // firstword has the first word of ${METRIC_NAME}
1571
+ // s has the remaining, if any
1572
+
1573
+ // create the chart type:
1574
+ snprintfz(type, len, STATSD_CHART_PREFIX "_%s", firstword);
1575
1556
- if(s && (*s == '.' || *s == '_')) {
1557
- *s++ = '\0';
1576
+ // for chart id, we want:
1577
+ // the remaining of the words of ${METRIC_NAME} + "_" + ${METRIC_TYPE}
1578
+ // or the ${METRIC_NAME} has no remaining words, the ${METRIC_TYPE} alone
1579
+ if(*s)
1580
snprintfz(id, len, "%s_%s", s, metrictype);
1559
- }
1560
- else {
1581
+ else
1582
snprintfz(id, len, "%s", metrictype);
1562
- }
1583
1564
- snprintfz(context, RRD_ID_LENGTH_MAX, "statsd_%s.%s", metrictype, m->name);
1584
+ // for the context, we want the full of both the above, separated with a dot (type.id):
1585
+ snprintfz(context, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
1586
1587
+ // make sure they don't have illegal characters
1588
netdata_fix_chart_id(type);
1589
netdata_fix_chart_id(id);
1590
netdata_fix_chart_id(context);
libnetdata/inlined.h
-13
@@ -222,19 +222,6 @@ static inline long double str2ld(const char *s, char **endptr) {
222
}
223
}
224
225
-#ifdef NETDATA_STRCMP_OVERRIDE
226
-#ifdef strcmp
227
-#undef strcmp
228
-#endif
229
-#define strcmp(a, b) strsame(a, b)
230
-#endif // NETDATA_STRCMP_OVERRIDE
231
-
232
-static inline int strsame(const char *a, const char *b) {
233
- if(unlikely(a == b)) return 0;
234
- while(*a && *a == *b) { a++; b++; }
235
- return *a - *b;
236
-}
237
-
225
static inline char *strncpyz(char *dst, const char *src, size_t n) {
226
char *p = dst;
227