From: Michał Kępień Date: Wed, 25 Nov 2020 11:45:47 +0000 (+0100) Subject: Convert add_quota() to a function X-Git-Tag: v9.17.8~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea54a932d225fc55d2a815d200c3f3b988675709;p=thirdparty%2Fbind9.git Convert add_quota() to a function 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. --- diff --git a/lib/isc/tests/quota_test.c b/lib/isc/tests/quota_test.c index 36b661c3c84..99ba08f75bf 100644 --- a/lib/isc/tests/quota_test.c +++ b/lib/isc/tests/quota_test.c @@ -52,19 +52,29 @@ isc_quota_get_set_test(void **state) { isc_quota_destroy("a); } -#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("a, 100); for (i = 0; i < 100; i++) { - add_quota("a, "as[i], ISC_R_SUCCESS, true, i + 1); + add_quota("a, "as[i], ISC_R_SUCCESS, i + 1); } - add_quota("a, "as[100], ISC_R_QUOTA, false, 100); + add_quota("a, "as[100], ISC_R_QUOTA, 100); assert_int_equal(isc_quota_getused("a), 100); isc_quota_detach("as[0]); assert_null(quotas[0]); - add_quota("a, "as[100], ISC_R_SUCCESS, true, 100); - add_quota("a, "as[101], ISC_R_QUOTA, false, 100); + add_quota("a, "as[100], ISC_R_SUCCESS, 100); + add_quota("a, "as[101], ISC_R_QUOTA, 100); for (i = 100; i > 0; i--) { isc_quota_detach("as[i]); @@ -108,13 +118,13 @@ isc_quota_soft_test(void **state) { isc_quota_soft("a, 50); for (i = 0; i < 50; i++) { - add_quota("a, "as[i], ISC_R_SUCCESS, true, i + 1); + add_quota("a, "as[i], ISC_R_SUCCESS, i + 1); } for (i = 50; i < 100; i++) { - add_quota("a, "as[i], ISC_R_SOFTQUOTA, true, i + 1); + add_quota("a, "as[i], ISC_R_SOFTQUOTA, i + 1); } - add_quota("a, "as[i], ISC_R_QUOTA, false, 100); + add_quota("a, "as[i], ISC_R_QUOTA, 100); for (i = 99; i >= 0; i--) { isc_quota_detach("as[i]);