]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix cppcheck 1.89 warnings
authorMichał Kępień <michal@isc.org>
Wed, 16 Oct 2019 20:06:00 +0000 (22:06 +0200)
committerMichał Kępień <michal@isc.org>
Thu, 17 Oct 2019 08:50:51 +0000 (10:50 +0200)
cppcheck 1.89 enabled certain value flow analysis mechanisms [1] which
trigger null pointer dereference false positives in lib/dns/rpz.c:

    lib/dns/rpz.c:584:7: warning: Possible null pointer dereference: tgt_ip [nullPointer]
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^
    lib/dns/rpz.c:1425:44: note: Calling function 'adj_trigger_cnt', 4th argument '(void*)0' value is 0
      adj_trigger_cnt(rpzs, rpz_num, rpz_type, NULL, 0, true);
                                               ^
    lib/dns/rpz.c:584:7: note: Null pointer dereference
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^
    lib/dns/rpz.c:598:7: warning: Possible null pointer dereference: tgt_ip [nullPointer]
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^
    lib/dns/rpz.c:1425:44: note: Calling function 'adj_trigger_cnt', 4th argument '(void*)0' value is 0
      adj_trigger_cnt(rpzs, rpz_num, rpz_type, NULL, 0, true);
                                               ^
    lib/dns/rpz.c:598:7: note: Null pointer dereference
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^
    lib/dns/rpz.c:612:7: warning: Possible null pointer dereference: tgt_ip [nullPointer]
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^
    lib/dns/rpz.c:1425:44: note: Calling function 'adj_trigger_cnt', 4th argument '(void*)0' value is 0
      adj_trigger_cnt(rpzs, rpz_num, rpz_type, NULL, 0, true);
                                               ^
    lib/dns/rpz.c:612:7: note: Null pointer dereference
      if (KEY_IS_IPV4(tgt_prefix, tgt_ip)) {
          ^

It seems that cppcheck no longer treats at least some REQUIRE()
assertion failures as fatal, so add extra assertion macro definitions to
lib/isc/include/isc/util.h that are only used when the CPPCHECK
preprocessor macro is defined; these definitions make cppcheck 1.89
behave as expected.

There is an important requirement for these custom definitions to work:
cppcheck must properly treat abort() as a function which does not
return.  In order for that to happen, the __GNUC__ macro must be set to
a high enough number (because system include directories are used and
system headers compile attributes away if __GNUC__ is not high enough).
__GNUC__ is thus set to the major version number of the GCC compiler
used, which is what that latter does itself during compilation.

[1] https://github.com/danmar/cppcheck/commit/aaeec462e6d96bb70c2b1cf030979d09e2d7c959

(cherry picked from commit abfde3d543576311ce5d32089d774a360b7edc9f)

.gitlab-ci.yml
lib/isc/include/isc/util.h

index 980087a67c21bae89959302fc8934508c86c9980..f5c8e376ffd6249821bc95e80dd8477e78dccc2e 100644 (file)
@@ -257,6 +257,9 @@ stages:
 .cppcheck: &cppcheck_job
   <<: *default_triggering_rules
   stage: postcheck
+  before_script:
+    - export GCC_VERSION=$(gcc --version | sed -n 's/.*\([0-9]\+\)\.[0-9]\+\.[0-9]\+.*/\1/p')
+    - sed -i "/gcc\",/a\"-DCPPCHECK\", \"-D__STDC__\", \"-D__GNUC__=${GCC_VERSION}\"," compile_commands.json
   script:
     - *run_cppcheck
   after_script:
index 6602aac2b37ab155b92bcbee271154711c17e825..20d0937f48105a66c2f96db666a827d0ad98e6f0 100644 (file)
@@ -230,6 +230,9 @@ extern void mock_assert(const int result, const char* const expression,
 #define _assert_int_not_equal(a, b, f, l) \
        (((a) != (b)) ? (void)0 : (_assert_int_not_equal(a, b, f, l), abort()))
 #else /* UNIT_TESTING */
+
+#ifndef CPPCHECK
+
 /*
  * Assertions
  */
@@ -244,6 +247,19 @@ extern void mock_assert(const int result, const char* const expression,
 /*% Invariant Assertion */
 #define INVARIANT(e)                   ISC_INVARIANT(e)
 
+#else /* CPPCHECK */
+
+/*% Require Assertion */
+#define REQUIRE(e)                     if (!(e)) abort()
+/*% Ensure Assertion */
+#define ENSURE(e)                      if (!(e)) abort()
+/*% Insist Assertion */
+#define INSIST(e)                      if (!(e)) abort()
+/*% Invariant Assertion */
+#define INVARIANT(e)                   if (!(e)) abort()
+
+#endif /* CPPCHECK */
+
 #endif /* UNIT_TESTING */
 
 /*