All of lore.kernel.org
 help / color / mirror / Atom feed
From: okuznik@symas.com (Ondřej Kuzník)
To: cocci@systeme.lip6.fr
Subject: [Cocci] Matching format strings
Date: Tue, 11 Jul 2017 15:25:44 +0100	[thread overview]
Message-ID: <20170711142544.dodc2awelxffs6oi@eos.mistotebe.net> (raw)
In-Reply-To: <WM!c47e11010083686c9aadfde329eb4f31090053d095a60ac9df8bbc8224796e493af84b1948709a4b0a11aa0da4f34967!@mailstronghold-2.zmailcloud.com>

On Tue, Jul 11, 2017 at 03:10:36PM +0200, Julia Lawall wrote:
> On Tue, 11 Jul 2017, Ond?ej Kuzn?k wrote:
>> On Mon, Jul 10, 2017 at 07:26:36PM +0200, Julia Lawall wrote:
>>> Try running spatch --parse-c on your directory to get a list at the end of
>>> the top 10 things it doesn't like.
>>
>> One of the system dependent macros SLAP_EVENT_DECL is listed in each of
>> the 10 things. The macro is used in
>> https://github.com/openldap/openldap/blob/master/servers/slapd/daemon.c#L2343
>> to declare some variables used in the corresponding event loop.
>>
>> The macro definition looks like this:
>>
>> # define SLAP_EVENT_DECL		struct pollfd *revents
>>
>> or:
>>
>> # define SLAP_EVENT_DECL	fd_set readfds, writefds; char *rflags
>>
>> Ignoring this macro is OK I guess, if there is a way to do that with
>> coccinelle?
> 
> Just
> 
> #define SLAP_EVENT_DECL
> 
> in the .h file. But there will be stray ;s then.  So you might want to put
> the whole first definition.

It does seem to improve things, thanks. The line 2496 still doesn't
apply even when I declare SLAP_EVENT_FNAME to something like "name" in
-macro-file-builtins and I think there were similar ones in other files
later. 

>> Also, moving onto the other patch, I get similar inconsistencies with
>> ./servers/slapd/back-sql/add.c, --parse-c doesn't list any problematic
>> statements but the output logs a few parse errors that don't make much
>> sense to me.
> 
> Could you send the complete semantic patch that you are currently
> considering?  Then I can take a look.

Attaching the patches I'm currently working with:
(-macro-file-builtins macros.h)
1. variadic.cocci
2. shortcut.cocci
3. merge.cocci

The back-sql issue above is when trying to apply 2. (shortcut.cocci).
But the one I pasted in my last email is simple enough to illustrate the
issue (trying to merge snprintf and Debug, then get rid of extra if()s)
even without the python stuff.

Thanks,

-- 
Ond?ej Kuzn?k
Senior Software Engineer
Symas Corporation                       http://www.symas.com
Packaged, certified, and supported LDAP solutions powered by OpenLDAP
-------------- next part --------------
A non-text attachment was scrubbed...
Name: macros.h
Type: text/x-chdr
Size: 106 bytes
Desc: not available
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20170711/3806a475/attachment.bin>
-------------- next part --------------
@@
identifier Logs =~ "Log[0-9]";
@@
-Logs
+Log

//identifier StatslogTest =~ "StatslogTest"; 
@@
@@
-StatslogTest
+LogTest

@@
format list[2] fmt;
expression list[2] args;
expression E;
@@

Debug( E, "%@fmt@", args
-, 0
 );

@@
format list[1] fmt;
expression list[1] args;
expression E;
@@

Debug( E, "%@fmt@", args
-, 0, 0
 );

@@
expression E, S;
@@

Debug( E, S
-, 0, 0, 0
 );

@@
format list[5] fmt;
expression list[5] args;
expression E;
@@

-Statslog
+Debug
 ( E, "%@fmt@", args );

@@
format list[4] fmt;
expression list[4] args;
expression E;
@@

-Statslog
+Debug
 ( E, "%@fmt@", args
-, 0
 );

@@
format list[3] fmt;
expression list[3] args;
expression E;
@@

-Statslog
+Debug
 ( E, "%@fmt@", args
-, 0, 0
 );

