@@ -46,9 +46,23 @@ static char *cmt_metric_name_from_labels(Prometheus__TimeSeries *ts)
4646 int i ;
4747 int count ;
4848
49+ if (ts == NULL || ts -> labels == NULL ) {
50+ return NULL ;
51+ }
52+
4953 count = ts -> n_labels ;
5054 for (i = 0 ; i < count ; i ++ ) {
55+ if (ts -> labels [i ] == NULL ||
56+ ts -> labels [i ]-> name == NULL ||
57+ ts -> labels [i ]-> name [0 ] == '\0' ) {
58+ continue ;
59+ }
60+
5161 if (strncmp ("__name__" , ts -> labels [i ]-> name , 8 ) == 0 ) {
62+ if (ts -> labels [i ]-> value == NULL ) {
63+ return NULL ;
64+ }
65+
5266 return strdup (ts -> labels [i ]-> value );
5367 }
5468 }
@@ -60,26 +74,28 @@ static struct cmt_map_label *create_map_label(char *caption, size_t length)
6074{
6175 struct cmt_map_label * map_label ;
6276
77+ if (caption == NULL ) {
78+ return NULL ;
79+ }
80+
6381 map_label = calloc (1 , sizeof (struct cmt_map_label ));
6482 if (!map_label ) {
6583 return NULL ;
6684 }
6785
6886 if (map_label != NULL ) {
69- if (caption != NULL ) {
70- if (length == 0 ) {
71- length = strlen (caption );
72- }
87+ if (length == 0 ) {
88+ length = strlen (caption );
89+ }
7390
74- map_label -> name = cfl_sds_create_len (caption , length );
91+ map_label -> name = cfl_sds_create_len (caption , length );
7592
76- if (map_label -> name == NULL ) {
77- cmt_errno ();
93+ if (map_label -> name == NULL ) {
94+ cmt_errno ();
7895
79- free (map_label );
96+ free (map_label );
8097
81- map_label = NULL ;
82- }
98+ map_label = NULL ;
8399 }
84100 }
85101
@@ -156,14 +172,19 @@ static int decode_labels(struct cmt *cmt,
156172 prom_label_index ++ ) {
157173
158174 label = labels [prom_label_index ];
175+ if (label == NULL || label -> name == NULL || label -> name [0 ] == '\0' ) {
176+ result = CMT_DECODE_PROMETHEUS_REMOTE_WRITE_INVALID_ARGUMENT_ERROR ;
177+ break ;
178+ }
159179
160180 label_found = CMT_FALSE ;
161181 label_index = 0 ;
162182
163183 cfl_list_foreach (label_iterator , & map -> label_keys ) {
164184 current_label = cfl_list_entry (label_iterator , struct cmt_map_label , _head );
165185
166- if (strcmp (current_label -> name , label -> name ) == 0 ) {
186+ if (current_label -> name != NULL &&
187+ strcmp (current_label -> name , label -> name ) == 0 ) {
167188 label_found = CMT_TRUE ;
168189
169190 break ;
@@ -190,7 +211,12 @@ static int decode_labels(struct cmt *cmt,
190211
191212 if (value_index_list [map_label_index ] != NULL ) {
192213 label = (Prometheus__Label * ) value_index_list [map_label_index ];
193- result = append_new_metric_label_value (metric , label -> value , 0 );
214+ result = append_new_metric_label_value (metric ,
215+ label -> value != NULL ? label -> value : "" ,
216+ 0 );
217+ }
218+ else {
219+ result = append_new_metric_label_value (metric , "" , 0 );
194220 }
195221 }
196222
@@ -426,6 +452,22 @@ static int decode_histogram_points(struct cmt *cmt,
426452 map -> metric_static_set = CMT_TRUE ;
427453 }
428454
455+ if (metric -> hist_buckets == NULL ) {
456+ metric -> hist_buckets = calloc (1 , sizeof (uint64_t ) *
457+ (histogram -> buckets -> count + 1 ));
458+ if (metric -> hist_buckets == NULL ) {
459+ if (static_metric_detected == CMT_FALSE ) {
460+ destroy_label_list (& metric -> labels );
461+
462+ cfl_list_del (& metric -> _head );
463+
464+ free (metric );
465+ }
466+
467+ return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_ALLOCATION_ERROR ;
468+ }
469+ }
470+
429471 if (result == CMT_DECODE_PROMETHEUS_REMOTE_WRITE_SUCCESS ) {
430472 if (hist -> n_negative_spans > 0 ) {
431473 for (i = 0 ; i < hist -> n_negative_counts ; i ++ ) {
@@ -542,17 +584,22 @@ static int decode_metrics_entry(struct cmt *cmt,
542584 ts_count = write -> n_timeseries ;
543585 for (i = 0 ; i < ts_count ; i ++ ) {
544586 ts = write -> timeseries [i ];
587+ if (ts == NULL ) {
588+ return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_DECODE_ERROR ;
589+ }
590+
545591 meta_count = write -> n_metadata ;
546592 hist_count = ts -> n_histograms ;
547- if (meta_count > 0 ) {
593+ metadata = NULL ;
594+ if (meta_count > i ) {
548595 metadata = write -> metadata [i ];
549596 }
550- if (metadata == NULL ) {
551- type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__GAUGE ;
597+ if (hist_count > 0 ) {
598+ type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__HISTOGRAM ;
552599 metric_description = "-" ;
553600 }
554- else if (hist_count > 0 ) {
555- type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__HISTOGRAM ;
601+ else if (metadata == NULL ) {
602+ type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__GAUGE ;
556603 metric_description = "-" ;
557604 }
558605 else {
@@ -635,14 +682,17 @@ static int decode_metrics_entry(struct cmt *cmt,
635682 metric_subsystem ,
636683 metric_name ,
637684 metric_description ,
638- ( struct cmt_histogram_buckets * ) cmt ,
685+ NULL ,
639686 0 , NULL );
640687
641688 if (instance == NULL ) {
642689 free (metric_name );
643690 return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_ALLOCATION_ERROR ;
644691 }
645692
693+ cmt_histogram_buckets_destroy (((struct cmt_histogram * ) instance )-> buckets );
694+ ((struct cmt_histogram * ) instance )-> buckets = NULL ;
695+
646696 result = decode_histogram_entry (cmt , instance , ts );
647697
648698 if (result ) {
0 commit comments