Fixed datetime decode of invalid tag. (#495)
* Fixed datetime decode of invalid application tag. --------- Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
+3
-1
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
The git repositories are hosted at the following sites:
|
The git repositories are hosted at the following sites:
|
||||||
* https://bacnet.sourceforge.net/
|
* https://bacnet.sourceforge.net/
|
||||||
* https://github.com/bacnet*stack
|
* https://github.com/bacnet-stack/bacnet-stack/
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
@@ -22,6 +22,8 @@ The git repositories are hosted at the following sites:
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
Fixed datetime decode of invalid application tag. (#495)
|
||||||
|
|
||||||
## [1.2.0] - 2023-09-11
|
## [1.2.0] - 2023-09-11
|
||||||
|
|
||||||
### Security
|
### Security
|
||||||
|
|||||||
@@ -1125,7 +1125,7 @@ int bacnet_datetime_decode(
|
|||||||
}
|
}
|
||||||
len = bacnet_date_application_decode(
|
len = bacnet_date_application_decode(
|
||||||
&apdu[apdu_len], apdu_size - apdu_len, bdate);
|
&apdu[apdu_len], apdu_size - apdu_len, bdate);
|
||||||
if (len < 0) {
|
if (len <= 0) {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
@@ -1134,7 +1134,7 @@ int bacnet_datetime_decode(
|
|||||||
}
|
}
|
||||||
len = bacnet_time_application_decode(
|
len = bacnet_time_application_decode(
|
||||||
&apdu[apdu_len], apdu_size - apdu_len, btime);
|
&apdu[apdu_len], apdu_size - apdu_len, btime);
|
||||||
if (len < 0) {
|
if (len <= 0) {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <zephyr/ztest.h>
|
#include <zephyr/ztest.h>
|
||||||
#include "bacnet/datetime.h"
|
#include "bacnet/datetime.h"
|
||||||
|
#include "bacnet/basic/sys/days.h"
|
||||||
#include "bacnet/bacdcode.h"
|
#include "bacnet/bacdcode.h"
|
||||||
|
|
||||||
/* define our epic beginnings */
|
/* define our epic beginnings */
|
||||||
@@ -133,7 +134,6 @@ static void testBACnetDateTimeAdd(void)
|
|||||||
zassert_equal(diff, 0, NULL);
|
zassert_equal(diff, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 /*TODO: Change to use external methods */
|
|
||||||
static void testBACnetDateTimeSeconds(void)
|
static void testBACnetDateTimeSeconds(void)
|
||||||
{
|
{
|
||||||
uint8_t hour = 0, minute = 0, second = 0;
|
uint8_t hour = 0, minute = 0, second = 0;
|
||||||
@@ -143,17 +143,17 @@ static void testBACnetDateTimeSeconds(void)
|
|||||||
for (hour = 0; hour < 24; hour++) {
|
for (hour = 0; hour < 24; hour++) {
|
||||||
for (minute = 0; minute < 60; minute += 3) {
|
for (minute = 0; minute < 60; minute += 3) {
|
||||||
for (second = 0; second < 60; second += 17) {
|
for (second = 0; second < 60; second += 17) {
|
||||||
seconds = seconds_since_midnight(hour, minute, second);
|
seconds = datetime_hms_to_seconds_since_midnight(
|
||||||
seconds_since_midnight_into_hms(
|
hour, minute, second);
|
||||||
|
datetime_hms_from_seconds_since_midnight(
|
||||||
seconds, &test_hour, &test_minute, &test_second);
|
seconds, &test_hour, &test_minute, &test_second);
|
||||||
test_seconds =
|
test_seconds = datetime_hms_to_seconds_since_midnight(
|
||||||
seconds_since_midnight(test_hour, test_minute, test_second);
|
test_hour, test_minute, test_second);
|
||||||
zassert_equal(seconds, test_seconds, NULL);
|
zassert_equal(seconds, test_seconds, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(CONFIG_ZTEST_NEW_API)
|
#if defined(CONFIG_ZTEST_NEW_API)
|
||||||
ZTEST(wp_tests, testBACnetDate)
|
ZTEST(wp_tests, testBACnetDate)
|
||||||
@@ -369,7 +369,6 @@ static void testWildcardDateTime(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 /*TODO: Change to use external methods */
|
|
||||||
static void testDayOfYear(void)
|
static void testDayOfYear(void)
|
||||||
{
|
{
|
||||||
uint32_t days = 0;
|
uint32_t days = 0;
|
||||||
@@ -379,17 +378,17 @@ static void testDayOfYear(void)
|
|||||||
BACNET_DATE bdate;
|
BACNET_DATE bdate;
|
||||||
BACNET_DATE test_bdate;
|
BACNET_DATE test_bdate;
|
||||||
|
|
||||||
days = day_of_year(1900, 1, 1);
|
days = days_of_year(1900, 1, 1);
|
||||||
zassert_equal(days, 1, NULL);
|
zassert_equal(days, 1, NULL);
|
||||||
day_of_year_into_md(days, 1900, &month, &day);
|
days_of_year_to_month_day(days, 1900, &month, &day);
|
||||||
zassert_equal(month, 1, NULL);
|
zassert_equal(month, 1, NULL);
|
||||||
zassert_equal(day, 1, NULL);
|
zassert_equal(day, 1, NULL);
|
||||||
|
|
||||||
for (year = 1900; year <= 2154; year++) {
|
for (year = 1900; year <= 2154; year++) {
|
||||||
for (month = 1; month <= 12; month++) {
|
for (month = 1; month <= 12; month++) {
|
||||||
for (day = 1; day <= datetime_month_days(year, month); day++) {
|
for (day = 1; day <= days_per_month(year, month); day++) {
|
||||||
days = day_of_year(year, month, day);
|
days = days_of_year(year, month, day);
|
||||||
day_of_year_into_md(days, year, &test_month, &test_day);
|
days_of_year_to_month_day(days, year, &test_month, &test_day);
|
||||||
zassert_equal(month, test_month, NULL);
|
zassert_equal(month, test_month, NULL);
|
||||||
zassert_equal(day, test_day, NULL);
|
zassert_equal(day, test_day, NULL);
|
||||||
}
|
}
|
||||||
@@ -397,16 +396,16 @@ static void testDayOfYear(void)
|
|||||||
}
|
}
|
||||||
for (year = 1900; year <= 2154; year++) {
|
for (year = 1900; year <= 2154; year++) {
|
||||||
for (month = 1; month <= 12; month++) {
|
for (month = 1; month <= 12; month++) {
|
||||||
for (day = 1; day <= datetime_month_days(year, month); day++) {
|
for (day = 1; day <= days_per_month(year, month); day++) {
|
||||||
datetime_set_date(&bdate, year, month, day);
|
datetime_set_date(&bdate, year, month, day);
|
||||||
days = datetime_day_of_year(&bdate);
|
days = datetime_day_of_year(&bdate);
|
||||||
datetime_day_of_year_into_date(days, year, &test_bdate);
|
datetime_day_of_year_into_date(days, year, &test_bdate);
|
||||||
zassert_true(datetime_compare_date(&bdate, &test_bdate), 0, NULL);
|
zassert_equal(datetime_compare_date(&bdate, &test_bdate), 0,
|
||||||
|
"year=%u month=%u day=%u", year, month, day);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static void testDateEpochConversionCompare(uint16_t year,
|
static void testDateEpochConversionCompare(uint16_t year,
|
||||||
uint8_t month,
|
uint8_t month,
|
||||||
@@ -448,7 +447,6 @@ static void testDateEpochConversion(void)
|
|||||||
BACNET_EPOCH_YEAR + 0xFF - 1, 12, 31, 23, 59, 59, 0);
|
BACNET_EPOCH_YEAR + 0xFF - 1, 12, 31, 23, 59, 59, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 /*TODO: Change to use external methods */
|
|
||||||
static void testDateEpoch(void)
|
static void testDateEpoch(void)
|
||||||
{
|
{
|
||||||
uint32_t days = 0;
|
uint32_t days = 0;
|
||||||
@@ -456,19 +454,19 @@ static void testDateEpoch(void)
|
|||||||
uint8_t month = 0, test_month = 0;
|
uint8_t month = 0, test_month = 0;
|
||||||
uint8_t day = 0, test_day = 0;
|
uint8_t day = 0, test_day = 0;
|
||||||
|
|
||||||
days = days_since_epoch(BACNET_EPOCH_YEAR, 1, 1);
|
days = days_since_epoch(BACNET_EPOCH_YEAR, BACNET_EPOCH_YEAR, 1, 1);
|
||||||
zassert_equal(days, 0, NULL);
|
zassert_equal(days, 1, "days=%lu", (unsigned long)days);
|
||||||
days_since_epoch_into_ymd(days, &year, &month, &day);
|
days_since_epoch_to_date(BACNET_EPOCH_YEAR, days, &year, &month, &day);
|
||||||
zassert_equal(year, BACNET_EPOCH_YEAR, NULL);
|
zassert_equal(year, BACNET_EPOCH_YEAR, NULL);
|
||||||
zassert_equal(month, 1, NULL);
|
zassert_equal(month, 1, NULL);
|
||||||
zassert_equal(day, 1, NULL);
|
zassert_equal(day, 1, NULL);
|
||||||
|
|
||||||
for (year = BACNET_EPOCH_YEAR; year < (BACNET_EPOCH_YEAR + 0xFF); year++) {
|
for (year = BACNET_EPOCH_YEAR; year < (BACNET_EPOCH_YEAR + 0xFF); year++) {
|
||||||
for (month = 1; month <= 12; month++) {
|
for (month = 1; month <= 12; month++) {
|
||||||
for (day = 1; day <= datetime_month_days(year, month); day++) {
|
for (day = 1; day <= days_per_month(year, month); day++) {
|
||||||
days = days_since_epoch(year, month, day);
|
days = days_since_epoch(BACNET_EPOCH_YEAR, year, month, day);
|
||||||
days_since_epoch_into_ymd(
|
days_since_epoch_to_date(BACNET_EPOCH_YEAR, days, &test_year,
|
||||||
days, &test_year, &test_month, &test_day);
|
&test_month, &test_day);
|
||||||
zassert_equal(year, test_year, NULL);
|
zassert_equal(year, test_year, NULL);
|
||||||
zassert_equal(month, test_month, NULL);
|
zassert_equal(month, test_month, NULL);
|
||||||
zassert_equal(day, test_day, NULL);
|
zassert_equal(day, test_day, NULL);
|
||||||
@@ -476,7 +474,6 @@ static void testDateEpoch(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(CONFIG_ZTEST_NEW_API)
|
#if defined(CONFIG_ZTEST_NEW_API)
|
||||||
ZTEST(wp_tests, testBACnetDayOfWeek)
|
ZTEST(wp_tests, testBACnetDayOfWeek)
|
||||||
@@ -520,8 +517,9 @@ static void testDatetimeCodec(void)
|
|||||||
uint8_t tag_number = 10;
|
uint8_t tag_number = 10;
|
||||||
BACNET_DATE_TIME datetimeIn;
|
BACNET_DATE_TIME datetimeIn;
|
||||||
BACNET_DATE_TIME datetimeOut;
|
BACNET_DATE_TIME datetimeOut;
|
||||||
int inLen;
|
int apdu_len;
|
||||||
int outLen;
|
int test_len;
|
||||||
|
int null_len;
|
||||||
int diff;
|
int diff;
|
||||||
bool status;
|
bool status;
|
||||||
|
|
||||||
@@ -530,40 +528,55 @@ static void testDatetimeCodec(void)
|
|||||||
status = datetime_time_init_ascii(&datetimeIn.time, "5:06:07.8");
|
status = datetime_time_init_ascii(&datetimeIn.time, "5:06:07.8");
|
||||||
zassert_true(status, NULL);
|
zassert_true(status, NULL);
|
||||||
/* application */
|
/* application */
|
||||||
inLen = bacapp_encode_datetime(NULL, &datetimeIn);
|
apdu_len = bacapp_encode_datetime(NULL, &datetimeIn);
|
||||||
zassert_true(inLen <= sizeof(apdu), NULL);
|
zassert_true(apdu_len <= sizeof(apdu), NULL);
|
||||||
inLen = bacapp_encode_datetime(apdu, &datetimeIn);
|
null_len = bacapp_encode_datetime(NULL, &datetimeIn);
|
||||||
outLen = bacnet_datetime_decode(apdu, inLen, &datetimeOut);
|
apdu_len = bacapp_encode_datetime(apdu, &datetimeIn);
|
||||||
zassert_equal(inLen, outLen, NULL);
|
zassert_equal(apdu_len, null_len, NULL);
|
||||||
|
zassert_true(apdu_len > 0, NULL);
|
||||||
|
null_len = bacnet_datetime_decode(apdu, apdu_len, NULL);
|
||||||
|
test_len = bacnet_datetime_decode(apdu, apdu_len, &datetimeOut);
|
||||||
|
zassert_equal(apdu_len, null_len, NULL);
|
||||||
|
zassert_equal(apdu_len, test_len, NULL);
|
||||||
|
zassert_true(apdu_len > 0, NULL);
|
||||||
diff = datetime_compare(&datetimeOut, &datetimeIn);
|
diff = datetime_compare(&datetimeOut, &datetimeIn);
|
||||||
zassert_equal(diff, 0, NULL);
|
zassert_equal(diff, 0, NULL);
|
||||||
|
/* test for invalid date tag */
|
||||||
|
apdu_len = bacapp_encode_datetime(apdu, &datetimeIn);
|
||||||
|
encode_tag(apdu, BACNET_APPLICATION_TAG_REAL, false, 4);
|
||||||
|
test_len = bacnet_datetime_decode(&apdu[0], apdu_len, &datetimeOut);
|
||||||
|
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||||
|
/* test for invalid time tag */
|
||||||
|
apdu_len = bacapp_encode_datetime(apdu, &datetimeIn);
|
||||||
|
encode_tag(&apdu[5], BACNET_APPLICATION_TAG_REAL, false, 4);
|
||||||
|
test_len = bacnet_datetime_decode(apdu, apdu_len, &datetimeOut);
|
||||||
|
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||||
/* ERROR too short APDU */
|
/* ERROR too short APDU */
|
||||||
while (inLen) {
|
apdu_len = bacapp_encode_datetime(apdu, &datetimeIn);
|
||||||
inLen--;
|
while (apdu_len) {
|
||||||
outLen = bacnet_datetime_decode(apdu, inLen, &datetimeOut);
|
apdu_len--;
|
||||||
zassert_equal(outLen, BACNET_STATUS_ERROR, NULL);
|
test_len = bacnet_datetime_decode(apdu, apdu_len, &datetimeOut);
|
||||||
|
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||||
}
|
}
|
||||||
/* context */
|
/* context */
|
||||||
inLen = bacapp_encode_context_datetime(NULL, tag_number, &datetimeIn);
|
apdu_len = bacapp_encode_context_datetime(NULL, tag_number, &datetimeIn);
|
||||||
zassert_true(inLen <= sizeof(apdu), NULL);
|
zassert_true(apdu_len <= sizeof(apdu), NULL);
|
||||||
inLen = bacapp_encode_context_datetime(apdu, tag_number, &datetimeIn);
|
apdu_len = bacapp_encode_context_datetime(apdu, tag_number, &datetimeIn);
|
||||||
outLen =
|
test_len = bacnet_datetime_context_decode(
|
||||||
bacnet_datetime_context_decode(apdu, inLen, tag_number, &datetimeOut);
|
apdu, apdu_len, tag_number, &datetimeOut);
|
||||||
zassert_equal(inLen, outLen, NULL);
|
zassert_equal(apdu_len, test_len, NULL);
|
||||||
/* ERROR too short APDU */
|
/* ERROR too short APDU */
|
||||||
while (inLen) {
|
while (apdu_len) {
|
||||||
inLen--;
|
apdu_len--;
|
||||||
outLen = bacnet_datetime_context_decode(
|
test_len = bacnet_datetime_context_decode(
|
||||||
apdu, inLen, tag_number, &datetimeOut);
|
apdu, apdu_len, tag_number, &datetimeOut);
|
||||||
zassert_equal(outLen, BACNET_STATUS_ERROR, NULL);
|
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||||
}
|
}
|
||||||
diff = datetime_compare(&datetimeOut, &datetimeIn);
|
diff = datetime_compare(&datetimeOut, &datetimeIn);
|
||||||
zassert_equal(diff, 0, NULL);
|
zassert_equal(diff, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 /*TODO: Change to use external methods */
|
static void testDatetimeConvertUTCSpecific(BACNET_DATE_TIME *utc_time,
|
||||||
static void testDatetimeConvertUTCSpecific(
|
|
||||||
BACNET_DATE_TIME *utc_time,
|
|
||||||
BACNET_DATE_TIME *local_time,
|
BACNET_DATE_TIME *local_time,
|
||||||
int16_t utc_offset_minutes,
|
int16_t utc_offset_minutes,
|
||||||
int8_t dst_adjust_minutes)
|
int8_t dst_adjust_minutes)
|
||||||
@@ -588,9 +601,7 @@ static void testDatetimeConvertUTCSpecific(
|
|||||||
zassert_equal(
|
zassert_equal(
|
||||||
local_time->time.hundredths, test_local_time.time.hundredths, NULL);
|
local_time->time.hundredths, test_local_time.time.hundredths, NULL);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#if 0 /*TODO: Change to use external methods */
|
|
||||||
static void testDatetimeConvertUTC(void)
|
static void testDatetimeConvertUTC(void)
|
||||||
{
|
{
|
||||||
BACNET_DATE_TIME local_time;
|
BACNET_DATE_TIME local_time;
|
||||||
@@ -614,30 +625,27 @@ static void testDatetimeConvertUTC(void)
|
|||||||
testDatetimeConvertUTCSpecific(
|
testDatetimeConvertUTCSpecific(
|
||||||
&utc_time, &local_time, utc_offset_minutes, dst_adjust_minutes);
|
&utc_time, &local_time, utc_offset_minutes, dst_adjust_minutes);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(CONFIG_ZTEST_NEW_API)
|
#if defined(CONFIG_ZTEST_NEW_API)
|
||||||
ZTEST_SUITE(wp_tests, NULL, NULL, NULL, NULL, NULL);
|
ZTEST_SUITE(datetime_tests, NULL, NULL, NULL, NULL, NULL);
|
||||||
#else
|
#else
|
||||||
void test_main(void)
|
void test_main(void)
|
||||||
{
|
{
|
||||||
#if 0
|
ztest_test_suite(datetime_tests, ztest_unit_test(testBACnetDate),
|
||||||
ztest_unit_test(testDateEpoch),
|
|
||||||
ztest_unit_test(testBACnetDateTimeSeconds),
|
|
||||||
ztest_unit_test(testDayOfYear),
|
|
||||||
#endif
|
|
||||||
ztest_test_suite(wp_tests, ztest_unit_test(testBACnetDate),
|
|
||||||
ztest_unit_test(testBACnetTime), ztest_unit_test(testBACnetDateTime),
|
ztest_unit_test(testBACnetTime), ztest_unit_test(testBACnetDateTime),
|
||||||
ztest_unit_test(testBACnetDayOfWeek),
|
ztest_unit_test(testBACnetDayOfWeek),
|
||||||
ztest_unit_test(testDateEpochConversion),
|
ztest_unit_test(testDateEpochConversion),
|
||||||
ztest_unit_test(testBACnetDateTimeAdd),
|
ztest_unit_test(testBACnetDateTimeAdd),
|
||||||
ztest_unit_test(testBACnetDateTimeWildcard),
|
ztest_unit_test(testBACnetDateTimeWildcard),
|
||||||
ztest_unit_test(testDatetimeCodec),
|
ztest_unit_test(testDatetimeCodec),
|
||||||
ztest_unit_test(testWildcardDateTime));
|
ztest_unit_test(testWildcardDateTime), ztest_unit_test(testDateEpoch),
|
||||||
|
ztest_unit_test(testBACnetDateTimeSeconds),
|
||||||
|
ztest_unit_test(testDayOfYear),
|
||||||
|
ztest_unit_test(testDatetimeConvertUTC));
|
||||||
|
|
||||||
ztest_run_test_suite(wp_tests);
|
ztest_run_test_suite(datetime_tests);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user