• Immutable Page
  • Info
  • Attachments

roelkluin/gres examples

To search for an erroneous pattern like this:

for (i = 0; i < n; i++) {}
...
if (i > n) ...

This is wrong because at the end of the loop i equals n and cannot be greater than n. To match this:

gres -A40 "^ for \(" \
"for \( (@V) = @d ; \1 < @d ; \1 \+\+ \)
    \{.8.\}
@n
if \( \1 > \3 \)" | less

The first string is used to get the git grep query. To see what is queried for execute

bli2 "^ for \("

It will print the extended regexp used in the query.

@V is translated to [[:alpha:]_]+[[:alnum:]_]*, which matches a local variable. @d matches any number in several formats, therefore it contains parentheses, that's why to match the second @d we use \3 as a back-reference. \{.8.\} matches anything between curly brackets, up to 8 nested curly brackets.

This results after about 10 seconds in a match:

---[ vi drivers/mmc/host/s3cmci.c +1209 ]---
        /* Set clock */
        for (mci_psc = 0; mci_psc < 255; mci_psc++) {
                ...
        }

        if (mci_psc > 255)
                mci_psc = 255;
        ...

Quite harmless so I left it.

I left this simple for explanatory reasons. A more advanced query to catch these errors, and others, can be found here gres_tests/loops.

Tell others about this page:

last edited 2010-02-18 21:31:07 by roelkluin