]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert add_quota() to a function
authorMichał Kępień <michal@isc.org>
Wed, 25 Nov 2020 11:45:47 +0000 (12:45 +0100)
committerMichał Kępień <michal@isc.org>
Wed, 25 Nov 2020 11:45:47 +0000 (12:45 +0100)
cppcheck 2.2 reports the following false positive:

    lib/isc/tests/quota_test.c:71:21: error: Array 'quotas[101]' accessed at index 110, which is out of bounds. [arrayIndexOutOfBounds]
     isc_quota_t *quotas[110];
                        ^

The above is not even an array access, so this report is obviously
caused by a cppcheck bug.  Yet, it seems to be triggered by the presence
of the add_quota() macro, which should really be a function.  Convert
the add_quota() macro to a function in order to make the code cleaner
and to prevent the above cppcheck 2.2 false positive from being
triggered.

lib/isc/tests/quota_test.c

index 36b661c3c84fcdafd78c25f7d26e127ec4ab3418..99ba08f75bfa004e9f31788e2646129586d08c75 100644 (file)
@@ -52,19 +52,29 @@ isc_quota_get_set_test(void **state) {
        isc_quota_destroy(&quota);
 }
 
-#define add_quota(quota, quotasp, exp, attached, exp_used)              \
-       {                                                               \
-               *quotasp = NULL;                                        \
-               isc_result_t result = isc_quota_attach(quota, quotasp); \
-               assert_int_equal(result, exp);                          \
-               if (attached) {                                         \
-                       assert_ptr_equal(*quotasp, quota);              \
-               } else {                                                \
-                       assert_null(*quotasp);                          \
-               }                                                       \
-               assert_int_equal(isc_quota_getused(quota), exp_used);   \
+static void
+add_quota(isc_quota_t *source, isc_quota_t **target,
+         isc_result_t expected_result, int expected_used) {
+       isc_result_t result;
+
+       *target = NULL;
+
+       result = isc_quota_attach(source, target);
+       assert_int_equal(result, expected_result);
+
+       switch (expected_result) {
+       case ISC_R_SUCCESS:
+       case ISC_R_SOFTQUOTA:
+               assert_ptr_equal(*target, source);
+               break;
+       default:
+               assert_null(*target);
+               break;
        }
 
+       assert_int_equal(isc_quota_getused(source), expected_used);
+}
+
 static void
 isc_quota_hard_test(void **state) {
        isc_quota_t quota;
@@ -75,18 +85,18 @@ isc_quota_hard_test(void **state) {
        isc_quota_init(&quota, 100);
 
        for (i = 0; i < 100; i++) {
-               add_quota(&quota, &quotas[i], ISC_R_SUCCESS, true, i + 1);
+               add_quota(&quota, &quotas[i], ISC_R_SUCCESS, i + 1);
        }
 
-       add_quota(&quota, &quotas[100], ISC_R_QUOTA, false, 100);
+       add_quota(&quota, &quotas[100], ISC_R_QUOTA, 100);
 
        assert_int_equal(isc_quota_getused(&quota), 100);
 
        isc_quota_detach(&quotas[0]);
        assert_null(quotas[0]);
 
-       add_quota(&quota, &quotas[100], ISC_R_SUCCESS, true, 100);
-       add_quota(&quota, &quotas[101], ISC_R_QUOTA, false, 100);
+       add_quota(&quota, &quotas[100], ISC_R_SUCCESS, 100);
+       add_quota(&quota, &quotas[101], ISC_R_QUOTA, 100);
 
        for (i = 100; i > 0; i--) {
                isc_quota_detach(&quotas[i]);
@@ -108,13 +118,13 @@ isc_quota_soft_test(void **state) {
        isc_quota_soft(&quota, 50);
 
        for (i = 0; i < 50; i++) {
-               add_quota(&quota, &quotas[i], ISC_R_SUCCESS, true, i + 1);
+               add_quota(&quota, &quotas[i], ISC_R_SUCCESS, i + 1);
        }
        for (i = 50; i < 100; i++) {
-               add_quota(&quota, &quotas[i], ISC_R_SOFTQUOTA, true, i + 1);
+               add_quota(&quota, &quotas[i], ISC_R_SOFTQUOTA, i + 1);
        }
 
-       add_quota(&quota, &quotas[i], ISC_R_QUOTA, false, 100);
+       add_quota(&quota, &quotas[i], ISC_R_QUOTA, 100);
 
        for (i = 99; i >= 0; i--) {
                isc_quota_detach(&quotas[i]);