@cryptotaxi247 / netdata-1 / commits / a84213ca3

fix various issues identified by coverity (#16294)

Costa Tsaousis committed Oct 28, 2023 at 16:20 UTC a84213ca315ea3cedba7d6197bcb087a1515704c
7 files changed +48 -31
collectors/plugins.d/pluginsd_parser.c
+8 -6
@@ -175,11 +175,13 @@ static inline void pluginsd_rrddim_put_to_slot(PARSER *parser, RRDSET *st, RRDDI
175 size_t wanted_size = st->pluginsd.size;
176
177 if(slot >= 1) {
178 - st->pluginsd.with_slots = true;
178 + st->pluginsd.dims_with_slots = true;
179 wanted_size = slot;
180 }
181 - else
181 + else {
182 + st->pluginsd.dims_with_slots = false;
183 wanted_size = dictionary_entries(st->rrddim_root_index);
184 + }
185
186 if(wanted_size > st->pluginsd.size) {
187 st->pluginsd.prd_array = reallocz(st->pluginsd.prd_array, wanted_size * sizeof(struct pluginsd_rrddim));
@@ -194,7 +196,7 @@ static inline void pluginsd_rrddim_put_to_slot(PARSER *parser, RRDSET *st, RRDDI
196 st->pluginsd.size = wanted_size;
197 }
198
197 - if(st->pluginsd.with_slots) {
199 + if(st->pluginsd.dims_with_slots) {
200 struct pluginsd_rrddim *prd = &st->pluginsd.prd_array[slot - 1];
201
202 if(prd->rd != rd) {
@@ -224,7 +226,7 @@ static inline RRDDIM *pluginsd_acquire_dimension(RRDHOST *host, RRDSET *st, cons
226 struct pluginsd_rrddim *prd;
227 RRDDIM *rd;
228
227 - if(likely(st->pluginsd.with_slots)) {
229 + if(likely(st->pluginsd.dims_with_slots)) {
230 // caching with slots
231
232 if(unlikely(slot < 1 || slot > st->pluginsd.size)) {
@@ -276,7 +278,7 @@ static inline RRDDIM *pluginsd_acquire_dimension(RRDHOST *host, RRDSET *st, cons
278 // the cached one is not good for us
279 rrddim_acquired_release(prd->rda);
280 prd->rda = NULL;
279 - prd->rd = rd = NULL;
281 + prd->rd = NULL;
282 prd->id = NULL;
283 }
284 }
@@ -2175,7 +2177,7 @@ static inline PARSER_RC pluginsd_end_v2(char **words __maybe_unused, size_t num_
2177 // ------------------------------------------------------------------------
2178 // cleanup RRDSET / RRDDIM
2179
2178 - if(likely(st->pluginsd.with_slots)) {
2180 + if(likely(st->pluginsd.dims_with_slots)) {
2181 for(size_t i = 0; i < st->pluginsd.size ;i++) {
2182 RRDDIM *rd = st->pluginsd.prd_array[i].rd;
2183
collectors/systemd-journal.plugin/systemd-journal.c
+9 -4
@@ -647,7 +647,7 @@ static bool netdata_systemd_filtering_by_journal(sd_journal *j, FACETS *facets,
647 size_t failures = 0;
648 size_t filters_added = 0;
649
650 - SD_JOURNAL_FOREACH_FIELD(j, field) {
650 + SD_JOURNAL_FOREACH_FIELD(j, field) { // for each key
651 bool interesting;
652
653 if(fqs->data_only)
@@ -660,7 +660,7 @@ static bool netdata_systemd_filtering_by_journal(sd_journal *j, FACETS *facets,
660 bool added_this_key = false;
661 size_t added_values = 0;
662
663 - SD_JOURNAL_FOREACH_UNIQUE(j, data, data_length) {
663 + SD_JOURNAL_FOREACH_UNIQUE(j, data, data_length) { // for each value of the key
664 const char *key, *value;
665 size_t key_length, value_length;
666
@@ -673,19 +673,24 @@ static bool netdata_systemd_filtering_by_journal(sd_journal *j, FACETS *facets,
673 continue;
674
675 if(added_keys && !added_this_key) {
676 - if(sd_journal_add_conjunction(j) < 0)
676 + if(sd_journal_add_conjunction(j) < 0) // key AND key AND key
677 failures++;
678
679 added_this_key = true;
680 added_keys++;
681 }
682 else if(added_values)
683 - if(sd_journal_add_disjunction(j) < 0)
683 + if(sd_journal_add_disjunction(j) < 0) // value OR value OR value
684 failures++;
685
686 if(sd_journal_add_match(j, data, data_length) < 0)
687 failures++;
688
689 + if(!added_keys) {
690 + added_keys++;
691 + added_this_key = true;
692 + }
693 +
694 added_values++;
695 filters_added++;
696 }
database/rrd.h
+1 -1
@@ -865,7 +865,7 @@ struct rrdset {
865 struct {
866 SPINLOCK spinlock; // used only for cleanup
867 pid_t collector_tid;
868 - bool with_slots;
868 + bool dims_with_slots;
869 bool set;
870 uint32_t pos;
871 int32_t last_slot;
database/rrdset.c
+2 -2
@@ -79,7 +79,7 @@ void rrdset_pluginsd_receive_unslot(RRDSET *st) {
79 }
80
81 st->pluginsd.last_slot = -1;
82 - st->pluginsd.with_slots = false;
82 + st->pluginsd.dims_with_slots = false;
83 }
84
85 void rrdset_pluginsd_receive_unslot_and_cleanup(RRDSET *st) {
@@ -96,7 +96,7 @@ void rrdset_pluginsd_receive_unslot_and_cleanup(RRDSET *st) {
96 st->pluginsd.pos = 0;
97 st->pluginsd.set = false;
98 st->pluginsd.last_slot = -1;
99 - st->pluginsd.with_slots = false;
99 + st->pluginsd.dims_with_slots = false;
100 st->pluginsd.collector_tid = 0;
101
102 spinlock_unlock(&st->pluginsd.spinlock);
streaming/compression.c
+16 -12
@@ -275,7 +275,7 @@ size_t rrdpush_compress(struct compressor_state *state, const char *data, size_t
275 }
276
277 if(unlikely(ret >= COMPRESSION_MAX_CHUNK)) {
278 - netdata_log_error("RRDPUSH_COMPRESS: compressed data is %zu bytes, which is >= than the max chunk size %zu",
278 + netdata_log_error("RRDPUSH_COMPRESS: compressed data is %zu bytes, which is >= than the max chunk size %d",
279 ret, COMPRESSION_MAX_CHUNK);
280 return 0;
281 }
@@ -384,7 +384,7 @@ size_t rrdpush_decompress(struct decompressor_state *state, const char *compress
384 // for backwards compatibility we cannot check for COMPRESSION_MAX_MSG_SIZE,
385 // because old children may send this big payloads.
386 if(unlikely(ret > COMPRESSION_MAX_CHUNK)) {
387 - netdata_log_error("RRDPUSH_DECOMPRESS: decompressed data is %zu bytes, which is bigger than the max msg size %zu",
387 + netdata_log_error("RRDPUSH_DECOMPRESS: decompressed data is %zu bytes, which is bigger than the max msg size %d",
388 ret, COMPRESSION_MAX_CHUNK);
389 return 0;
390 }
@@ -395,17 +395,21 @@ size_t rrdpush_decompress(struct decompressor_state *state, const char *compress
395 // ----------------------------------------------------------------------------
396 // unit test
397
398 +static inline long int my_random (void) {
399 + return random();
400 +}
401 +
402 void unittest_generate_random_name(char *dst, size_t size) {
403 if(size < 7)
404 size = 7;
405
402 - size_t len = 5 + random() % (size - 6);
406 + size_t len = 5 + my_random() % (size - 6);
407
408 for(size_t i = 0; i < len ; i++) {
405 - if(random() % 2 == 0)
406 - dst[i] = 'A' + random() % 26;
409 + if(my_random() % 2 == 0)
410 + dst[i] = 'A' + my_random() % 26;
411 else
408 - dst[i] = 'a' + random() % 26;
412 + dst[i] = 'a' + my_random() % 26;
413 }
414
415 dst[len] = '\0';
@@ -419,9 +423,9 @@ void unittest_generate_message(BUFFER *wb, time_t now_s, size_t counter) {
423 time_t point_end_time_s = now_s;
424 time_t wall_clock_time_s = now_s;
425 size_t chart_slot = counter + 1;
422 - size_t dimensions = 2 + random() % 5;
426 + size_t dimensions = 2 + my_random() % 5;
427 char chart[RRD_ID_LENGTH_MAX + 1] = "name";
424 - unittest_generate_random_name(chart, 5 + random() % 30);
428 + unittest_generate_random_name(chart, 5 + my_random() % 30);
429
430 buffer_fast_strcat(wb, PLUGINSD_KEYWORD_BEGIN_V2, sizeof(PLUGINSD_KEYWORD_BEGIN_V2) - 1);
431
@@ -447,10 +451,10 @@ void unittest_generate_message(BUFFER *wb, time_t now_s, size_t counter) {
451 for(size_t d = 0; d < dimensions ;d++) {
452 size_t dim_slot = d + 1;
453 char dim_id[RRD_ID_LENGTH_MAX + 1] = "dimension";
450 - unittest_generate_random_name(dim_id, 10 + random() % 20);
451 - int64_t last_collected_value = (random() % 2 == 0) ? (int64_t)(counter + d) : (int64_t)random();
452 - NETDATA_DOUBLE value = (random() % 2 == 0) ? (NETDATA_DOUBLE)random() / ((NETDATA_DOUBLE)random() + 1) : (NETDATA_DOUBLE)last_collected_value;
453 - SN_FLAGS flags = (random() % 1000 == 0) ? SN_FLAG_NONE : SN_FLAG_NOT_ANOMALOUS;
454 + unittest_generate_random_name(dim_id, 10 + my_random() % 20);
455 + int64_t last_collected_value = (my_random() % 2 == 0) ? (int64_t)(counter + d) : (int64_t)my_random();
456 + NETDATA_DOUBLE value = (my_random() % 2 == 0) ? (NETDATA_DOUBLE)my_random() / ((NETDATA_DOUBLE)my_random() + 1) : (NETDATA_DOUBLE)last_collected_value;
457 + SN_FLAGS flags = (my_random() % 1000 == 0) ? SN_FLAG_NONE : SN_FLAG_NOT_ANOMALOUS;
458
459 buffer_fast_strcat(wb, PLUGINSD_KEYWORD_SET_V2, sizeof(PLUGINSD_KEYWORD_SET_V2) - 1);
460
streaming/compression_brotli.c
+2 -2
@@ -46,7 +46,7 @@ size_t rrdpush_compress_brotli(struct compressor_state *state, const char *data,
46 }
47
48 if(available_in != 0) {
49 - netdata_log_error("STREAM: BrotliEncoderCompressStream() did not use all the input buffer, %u bytes out of %zu remain",
49 + netdata_log_error("STREAM: BrotliEncoderCompressStream() did not use all the input buffer, %zu bytes out of %zu remain",
50 available_in, size);
51 return 0;
52 }
@@ -109,7 +109,7 @@ size_t rrdpush_decompress_brotli(struct decompressor_state *state, const char *c
109 }
110
111 if(available_in != 0) {
112 - netdata_log_error("STREAM: BrotliDecoderDecompressStream() did not use all the input buffer, %u bytes out of %zu remain",
112 + netdata_log_error("STREAM: BrotliDecoderDecompressStream() did not use all the input buffer, %zu bytes out of %zu remain",
113 available_in, compressed_size);
114 return 0;
115 }
streaming/compression_gzip.c
+10 -4
@@ -34,7 +34,7 @@ void rrdpush_compressor_init_gzip(struct compressor_state *state) {
34 void rrdpush_compressor_destroy_gzip(struct compressor_state *state) {
35 if (state->stream) {
36 deflateEnd(state->stream);
37 - free(state->stream);
37 + freez(state->stream);
38 state->stream = NULL;
39 }
40 }
@@ -92,12 +92,18 @@ void rrdpush_decompressor_init_gzip(struct decompressor_state *state) {
92 state->initialized = true;
93
94 // Initialize inflate stream
95 - z_stream *strm = state->stream = (z_stream *) malloc(sizeof(z_stream));
95 + z_stream *strm = state->stream = (z_stream *)mallocz(sizeof(z_stream));
96 strm->zalloc = Z_NULL;
97 strm->zfree = Z_NULL;
98 strm->opaque = Z_NULL;
99
100 - inflateInit2(strm, 15 + 16);
100 + int r = inflateInit2(strm, 15 + 16);
101 + if (r != Z_OK) {
102 + netdata_log_error("Failed to initialize inflateInit2() with error: %d", r);
103 + freez(state->stream);
104 + state->initialized = false;
105 + return;
106 + }
107
108 simple_ring_buffer_make_room(&state->output, COMPRESSION_MAX_CHUNK);
109 }
@@ -106,7 +112,7 @@ void rrdpush_decompressor_init_gzip(struct decompressor_state *state) {
112 void rrdpush_decompressor_destroy_gzip(struct decompressor_state *state) {
113 if (state->stream) {
114 inflateEnd(state->stream);
109 - free(state->stream);
115 + freez(state->stream);
116 state->stream = NULL;
117 }
118 }