Detect unprotected code using cppdepend to audit missing outer guard in call chain?

184 Views Asked by At

I'm trying to detect code which is unprotected by a guard clause at a higher level in its callers. I think I can get quite close with cppdepend but unsure if I can precisely identify problems.

I have a bunch of code where a locking protection is required so we have an assertion at the innermost functions:

assert(eventQueue.IsCurrentEventGuarded());  

Note that eventQueue is a static instance of type EventQueue.

I want to detect all places where the functions containing that assertion are invoked and there is not an instance of a guard object at some point in their caller chain.

So, it is OK if at some point in the caller chain there is an expression:

GSEventGuard guard;

However, if the call to IsCurrentEventGuarded occurs without that protective instance somewhere above it, we have a problem that should be detected.

I can find the upper-level Guarding Methods which create the guard (ie: good ones we can ignore) with a clause:

SELECT METHODS WHERE IsDirectlyUsing "GSEventGuard" AND 
   IsUsing "EventQueue.IsCurrentEventGuarded()"

Ideally I should be able to ignore everything above and below those methods.

A more interesting clause finds two sets of methods - those below the Guarding Methods (between a guard and the test) and those which are in the calling chains we want, unprotected.

SELECT METHODS WHERE
DepthOfIsUsing "EventQueue.IsCurrentEventGuarded()" > 1
AND !(IsUsing "GSEventGuard.try_lock()" OR IsUsing "GSEventGuard.lock()")
ORDER BY DepthOfIsUsing DESC

finds 65 methods which are either in the calling chain below a lock or higher up and unguarded

0

There are 0 best solutions below