3333#include <cmetrics/cmt_mpack_utils.h>
3434#include <cmetrics/cmt_atomic.h>
3535
36+ #include <limits.h>
3637
3738static int create_counter_instance (struct cmt_map * map )
3839{
@@ -330,6 +331,7 @@ static int unpack_label(mpack_reader_t *reader,
330331 size_t index ,
331332 struct cfl_list * target_label_list )
332333{
334+ mpack_tag_t tag ;
333335 struct cmt_map_label * new_label ;
334336 int result ;
335337
@@ -344,7 +346,27 @@ static int unpack_label(mpack_reader_t *reader,
344346 return CMT_DECODE_MSGPACK_ALLOCATION_ERROR ;
345347 }
346348
347- result = cmt_mpack_consume_string_tag (reader , & new_label -> name );
349+ tag = mpack_peek_tag (reader );
350+ if (mpack_ok != mpack_reader_error (reader )) {
351+ free (new_label );
352+
353+ return CMT_DECODE_MSGPACK_CORRUPT_INPUT_DATA_ERROR ;
354+ }
355+
356+ if (mpack_tag_type (& tag ) == mpack_type_nil ) {
357+ mpack_expect_nil (reader );
358+ if (mpack_ok != mpack_reader_error (reader )) {
359+ free (new_label );
360+
361+ return CMT_DECODE_MSGPACK_CORRUPT_INPUT_DATA_ERROR ;
362+ }
363+
364+ new_label -> name = NULL ;
365+ result = CMT_DECODE_MSGPACK_SUCCESS ;
366+ }
367+ else {
368+ result = cmt_mpack_consume_string_tag (reader , & new_label -> name );
369+ }
348370
349371 if (result != CMT_DECODE_MSGPACK_SUCCESS ) {
350372 free (new_label );
@@ -520,6 +542,11 @@ static int unpack_metric_value_type(mpack_reader_t *reader, size_t index, void *
520542 int result ;
521543 struct cmt_msgpack_decode_context * decode_context ;
522544
545+ if (NULL == reader ||
546+ NULL == context ) {
547+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
548+ }
549+
523550 decode_context = (struct cmt_msgpack_decode_context * ) context ;
524551
525552 result = cmt_mpack_consume_uint_tag (reader , & value );
@@ -529,6 +556,9 @@ static int unpack_metric_value_type(mpack_reader_t *reader, size_t index, void *
529556 value == CMT_METRIC_VALUE_DOUBLE ) {
530557 cmt_atomic_store (& decode_context -> metric -> value_type , value );
531558 }
559+ else {
560+ result = CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
561+ }
532562 }
533563
534564 return result ;
@@ -540,6 +570,11 @@ static int unpack_metric_value_int64(mpack_reader_t *reader, size_t index, void
540570 int result ;
541571 struct cmt_msgpack_decode_context * decode_context ;
542572
573+ if (NULL == reader ||
574+ NULL == context ) {
575+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
576+ }
577+
543578 decode_context = (struct cmt_msgpack_decode_context * ) context ;
544579 result = cmt_mpack_consume_int_tag (reader , & value );
545580 if (result == CMT_DECODE_MSGPACK_SUCCESS ) {
@@ -558,6 +593,11 @@ static int unpack_metric_value_uint64(mpack_reader_t *reader, size_t index, void
558593 int result ;
559594 struct cmt_msgpack_decode_context * decode_context ;
560595
596+ if (NULL == reader ||
597+ NULL == context ) {
598+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
599+ }
600+
561601 decode_context = (struct cmt_msgpack_decode_context * ) context ;
562602 result = cmt_mpack_consume_uint_tag (reader , & value );
563603 if (result == CMT_DECODE_MSGPACK_SUCCESS ) {
@@ -863,9 +903,18 @@ static int unpack_exp_histogram_scale(mpack_reader_t *reader, size_t index, void
863903 int64_t value ;
864904 int result ;
865905
906+ if (NULL == reader ||
907+ NULL == context ) {
908+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
909+ }
910+
866911 decode_context = (struct cmt_msgpack_decode_context * ) context ;
867912 result = cmt_mpack_consume_int_tag (reader , & value );
868913 if (result == CMT_DECODE_MSGPACK_SUCCESS ) {
914+ if (value < INT_MIN || value > INT_MAX ) {
915+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
916+ }
917+
869918 decode_context -> metric -> exp_hist_scale = (int32_t ) value ;
870919 }
871920 return result ;
@@ -874,13 +923,25 @@ static int unpack_exp_histogram_scale(mpack_reader_t *reader, size_t index, void
874923static int unpack_exp_histogram_zero_count (mpack_reader_t * reader , size_t index , void * context )
875924{
876925 struct cmt_msgpack_decode_context * decode_context ;
926+
927+ if (NULL == reader ||
928+ NULL == context ) {
929+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
930+ }
931+
877932 decode_context = (struct cmt_msgpack_decode_context * ) context ;
878933 return cmt_mpack_consume_uint_tag (reader , & decode_context -> metric -> exp_hist_zero_count );
879934}
880935
881936static int unpack_exp_histogram_zero_threshold (mpack_reader_t * reader , size_t index , void * context )
882937{
883938 struct cmt_msgpack_decode_context * decode_context ;
939+
940+ if (NULL == reader ||
941+ NULL == context ) {
942+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
943+ }
944+
884945 decode_context = (struct cmt_msgpack_decode_context * ) context ;
885946 return cmt_mpack_consume_double_tag (reader , & decode_context -> metric -> exp_hist_zero_threshold );
886947}
@@ -891,9 +952,18 @@ static int unpack_exp_histogram_positive_offset(mpack_reader_t *reader, size_t i
891952 int64_t value ;
892953 int result ;
893954
955+ if (NULL == reader ||
956+ NULL == context ) {
957+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
958+ }
959+
894960 decode_context = (struct cmt_msgpack_decode_context * ) context ;
895961 result = cmt_mpack_consume_int_tag (reader , & value );
896962 if (result == CMT_DECODE_MSGPACK_SUCCESS ) {
963+ if (value < INT_MIN || value > INT_MAX ) {
964+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
965+ }
966+
897967 decode_context -> metric -> exp_hist_positive_offset = (int32_t ) value ;
898968 }
899969 return result ;
@@ -905,9 +975,18 @@ static int unpack_exp_histogram_negative_offset(mpack_reader_t *reader, size_t i
905975 int64_t value ;
906976 int result ;
907977
978+ if (NULL == reader ||
979+ NULL == context ) {
980+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
981+ }
982+
908983 decode_context = (struct cmt_msgpack_decode_context * ) context ;
909984 result = cmt_mpack_consume_int_tag (reader , & value );
910985 if (result == CMT_DECODE_MSGPACK_SUCCESS ) {
986+ if (value < INT_MIN || value > INT_MAX ) {
987+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
988+ }
989+
911990 decode_context -> metric -> exp_hist_negative_offset = (int32_t ) value ;
912991 }
913992 return result ;
@@ -916,14 +995,36 @@ static int unpack_exp_histogram_negative_offset(mpack_reader_t *reader, size_t i
916995static int unpack_exp_histogram_positive_bucket (mpack_reader_t * reader , size_t index , void * context )
917996{
918997 struct cmt_msgpack_decode_context * decode_context ;
998+
999+ if (NULL == reader ||
1000+ NULL == context ) {
1001+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1002+ }
1003+
9191004 decode_context = (struct cmt_msgpack_decode_context * ) context ;
1005+ if (decode_context -> metric -> exp_hist_positive_buckets == NULL ||
1006+ index >= decode_context -> metric -> exp_hist_positive_count ) {
1007+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1008+ }
1009+
9201010 return cmt_mpack_consume_uint_tag (reader , & decode_context -> metric -> exp_hist_positive_buckets [index ]);
9211011}
9221012
9231013static int unpack_exp_histogram_negative_bucket (mpack_reader_t * reader , size_t index , void * context )
9241014{
9251015 struct cmt_msgpack_decode_context * decode_context ;
1016+
1017+ if (NULL == reader ||
1018+ NULL == context ) {
1019+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1020+ }
1021+
9261022 decode_context = (struct cmt_msgpack_decode_context * ) context ;
1023+ if (decode_context -> metric -> exp_hist_negative_buckets == NULL ||
1024+ index >= decode_context -> metric -> exp_hist_negative_count ) {
1025+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1026+ }
1027+
9271028 return cmt_mpack_consume_uint_tag (reader , & decode_context -> metric -> exp_hist_negative_buckets [index ]);
9281029}
9291030
@@ -932,6 +1033,11 @@ static int unpack_exp_histogram_positive_buckets(mpack_reader_t *reader, size_t
9321033 struct cmt_msgpack_decode_context * decode_context ;
9331034 size_t count ;
9341035
1036+ if (NULL == reader ||
1037+ NULL == context ) {
1038+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1039+ }
1040+
9351041 decode_context = (struct cmt_msgpack_decode_context * ) context ;
9361042 count = cmt_mpack_peek_array_length (reader );
9371043
@@ -957,6 +1063,11 @@ static int unpack_exp_histogram_negative_buckets(mpack_reader_t *reader, size_t
9571063 struct cmt_msgpack_decode_context * decode_context ;
9581064 size_t count ;
9591065
1066+ if (NULL == reader ||
1067+ NULL == context ) {
1068+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1069+ }
1070+
9601071 decode_context = (struct cmt_msgpack_decode_context * ) context ;
9611072 count = cmt_mpack_peek_array_length (reader );
9621073
@@ -983,6 +1094,11 @@ static int unpack_exp_histogram_count(mpack_reader_t *reader, size_t index, void
9831094 uint64_t value ;
9841095 struct cmt_msgpack_decode_context * decode_context ;
9851096
1097+ if (NULL == reader ||
1098+ NULL == context ) {
1099+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1100+ }
1101+
9861102 decode_context = (struct cmt_msgpack_decode_context * ) context ;
9871103 result = cmt_mpack_consume_uint_tag (reader , & value );
9881104
@@ -999,6 +1115,11 @@ static int unpack_exp_histogram_sum_set(mpack_reader_t *reader, size_t index, vo
9991115 uint64_t value ;
10001116 int result ;
10011117
1118+ if (NULL == reader ||
1119+ NULL == context ) {
1120+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1121+ }
1122+
10021123 decode_context = (struct cmt_msgpack_decode_context * ) context ;
10031124 result = cmt_mpack_consume_uint_tag (reader , & value );
10041125
@@ -1016,6 +1137,11 @@ static int unpack_exp_histogram_sum(mpack_reader_t *reader, size_t index, void *
10161137 uint64_t value ;
10171138 struct cmt_msgpack_decode_context * decode_context ;
10181139
1140+ if (NULL == reader ||
1141+ NULL == context ) {
1142+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1143+ }
1144+
10191145 decode_context = (struct cmt_msgpack_decode_context * ) context ;
10201146 result = cmt_mpack_consume_uint_tag (reader , & value );
10211147
@@ -1313,6 +1439,19 @@ static int unpack_meta_type(mpack_reader_t *reader, size_t index, void *context)
13131439 result = cmt_mpack_consume_uint_tag (reader , & value );
13141440
13151441 if (CMT_DECODE_MSGPACK_SUCCESS == result ) {
1442+ if (decode_context -> map -> parent != NULL ) {
1443+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1444+ }
1445+
1446+ if (value != CMT_COUNTER &&
1447+ value != CMT_GAUGE &&
1448+ value != CMT_SUMMARY &&
1449+ value != CMT_HISTOGRAM &&
1450+ value != CMT_EXP_HISTOGRAM &&
1451+ value != CMT_UNTYPED ) {
1452+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1453+ }
1454+
13161455 decode_context -> map -> type = value ;
13171456
13181457 result = create_metric_instance (decode_context -> map );
@@ -1337,6 +1476,12 @@ static int unpack_meta_aggregation_type(mpack_reader_t *reader, size_t index, vo
13371476 result = cmt_mpack_consume_uint_tag (reader , & value );
13381477
13391478 if (CMT_DECODE_MSGPACK_SUCCESS == result ) {
1479+ if (value != CMT_AGGREGATION_TYPE_UNSPECIFIED &&
1480+ value != CMT_AGGREGATION_TYPE_DELTA &&
1481+ value != CMT_AGGREGATION_TYPE_CUMULATIVE ) {
1482+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1483+ }
1484+
13401485 decode_context -> aggregation_type = value ;
13411486 }
13421487
@@ -1346,13 +1491,23 @@ static int unpack_meta_aggregation_type(mpack_reader_t *reader, size_t index, vo
13461491static int unpack_meta_opts (mpack_reader_t * reader , size_t index , void * context )
13471492{
13481493 struct cmt_msgpack_decode_context * decode_context ;
1494+ struct cmt_opts * opts ;
13491495
13501496 if (NULL == reader ||
13511497 NULL == context ) {
13521498 return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
13531499 }
13541500
13551501 decode_context = (struct cmt_msgpack_decode_context * ) context ;
1502+ opts = decode_context -> map -> opts ;
1503+ if (opts == NULL ||
1504+ opts -> ns != NULL ||
1505+ opts -> subsystem != NULL ||
1506+ opts -> name != NULL ||
1507+ opts -> description != NULL ||
1508+ decode_context -> map -> unit != NULL ) {
1509+ return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1510+ }
13561511
13571512 return unpack_opts (reader , decode_context -> map );
13581513}
@@ -1505,9 +1660,13 @@ static int unpack_basic_type_meta(mpack_reader_t *reader, size_t index, void *co
15051660 result = cmt_mpack_unpack_map (reader , callbacks , context );
15061661
15071662 if (CMT_DECODE_MSGPACK_SUCCESS == result ) {
1508- if (decode_context -> map == NULL || decode_context -> map -> parent == NULL ) {
1663+ if (decode_context -> map == NULL ||
1664+ decode_context -> map -> parent == NULL ||
1665+ decode_context -> map -> opts == NULL ||
1666+ decode_context -> map -> opts -> name == NULL ||
1667+ decode_context -> map -> opts -> description == NULL ) {
15091668 return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
1510- }
1669+ }
15111670
15121671 decode_context -> map -> label_count = cfl_list_size (& decode_context -> map -> label_keys );
15131672 if (decode_context -> map -> type == CMT_HISTOGRAM ) {
@@ -1995,6 +2154,8 @@ int cmt_decode_msgpack_create(struct cmt **out_cmt, char *in_buf, size_t in_size
19952154 return CMT_DECODE_MSGPACK_INVALID_ARGUMENT_ERROR ;
19962155 }
19972156
2157+ * out_cmt = NULL ;
2158+
19982159 if (0 == in_size ||
19992160 0 == (in_size - * offset ) ) {
20002161 return CMT_DECODE_MSGPACK_INSUFFICIENT_DATA ;
0 commit comments