Menu

checking enter/leave

sfjimmyo
2022-02-11
2022-02-14
  • sfjimmyo

    sfjimmyo - 2022-02-11

    Hello,
    is there a way to check follow

    void test()
    {
    ....
    if (c)
    return; // ok: no os_leave needed, because there is no os_enter before

    if (d) {
    os_leave(); // ERROR os_leave without a os_enter()
    return;
    }

    os_enter();
    if (a) {
    os_leave();
    return; // OK ,
    }
    if (b) {
    return; // BUG os_leave missing
    }
    os_leave();
    }

    os_enter / os_leave has a internal reference counter os_enter=++ and os_leave=--
    Thanks

     
    • Daniel Marjamäki

      I don't really remember. I have the feeling we discussed this some 10-15 years ago I don't remember if we did anything.

      wild idea you could define preprocessor macros..

      #ifdef __cppcheck__
      // macros for cppcheck analysis
      #define check_os_enter_leave()  void* __checkPtr 
      #define os_enter()  __checkPtr = malloc(100);
      #define os_leave()  free(__checkPtr);
      #else
      // macros used in compilation
      #define check_os_enter_leave()
      #endif
      
      void test()
      {
          check_os_enter_leave();
      
          if (c)
              return; // ok
      
          if (d) {
              os_leave(); // ERROR os_leave without a os_enter()
              return;
          }
      
          os_enter();
          if (a) {
              os_leave();
              return; // OK ,
          }
          if (b) {
              return; // BUG os_leave missing
          }
          os_leave();
      }
      

      You will get a bit strange errors about uninitialized variable usage / memory leak.. but you will see errors.

       

      Last edit: Daniel Marjamäki 2022-02-12
  • Daniel Marjamäki

    note that the drawback is that you must call check_os_enter_leave(); at the start of the function otherwise this hack does not work.

    As I said I don't remember exactly what builtin checking we have for this. possibly there is something better..

     
  • sfjimmyo

    sfjimmyo - 2022-02-14

    thanks, i will test your solution.

     

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.