1

I have a virtual function (not pure) like this:

In the header:

  virtual int8_t insertData(
    uint8_t* AddressInput,
    myStruct* cpstrData,
    );

This function can be implemented on child classes but is not mandatory that all of them do it.

Because of this, the base class has an implementation that goes like this:

int8_t myClass::insertData(
    uint8_t* AddressInput,
    myStruct* cpstrData,
  )
{
  int8_t s8Err = SUCCESS;
  (void)(*AddressInput); //I also tried: (void)AddressInput;
  (void)(*cpstrData);    //I also tried: (void)cpstrData;
  return s8Err;
}

This works OK BUT a MISRA checker is giving me the following error (for both pointers): https://rules.sonarsource.com/c/RSPEC-995

Pointer parameter is not used to modify the addressed object but is not declared as a pointer to const

In the children classes this pointer is actually used to modify the addressed object, is there a way to tell the program that this pointers point to const only in the base class? Or a workaround similar to (void)thisVariable thatworks on the pointed objects?

sehe
  • 374,641
  • 47
  • 450
  • 633
Ivan
  • 1,352
  • 2
  • 13
  • 31
  • Does this still occur when you add the `override` keyword to the function? – RL-S Nov 03 '21 at 15:23
  • 2
    Does it make a difference to MISRA if the parameter is unnamed, as in `int8_t myClass::insertData( uint8_t* /*AddressInput*/, myStruct* /*cpstrData*/, )`? – JaMiT Nov 03 '21 at 15:23
  • 1
    Unfortunately, I don't know anything about MISRA beyond that it does exist. My base class impl. would be: `int8_t myClass::insertData(uint8_t*, myStruct*) { return SUCCESS; }`. – Scheff's Cat Nov 03 '21 at 15:24
  • If you're really not using the parameters at all, JaMiT's answer should do the trick. However, then you should receive an `unused parameter` warning. – RL-S Nov 03 '21 at 15:25
  • @RL-S you are right, the `(void)Something` trick is meant to avoid the `unused parameter` – Ivan Nov 03 '21 at 15:26
  • 1
    Reminder that If these are out parameters ( I can see they aren't in this case) then you need to follow the API convention for them, Which is probably to initialize them some way. Particularly if you're returning `SUCCESS` then the caller assumes you've made the appropriate modifications to the pointers. Also in C++17 you'd use [`[[maybe_unused]]`](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused) to indicate this. – Mgetz Nov 03 '21 at 15:30
  • I've seen the `(void)parameter` trick in C and was under the impression that parameters must be named there. ([How to suppress "unused parameter" warnings in C?](https://stackoverflow.com/q/3599160/7478597)) In C++, there shouldn't be such necessity as unnamed parameters are supported. – Scheff's Cat Nov 03 '21 at 15:30
  • I think we can safely assume this is not about silencing compiler warnings. The warning doesn't exist in any compiler I know of and (only) makes a lot of sense in MISRA guidelines. (added the link https://rules.sonarsource.com/c/RSPEC-995 to the text) – sehe Nov 03 '21 at 15:31
  • @RL-S i can try the `override` on the base class, but I don't get why this would solve the problem – Ivan Nov 03 '21 at 15:35
  • @sehe true, in this case it got complicated because the pointed object is used in the iplementatinos in the child classes but not in the one of the base.. and not all children need it (else I would make it pure virtual) – Ivan Nov 03 '21 at 15:37
  • If the base class is the first which declares the `virtual` member function, `override` is an error. `override` may be used only if the function `override`s one of its own direct or indirect base classes. (IMHO this is the actual sense why `override` has been introduced - to give the compiler a hint that there must be a member function with matching name and signature in a base class.) – Scheff's Cat Nov 03 '21 at 15:37
  • `(void)(*AddressInput);` is wrong BTW, deferencing potentially invalid/nullptr pointer. `(void)(AddressInput);` would silent warning for unused variable (in most compiler). – Jarod42 Nov 03 '21 at 15:40
  • @Jarod42 you are right, `(void)(AddressInput);` is ok for unused variable but not for `Pointer parameter is not used to modify the addressed object but is not declared as a pointer to const` – Ivan Nov 03 '21 at 15:42
  • 1
    ah, yeah, I thought that it's a child class implementation. Then `override` is wrong. – RL-S Nov 03 '21 at 15:43
  • However, the answer by JaMiT (#2) should already solve the issue. – RL-S Nov 03 '21 at 15:43
  • @RL-S just FYI, in child implementations I use `override` :) – Ivan Nov 03 '21 at 15:43
  • @RL-S I'm testing ;) – Ivan Nov 03 '21 at 15:44
  • `if (AddressInput) *AddressInput = *AddressInput;` might silent that warning (but create others) – Jarod42 Nov 03 '21 at 15:47
  • 1
    I would extract interface for this function and then in implementation drop names of parameters `int8_t myClass::insertData(uint8_t* /*AddressInput*/, myStruct* /*cpstrData*/)` – Marek R Nov 03 '21 at 16:26

1 Answers1

0

As said in the comments, using unnamed parameters fixes MISRA's issue:

int8_t myClass::insertData(uint8_t* /*AddressInput*/, myStruct* /*cpstrData*/)
{
  int8_t s8Err = FAIL;
  return s8Err;
}
Ivan
  • 1,352
  • 2
  • 13
  • 31
  • 1
    It doesn't make any sense at all for this fix to make the error go away. Or it would appear that your MISRA checker is broken. – Lundin Nov 03 '21 at 21:25
  • 1
    @Lundin: Issue with virtual function is that the rule would have to check every overridden methods to be correct (or simply ignore that rule for every virtual method). Way to silent the warning might be discutable indeed. – Jarod42 Nov 04 '21 at 12:05
  • @Jarod42 Yeah I understand the problem. Though the more "premium" MISRA checkers have the option to include the whole project and you should expect them to do such checks on all derived classes when used on the project as whole. That is, if MISRA-C++ is even mentioning the problem with inheritance, which isn't necessarily the case. Otherwise this could arguably be a defect in the guidelines, but I haven't used MISRA-C++ so I can't say. – Lundin Nov 04 '21 at 12:13
  • Btw at least in MISRA-C there's a requirement that function declaration and definition must match. What does MISRA-C++ say in this regard when it comes to `virtual` functions? Is that an allowed exception? – Lundin Nov 04 '21 at 12:19