summaryrefslogtreecommitdiffstats
path: root/streaming
diff options
context:
space:
mode:
authorCosta Tsaousis <costa@netdata.cloud>2023-10-28 16:20:47 +0100
committerGitHub <noreply@github.com>2023-10-28 18:20:47 +0300
commita84213ca315ea3cedba7d6197bcb087a1515704c (patch)
treec7f88269a5180a1b8e800d83a9dc3047a1518de0 /streaming
parentd41a4912d45871c927022aab2a08af4610efd4ba (diff)
fix various issues identified by coverity (#16294)
Diffstat (limited to 'streaming')
-rw-r--r--streaming/compression.c28
-rw-r--r--streaming/compression_brotli.c4
-rw-r--r--streaming/compression_gzip.c14
3 files changed, 28 insertions, 18 deletions
diff --git a/streaming/compression.c b/streaming/compression.c
index 6fdce54735..a94c8a0a68 100644
--- a/streaming/compression.c
+++ b/streaming/compression.c
@@ -275,7 +275,7 @@ size_t rrdpush_compress(struct compressor_state *state, const char *data, size_t
}
if(unlikely(ret >= COMPRESSION_MAX_CHUNK)) {
- netdata_log_error("RRDPUSH_COMPRESS: compressed data is %zu bytes, which is >= than the max chunk size %zu",
+ netdata_log_error("RRDPUSH_COMPRESS: compressed data is %zu bytes, which is >= than the max chunk size %d",
ret, COMPRESSION_MAX_CHUNK);
return 0;
}
@@ -384,7 +384,7 @@ size_t rrdpush_decompress(struct decompressor_state *state, const char *compress
// for backwards compatibility we cannot check for COMPRESSION_MAX_MSG_SIZE,
// because old children may send this big payloads.
if(unlikely(ret > COMPRESSION_MAX_CHUNK)) {
- netdata_log_error("RRDPUSH_DECOMPRESS: decompressed data is %zu bytes, which is bigger than the max msg size %zu",
+ netdata_log_error("RRDPUSH_DECOMPRESS: decompressed data is %zu bytes, which is bigger than the max msg size %d",
ret, COMPRESSION_MAX_CHUNK);
return 0;
}
@@ -395,17 +395,21 @@ size_t rrdpush_decompress(struct decompressor_state *state, const char *compress
// ----------------------------------------------------------------------------
// unit test
+static inline long int my_random (void) {
+ return random();
+}
+
void unittest_generate_random_name(char *dst, size_t size) {
if(size < 7)
size = 7;
- size_t len = 5 + random() % (size - 6);
+ size_t len = 5 + my_random() % (size - 6);
for(size_t i = 0; i < len ; i++) {
- if(random() % 2 == 0)
- dst[i] = 'A' + random() % 26;
+ if(my_random() % 2 == 0)
+ dst[i] = 'A' + my_random() % 26;
else
- dst[i] = 'a' + random() % 26;
+ dst[i] = 'a' + my_random() % 26;
}
dst[len] = '\0';
@@ -419,9 +423,9 @@ void unittest_generate_message(BUFFER *wb, time_t now_s, size_t counter) {
time_t point_end_time_s = now_s;
time_t wall_clock_time_s = now_s;
size_t chart_slot = counter + 1;
- size_t dimensions = 2 + random() % 5;
+ size_t dimensions = 2 + my_random() % 5;
char chart[RRD_ID_LENGTH_MAX + 1] = "name";
- unittest_generate_random_name(chart, 5 + random() % 30);
+ unittest_generate_random_name(chart, 5 + my_random() % 30);
buffer_fast_strcat(wb, PLUGINSD_KEYWORD_BEGIN_V2, sizeof(PLUGINSD_KEYWORD_BEGIN_V2) - 1);
@@ -447,10 +451,10 @@ void unittest_generate_message(BUFFER *wb, time_t now_s, size_t counter) {
for(size_t d = 0; d < dimensions ;d++) {
size_t dim_slot = d + 1;
char dim_id[RRD_ID_LENGTH_MAX + 1] = "dimension";
- unittest_generate_random_name(dim_id, 10 + random() % 20);
- int64_t last_collected_value = (random() % 2 == 0) ? (int64_t)(counter + d) : (int64_t)random();
- NETDATA_DOUBLE value = (random() % 2 == 0) ? (NETDATA_DOUBLE)random() / ((NETDATA_DOUBLE)random() + 1) : (NETDATA_DOUBLE)last_collected_value;
- SN_FLAGS flags = (random() % 1000 == 0) ? SN_FLAG_NONE : SN_FLAG_NOT_ANOMALOUS;
+ unittest_generate_random_name(dim_id, 10 + my_random() % 20);
+ int64_t last_collected_value = (my_random() % 2 == 0) ? (int64_t)(counter + d) : (int64_t)my_random();
+ NETDATA_DOUBLE value = (my_random() % 2 == 0) ? (NETDATA_DOUBLE)my_random() / ((NETDATA_DOUBLE)my_random() + 1) : (NETDATA_DOUBLE)last_collected_value;
+ SN_FLAGS flags = (my_random() % 1000 == 0) ? SN_FLAG_NONE : SN_FLAG_NOT_ANOMALOUS;
buffer_fast_strcat(wb, PLUGINSD_KEYWORD_SET_V2, sizeof(PLUGINSD_KEYWORD_SET_V2) - 1);
diff --git a/streaming/compression_brotli.c b/streaming/compression_brotli.c
index 5f772d56e1..cf52f3bca4 100644
--- a/streaming/compression_brotli.c
+++ b/streaming/compression_brotli.c
@@ -46,7 +46,7 @@ size_t rrdpush_compress_brotli(struct compressor_state *state, const char *data,
}
if(available_in != 0) {
- netdata_log_error("STREAM: BrotliEncoderCompressStream() did not use all the input buffer, %u bytes out of %zu remain",
+ netdata_log_error("STREAM: BrotliEncoderCompressStream() did not use all the input buffer, %zu bytes out of %zu remain",
available_in, size);
return 0;
}
@@ -109,7 +109,7 @@ size_t rrdpush_decompress_brotli(struct decompressor_state *state, const char *c
}
if(available_in != 0) {
- netdata_log_error("STREAM: BrotliDecoderDecompressStream() did not use all the input buffer, %u bytes out of %zu remain",
+ netdata_log_error("STREAM: BrotliDecoderDecompressStream() did not use all the input buffer, %zu bytes out of %zu remain",
available_in, compressed_size);
return 0;
}
diff --git a/streaming/compression_gzip.c b/streaming/compression_gzip.c
index c76c4aad41..c4ef3af05e 100644
--- a/streaming/compression_gzip.c
+++ b/streaming/compression_gzip.c
@@ -34,7 +34,7 @@ void rrdpush_compressor_init_gzip(struct compressor_state *state) {
void rrdpush_compressor_destroy_gzip(struct compressor_state *state) {
if (state->stream) {
deflateEnd(state->stream);
- free(state->stream);
+ freez(state->stream);
state->stream = NULL;
}
}
@@ -92,12 +92,18 @@ void rrdpush_decompressor_init_gzip(struct decompressor_state *state) {
state->initialized = true;
// Initialize inflate stream
- z_stream *strm = state->stream = (z_stream *) malloc(sizeof(z_stream));
+ z_stream *strm = state->stream = (z_stream *)mallocz(sizeof(z_stream));
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
- inflateInit2(strm, 15 + 16);
+ int r = inflateInit2(strm, 15 + 16);
+ if (r != Z_OK) {
+ netdata_log_error("Failed to initialize inflateInit2() with error: %d", r);
+ freez(state->stream);
+ state->initialized = false;
+ return;
+ }
simple_ring_buffer_make_room(&state->output, COMPRESSION_MAX_CHUNK);
}
@@ -106,7 +112,7 @@ void rrdpush_decompressor_init_gzip(struct decompressor_state *state) {
void rrdpush_decompressor_destroy_gzip(struct decompressor_state *state) {
if (state->stream) {
inflateEnd(state->stream);
- free(state->stream);
+ freez(state->stream);
state->stream = NULL;
}
}