Fixed bacnet_snprintf() to be able to return the full length of a string when the size is zero. (#1164)

This commit is contained in:
Steve Karg
2025-12-01 13:45:30 -06:00
committed by GitHub
parent f28c804969
commit 73fbb400cb
4 changed files with 58 additions and 13 deletions
+10 -2
View File
@@ -121,7 +121,7 @@ static void test_BACnetDestination_ASCII(void)
zassert_true(len > 0, NULL);
status = bacnet_destination_same(&destination, &test_destination);
zassert_true(status, NULL);
/* get the length */
/* get the length without NULL termination */
null_len = bacnet_destination_to_ascii(&test_destination, NULL, 0);
if (null_len > 0) {
test_ascii = calloc(null_len, sizeof(char));
@@ -132,7 +132,15 @@ static void test_BACnetDestination_ASCII(void)
while (--test_len) {
len = bacnet_destination_to_ascii(
&test_destination, test_ascii, test_len);
zassert_equal(len, null_len, NULL);
if (test_len > 0) {
zassert_equal(
len, test_len, "%s len=%d test_len=%d", test_ascii, len,
test_len);
} else {
zassert_equal(
len, null_len, "%s len=%d null_len=%d", test_ascii, len,
null_len);
}
}
free(test_ascii);
}
+22 -1
View File
@@ -790,10 +790,27 @@ ZTEST(bacstr_tests, test_bacnet_snprintf)
static void test_bacnet_snprintf(void)
#endif
{
int buf_len = 0, str_len;
int buf_len = 0, str_len, null_len = 0, test_null_len = 0;
int i;
char str[30] = "";
const char *null_string = "REALLY BIG NULL STRING";
const char *one_char_string = "1";
const char *two_char_string = "12";
/* one char */
buf_len = 0;
str_len = 1;
buf_len = bacnet_snprintf(str, str_len, buf_len, "%s", one_char_string);
zassert_equal(buf_len, str_len, "buf_len=%d str_len=%d", buf_len, str_len);
/* two char */
buf_len = 0;
str_len = 2;
buf_len = bacnet_snprintf(str, str_len, buf_len, "%s", two_char_string);
zassert_equal(buf_len, str_len, "buf_len=%d str_len=%d", buf_len, str_len);
buf_len = bacnet_snprintf(str, str_len, buf_len, "%s", two_char_string);
zassert_equal(buf_len, str_len, "buf_len=%d str_len=%d", buf_len, str_len);
/* large chars */
buf_len = 0;
str_len = sizeof(str);
for (i = 0; i < 5; i++) {
/* appending formatted strings */
@@ -801,10 +818,14 @@ static void test_bacnet_snprintf(void)
buf_len =
bacnet_snprintf(str, str_len, buf_len, "REALLY BIG STRING BASS");
buf_len = bacnet_snprintf(str, str_len, buf_len, "}");
/* appending to a NULL string for length */
null_len = bacnet_snprintf(NULL, 0, null_len, null_string);
test_null_len += strlen(null_string);
}
zassert_equal(buf_len, str_len, "buf_len=%d str_len=%d", buf_len, str_len);
zassert_equal(
str[buf_len - 1], 0, "str[%d]=%c", buf_len - 1, str[buf_len - 1]);
zassert_equal(null_len, test_null_len, "null_len=%d", null_len);
}
/**