From: James Jones Date: Thu, 14 May 2020 18:25:40 +0000 (-0500) Subject: Correct FR_DBUF_MAX macro and add tests for it. (#3452) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb76269352d8a2684c57fe83d367f07d71950b42;p=thirdparty%2Ffreeradius-server.git Correct FR_DBUF_MAX macro and add tests for it. (#3452) --- diff --git a/src/lib/util/dbuff.h b/src/lib/util/dbuff.h index 2ceeb5f22c3..46cbaa65dc6 100644 --- a/src/lib/util/dbuff.h +++ b/src/lib/util/dbuff.h @@ -111,8 +111,8 @@ struct fr_dbuff_s { * @param[in] _dbuff to reserve bytes in. * @param[in] _max The maximum number of bytes the caller is allowed to write to. */ -#define FR_DBUFF_MAX(_dbuff, _max) (fr_dbuff_remaining(_dbuff) > (_max)) ? \ - FR_DBUFF_RESERVE(_dbuff, fr_dbuff_remaining(_dbuff) - (_max)) : \ +#define FR_DBUFF_MAX(_dbuff, _max) ((fr_dbuff_remaining(_dbuff) > (_max)) ? \ + &FR_DBUFF_RESERVE(_dbuff, fr_dbuff_remaining(_dbuff) - (_max)) : \ _dbuff) /** Does the actual work of initialising a dbuff diff --git a/src/lib/util/dbuff_tests.c b/src/lib/util/dbuff_tests.c index 5450dd2cdd7..55a02a9a7fa 100644 --- a/src/lib/util/dbuff_tests.c +++ b/src/lib/util/dbuff_tests.c @@ -42,6 +42,23 @@ static void test_dbuff_init_no_parent(void) TEST_CHECK(dbuff.parent == NULL); } +static void test_dbuff_max(void) +{ + uint8_t const in[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + fr_dbuff_t dbuff; + fr_dbuff_t *max_dbuff; + + TEST_CASE("Confirm max constrains available space"); + fr_dbuff_init(&dbuff, in, sizeof(in)); + + max_dbuff = FR_DBUFF_MAX(&dbuff, 4); + TEST_CHECK(fr_dbuff_remaining(max_dbuff) == 4); + + max_dbuff = FR_DBUFF_MAX(&dbuff, 2 * sizeof(in)); + TEST_CHECK(fr_dbuff_remaining(max_dbuff) == sizeof(in)); +} + + /** Test the various dbuff_net_encode() functions and macros * * @note Passing constants to fr_dbuff_in() as it is written results in @@ -209,6 +226,7 @@ TEST_LIST = { */ { "fr_dbuff_init", test_dbuff_init }, { "fr_dbuff_init_no_parent", test_dbuff_init_no_parent }, + { "fr_dbuff_max", test_dbuff_max }, { "fr_dbuff_in", test_dbuff_net_encode }, { NULL }