diff --git a/bacnet-stack/bacdcode.c b/bacnet-stack/bacdcode.c index be16d334..5fa8ae2b 100644 --- a/bacnet-stack/bacdcode.c +++ b/bacnet-stack/bacdcode.c @@ -974,7 +974,11 @@ int encode_context_object_id(uint8_t * apdu, /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_object_id(&apdu[1], object_type, instance); - len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + /* we only reserved 1 byte for encoding the tag - check the limits */ + if ((tag_number <= 14) && (len <= 4)) + len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + else + len = 0; return len; } @@ -1149,7 +1153,11 @@ int encode_context_unsigned(uint8_t * apdu, int tag_number, uint32_t value) int len = 0; len = encode_bacnet_unsigned(&apdu[1], value); - len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + /* we only reserved 1 byte for encoding the tag - check the limits */ + if ((tag_number <= 14) && (len <= 4)) + len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + else + len = 0; return len; } @@ -1246,7 +1254,11 @@ int encode_context_enumerated(uint8_t * apdu, int tag_number, int value) /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_enumerated(&apdu[1], value); - len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + /* we only reserved 1 byte for encoding the tag - check the limits */ + if ((tag_number <= 14) && (len <= 4)) + len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + else + len = 0; return len; } @@ -1333,7 +1345,11 @@ int encode_context_signed(uint8_t * apdu, int tag_number, int32_t value) /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_signed(&apdu[1], value); - len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + /* we only reserved 1 byte for encoding the tag - check the limits */ + if ((tag_number <= 14) && (len <= 4)) + len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + else + len = 0; return len; } @@ -1420,6 +1436,21 @@ int encode_tagged_date(uint8_t * apdu, BACNET_DATE * bdate) } +int encode_context_date(uint8_t * apdu, int tag_number, BACNET_DATE * bdate) +{ + int len = 0; /* return value */ + + /* assumes that the tag only consumes 1 octet */ + len = encode_bacnet_date(&apdu[1], bdate); + /* we only reserved 1 byte for encoding the tag - check the limits */ + if ((tag_number <= 14) && (len <= 4)) + len += encode_tag(&apdu[0], (uint8_t) tag_number, true, len); + else + len = 0; + + return len; +} + /* from clause 20.2.12 Encoding of a Date Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ diff --git a/bacnet-stack/bacdcode.h b/bacnet-stack/bacdcode.h index 0ee3c38a..4799dd6c 100644 --- a/bacnet-stack/bacdcode.h +++ b/bacnet-stack/bacdcode.h @@ -170,6 +170,8 @@ extern "C" { /* returns the number of apdu bytes consumed */ int encode_bacnet_date(uint8_t * apdu, BACNET_DATE * bdate); int encode_tagged_date(uint8_t * apdu, BACNET_DATE * bdate); + int encode_context_date(uint8_t * apdu, int tag_number, + BACNET_DATE * bdate); int decode_date(uint8_t * apdu, BACNET_DATE * bdate); /* two octet unsigned16 */