nimble-code / Cobra

An interactive (fast) static source code analyzer
139 stars 31 forks source link

Broken Pattern? #10

Closed TomMD closed 3 years ago

TomMD commented 3 years ago

I think I found a broken pattern but it could easily be my lack of understanding of the pattern language.

Consider:

void func3()
{
    void *x;
    curl_easy_getinfo(x);
    x = curl_easy_init();
}

It seems we should be able to match this patter of f(x) ; x = g() or a more generalized f(x ...) ; ... ; x = g() using the pattern:

cobra -pat '{ .*  curl_easy_getinfo ( .* x:@ident .* ) ^:x* :x = curl_easy_init ( ) .* }' b.c

But this yields no output. It seems that generalization from f(x) to f(x,y) or f(x,y,z) really threw us for a loop because this does work:

cobra -pat '{ .*  curl_easy_getinfo ( .* x:@ident ) ^:x* :x = curl_easy_init ( ) .* }'

Notice we removed .* after x:@ident.

This is certainly contrary to the english specification on the website ". matches zero or more" since there are zero tokens and . should never make a match that did succeed suddenly fail. Is it a bug? My misunderstanding? Is there another way to approach the pattern?

TomMD commented 3 years ago

OK, I've now come up with a better pattern:

{ .* curl_easy_getinfo ( ^)* x:@ident ^)* ) ^:x* :x = curl_easy_init ( ) .* }

I get how .* could have been overly greedy. Still not sure if I solved this correctly so I'll leave my ticket open for a little hoping for an education.

nimble-code commented 3 years ago

That's an interesting example. You are right that the problem seems to be that the . is greedy and tries to read past the closing ) of the argument list -- but it shouldn't because the closing ) that is part of the pattern should be a match (at the same level of nesting) to the opening ( of the argument list. Using ^) before the x:@ident alone also fixes it in my version (slightly updated from the one now on github). I'll put this on my list of things to look into more closely - to figure out why the pattern matching algorithm behaves this way. It shouldn't. Good example.

nimble-code commented 3 years ago

found and fixed the problem. after some more testing, i'll be uploading the fix later today thanks for the example!

nimble-code commented 3 years ago

should be fixed now, with the last updates