]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Correct FR_DBUF_MAX macro and add tests for it. (#3452)
authorJames Jones <jejones3141@gmail.com>
Thu, 14 May 2020 18:25:40 +0000 (13:25 -0500)
committerGitHub <noreply@github.com>
Thu, 14 May 2020 18:25:40 +0000 (13:25 -0500)
src/lib/util/dbuff.h
src/lib/util/dbuff_tests.c

index 2ceeb5f22c3dc42a9dd33d50b22943afd5411086..46cbaa65dc64bc43d875eb4ca3441d892cff220f 100644 (file)
@@ -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
index 5450dd2cdd7913d2e800b3432a2a456904c92073..55a02a9a7fac1320577164f6cdeb6248825f42f7 100644 (file)
@@ -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 }