@@
format list[2] fmt;
expression list[2] args;
expression E;
@@

-Statslog
+Debug
 ( E, "%@fmt@", args
-, 0, 0, 0
 );

@@
format list[1] fmt;
expression list[1] args;
expression E;
@@

-Statslog
+Debug
 ( E, "%@fmt@", args
-, 0, 0, 0, 0
 );

@@
expression E, S;
@@

-Statslog
+Debug
 ( E, S
-, 0, 0, 0, 0, 0
 );

@@
identifier Stats =~ "^Statslog";
@@
(
 StatslogEtime
|
-Stats
+Debug
)
-------------- next part --------------
@initialize:python@
@@

#regex from https://stackoverflow.com/questions/30011379/how-can-i-parse-a-c-format-string-in-python
import re
fmtstring = '''\
(                                  # start of capture group 1
%                                  # literal "%"
(?:                                # first option
(?:[-+0 #]{0,5})                   # optional flags
(?:\d+|\*)?                        # width
(?:\.(?:\d+|\*))?                  # precision
(?:h|l|ll|w|I|I32|I64)?            # size
[cCdiouxXeEfgGaAnpsSZ]             # type
) |                                # OR
%%)                                # literal "%%"
'''

regex = re.compile(fmtstring, re.X)

def parse_format(f):
    return tuple((m.span(), m.group()) for m in
        regex.finditer(f))

// covered by others but processing that can take hours on some files
@shortcut@
type T;
identifier buf;
expression I, E, L;
expression list args_before, args, args_after;
expression format1, format2;
position p1, p2;
@@

(
{
\( T buf; \| T buf = I; \| \)
snprintf at p1( buf, E, format1, args );
Debug at p2( L, format2, args_before, buf, args_after );
}
|
{
\( T buf; \| T buf = I; \| \)
snprintf at p1( buf, E, format1, args );
Debug at p2( L, format2, args_before, buf, args_after );
}
)

@script:python shortcut_process@
format1 << shortcut.format1;
format2 << shortcut.format2;
args_before << shortcut.args_before;
merged;
@@

pos = len(args_before.elements)
formats = parse_format(format2)

span, format = formats[pos]
merged = format2[:span[0]] + format1.strip('"') + format2[span[1]:]

coccinelle.merged = merged

@shortcut_replace@
position shortcut.p1, shortcut.p2;
identifier shortcut_process.merged;

type T;
identifier buf;
expression I, E, L;
expression list args_before, args, args_after;
expression format1, format2;
@@

(
-{
-\( T buf; \| T buf = I; \| \)
-snprintf at p1( buf, E, format1, args );
-Debug at p2( L, format2, args_before, buf, args_after );
+Debug( L, merged, args_before, args, args_after );
-}
|
{
-\( T buf; \| T buf = I; \| \)
-snprintf at p1( buf, E, format1, args );
-Debug at p2( L, format2, args_before, buf, args_after );
+Debug( L, merged, args_before, args, args_after );
}
)

@useless_if@
expression L;
@@

-if ( LogTest( L ) ) {
 Debug( L, ... );
-}
-------------- next part --------------
@initialize:python@
@@

#regex from https://stackoverflow.com/questions/30011379/how-can-i-parse-a-c-format-string-in-python
import re
fmtstring = '''\
(                                  # start of capture group 1
%                                  # literal "%"
(?:                                # first option
(?:[-+0 #]{0,5})                   # optional flags
(?:\d+|\*)?                        # width
(?:\.(?:\d+|\*))?                  # precision
(?:h|l|ll|w|I|I32|I64)?            # size
[cCdiouxXeEfgGaAnpsSZ]             # type
) |                                # OR
%%)                                # literal "%%"
'''

regex = re.compile(fmtstring, re.X)

def parse_format(f):
    return tuple((m.span(), m.group()) for m in
        regex.finditer(f))

@a exists@
expression lock, E, L;
expression list args_before, args, args_after;
identifier buf;
expression format1, format2;
type T;
position p1, p2;
@@

{
...
T buf;
...
ldap_pvt_thread_mutex_lock(lock);
...
snprintf at p1( buf, E, format1, args );
...
ldap_pvt_thread_mutex_unlock(lock);
...
Debug at p2( L, format2, args_before, buf, args_after );
...
}

@script:python a_process@
format1 << a.format1;
format2 << a.format2;
args_before << a.args_before;
merged;
@@

pos = len(args_before.elements)
formats = parse_format(format2)

span, format = formats[pos]
merged = format2[:span[0]] + format1.strip('"') + format2[span[1]:]

coccinelle.merged = merged

@a_replace@
position a.p1, a.p2;
identifier a_process.merged;

expression lock, E, L;
expression list args_before, args, args_after;
identifier buf;
expression format1, format2;
type T;
@@

{
...
-T buf;
...
ldap_pvt_thread_mutex_lock(lock);
...
-snprintf at p1( buf, E, format1, args );
+Debug( L, merged, args_before, args, args_after );
...
ldap_pvt_thread_mutex_unlock(lock);
...
-Debug at p2( L, format2, args_before, buf, args_after );
...
}

@b exists@
expression E, L;
expression list args_before, args, args_after;
identifier buf;
expression format1, format2;
position p1, p2;
@@

snprintf at p1( buf, E, format1, args );
...
Debug at p2( L, format2, args_before, buf, args_after );

@script:python b_process@
format1 << b.format1;
format2 << b.format2;
args_before << b.args_before;
merged;
@@

pos = len(args_before.elements)
formats = parse_format(format2)

span, format = formats[pos]
merged = format2[:span[0]] + format1.strip('"') + format2[span[1]:]

coccinelle.merged = merged

@b_replace@
position b.p1, b.p2;
identifier b_process.merged;

expression E, L;
expression list args_before, args, args_after;
identifier buf;
expression format1, format2;
@@

-snprintf at p1( buf, E, format1, args );
+Debug( L, merged, args_before, args, args_after );
...
-Debug@p2( L, format2, args_before, buf, args_after );

  parent reply	other threads:[~2017-07-11 14:25 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-29 16:04 [Cocci] Matching format strings Ondřej Kuzník
2017-06-29 16:31 ` Julia Lawall
     [not found]   ` <WM!fa141e32a0b1e5356bb136cefca80be54d02a269aa1d5e1c1d01b9dd1e9da14f2f9fffdc08676c298a48132a86f2037e!@mailstronghold-1.zmailcloud.com>
2017-06-29 17:58     ` Ondřej Kuzník
2017-06-29 20:07       ` Julia Lawall
2017-06-29 23:40       ` Julia Lawall
     [not found]         ` <WM!a6114e049b67df39e40551cd95bf5d65e432ff2f8c0445b0273a6b12349ae6346924a09e4c027fdcc87febe26a5e6a4e!@mailstronghold-2.zmailcloud.com>
     [not found]           ` <20170630135727.lyjp7ayalzxf73jf@eos.mistotebe.net>
2017-07-04 13:22             ` Ondřej Kuzník
2017-07-04 13:32               ` Julia Lawall
     [not found]                 ` <WM!8666006bd8d2547072f2aaa49a217ebee942918ea85a26184b30aa0c245a807e35f125c1045776851fe6d85360d3ed76!@mailstronghold-3.zmailcloud.com>
2017-07-04 13:50                   ` Ondřej Kuzník
2017-07-04 13:53                     ` Julia Lawall
     [not found]                       ` <WM!65159de96edf810da1e56d42962da09a256f7f76b7d8eab05109b214b5dc8c18f88b6a34ac273725a5e7afd1a6bed1d7!@mailstronghold-2.zmailcloud.com>
2017-07-04 15:11                         ` Ondřej Kuzník
2017-07-04 15:25                           ` Julia Lawall
     [not found]                             ` <WM!e764e7a6685d1e3af9c59f905772f1c4a8db9db4c655054ccb07f1b8485096c0979716269a6920eeac860b62d25a700e!@mailstronghold-2.zmailcloud.com>
2017-07-04 16:01                               ` Ondřej Kuzník
2017-07-04 16:09                                 ` Julia Lawall
     [not found]                                   ` <WM!cc508729d042cf12207adee814670b88c105ae73cdaf601fdeb9c3e26fa32e7df0d4d49f847a216810550421de0d8de6!@mailstronghold-3.zmailcloud.com>
2017-07-04 16:27                                     ` Ondřej Kuzník
2017-07-04 16:40                                       ` Julia Lawall
     [not found]                                         ` <WM!eb7db9be9e260b8b332afe997cbb4412d355372680c68c06376375f785c138afeda43804b4aaff43c0242a17ff2d826f!@mailstronghold-3.zmailcloud.com>
2017-07-04 16:56                                           ` Ondřej Kuzník
2017-07-04 16:59                                             ` Julia Lawall
     [not found]                                               ` <WM!6445a14b41a047e62e6e93d9a0da969d5ea2daf83555655349684d54f3d26f5ca8795d092a053f4ee93c2a22f577a788!@mailstronghold-1.zmailcloud.com>
2017-07-04 17:46                                                 ` Ondřej Kuzník
2017-07-04 17:53                                                   ` Julia Lawall
     [not found]                                                     ` <WM!afea88a820c9c853667fab8a120e0e62e64bc1c9aacab0e42f4149cedda4906e7b85ddc1f742afe16ea78098b42a61f6!@mailstronghold-3.zmailcloud.com>
2017-07-04 19:17                                                       ` Ondřej Kuzník
2017-07-04 21:05                                                         ` Julia Lawall
2017-07-04 21:09                                                           ` Julia Lawall
2017-07-05 12:20                                                         ` Julia Lawall
     [not found]                                                           ` <WM!da212a33f363d5e8666de4eea7afef6ff657b4ba0eea72f6c914a365ab717eec189b136e17bf2c01cc76cdde89f25962!@mailstronghold-2.zmailcloud.com>
2017-07-05 16:25                                                             ` Ondřej Kuzník
2017-07-05 18:33                                                               ` Julia Lawall
2017-07-05 18:39                                                               ` Julia Lawall
2017-07-05 20:38                                                               ` Julia Lawall
     [not found]                                                                 ` <WM!3b412209b28e186e2cf1ceaaa46d40e6d1947012ed574b129f9c461a255f53e26f88920405ecdf4a7d052d70724bc64e!@mailstronghold-2.zmailcloud.com>
2017-07-10 16:12                                                                   ` Ondřej Kuzník
2017-07-10 16:18                                                                     ` Julia Lawall
     [not found]                                                                       ` <WM!18ce1dcc914ca4f9b61f6c3aaf891296df9651e3b4d79666e0682dd2ff392a761c732ee0cf4f6500e93b392f83991daf!@mailstronghold-1.zmailcloud.com>
2017-07-10 16:28                                                                         ` Ondřej Kuzník
2017-07-10 17:26                                                                           ` Julia Lawall
     [not found]                                                                             ` <WM!5b84796296144341d624902eddc2fab8cb72294094f53296fafa12642d6a73a74f5216fe1a48aa1018fdaf2162eca726!@mailstronghold-2.zmailcloud.com>
2017-07-11 13:06                                                                               ` Ondřej Kuzník
2017-07-11 13:10                                                                                 ` Julia Lawall
     [not found]                                                                                   ` <WM!c47e11010083686c9aadfde329eb4f31090053d095a60ac9df8bbc8224796e493af84b1948709a4b0a11aa0da4f34967!@mailstronghold-2.zmailcloud.com>
2017-07-11 14:25                                                                                     ` Ondřej Kuzník [this message]
2017-07-11 19:24                                                                                       ` Julia Lawall
     [not found]                                                                                         ` <WM!722e339a1005fef2c134dd9ac0ae6a397244b31d97d213bd70f018388df0843856b835981db31cfffbbceb8ea4d36bdf!@mailstronghold-2.zmailcloud.com>
2017-07-11 22:05                                                                                           ` Ondřej Kuzník
2017-07-11 22:17                                                                                             ` Ondřej Kuzník
2017-07-12  5:48                                                                                               ` Julia Lawall
2017-07-04 16:25                                 ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170711142544.dodc2awelxffs6oi@eos.mistotebe.net \
    --to=okuznik@symas.com \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.