Menu

Possible false positve

Robert
2020-12-24
2020-12-28
  • Robert

    Robert - 2020-12-24

    Hi,

    I'm not sure if this should be marked as false positive or not. I have the following code

    if (exceptions != nullptr)
    {
      std::lock_guard<FAIC_Mutex> l_MutexLocker(exceptionsMutex);
      if (exceptions != nullptr)
      {
        ((*exceptions).*a_Function)(a_Args...);
      }
    }
    

    I get the warning identicalInnerCondition on the second if statement. But in this case the two ifs are needed. The firts is to check if we actual need to aquire the lock (is someone else has it, it can take some time). The second is to be sure that the pointer is still valid once the lock is aquired (as anaother locking party might have invalidated it).

    I don't know if this kind of dynamic behavior should be checked by cppcheck

     
  • Daniel Marjamäki

    I think it would be good with a bit of context. I can't see what exceptions is for instance. I assume it's some kind of global variable? Is it volatile?

     
  • Robert

    Robert - 2020-12-27

    It is a bit of code from my work. I will provide some context after the holidays vacation

     
  • Lionel Gimbert

    Lionel Gimbert - 2020-12-28

    It's a kind of false positive I seen when dealing with multi-threading protection.

    I can provide a code example that also trigger the issue:

    void func()
    {
      static int i = 0;
      static std::mutex mutex;
      if( i == 0)
      {
        std::lock_guard<std::mutex> lock(mutex);
    
        if( i == 0 ) // <= Complain about identicalInnerCondition
        {
          i = 1;
        }
      }
    }
    

    Which would be true for a mono-threaded process or a non-static local variable.
    But in the case of concurrent call to func(), i could have change between the two if's.

     

    Last edit: Lionel Gimbert 2020-12-28

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.