Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-2782

S995, S1172: Fix false positive with C APIs

    Details

      Description

      It is a common pattern to use function pointers to pass callback arguments in C APIs (and even to some C++ APIs). This also extends to uses that are not strictly speaking related to callbacks.
      In that context, having unused arguments or wrongly non const arguments is not under the control of the author of the callback.

      // coming from an API
      typedef int (*some_cb_signature) (int *a, int *b);
      int some_c_api_function(some_cb_signature cb);
      
      // b is unused but
      // this should not raise an issue as it used as an argument to a C API later on
      int myCallback1 (int *a, int *b) {
        *a = 0;
        return *a;
      }
      
      // a and b should be const pointers but
      // this should not raise an issue as it used as an argument to a C API later on
      int myCallback2 (int *a, int *b) {
        return *a + *b;
      }
      
      void f() {
        some_c_api_function(&myCallback1);
        some_c_api_function(&myCallback2);
      }
      

      This exception can be implemented in two ways:

      • in each translation unit with an heuristic on the name. Any function name having "cb" or "callback" in it for instance should be free to have unused parameters. More patterns can be added.
      • at project level, collect all functions used as arguments and silent the corresponding issues

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                geoffray.adde Geoffray Adde
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: