cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Strace Labs <stracelabs@gmail.com>
Cc: cocci@systeme.lip6.fr
Subject: Re: [Cocci] Replacing printf() parameters according to used data types
Date: Sat, 30 Nov 2019 07:35:39 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.21.1911300733180.2588@hadrien> (raw)
In-Reply-To: <CABvP5W3hmmovw09n6gJQ5m=Cps2YAYJFFfOPmKY6Zu_SJOU=NQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3268 bytes --]



--- Please note the new email address ---


On Sat, 30 Nov 2019, Strace Labs wrote:

> Hi Julia,
>
> Thanks for the suggestion. But, It is working partially... I am not sure if
> Coccinelle is able to filter the %fmt e.g: %s only called with a parameter
> of specific data-type. In my case, {struct mydata }.name and { struct mydata
> *}->name.
>
> #####  Cocci
> @r1@
> format list d;
> identifier fn;
> @@
>
> fn("%@d@", ...)

There is no need to put ... here if you want to specify something else.

julia

> @script:ocaml s1@
> d << r1.d;
> res;
> @@
> res := make_expr ("\""^(String.concat "%m"
> (Str.split_delim(Str.regexp_string "%s") d))^"\"")
>
> @main depends on s1 && r1@
> format list r1.d;
> expression s1.res;
> identifier r1.fn;
> struct mydata SMD;
> struct mydata* SMDP;
> @@
>
>  fn(
> -"%@d@"
> +res
> ,
> (
> + &
>   SMD
> - .name
> |
>   SMDP
> - ->name
> )
>  );
>  
> #####  .c Code sample
> int foo() {
> int id;
> struct mydata h1, *h2, s1, *s2;
> char *city;
>
> // works fine
> my_printf("%s", s2->name);
>
> // works without any criterions about the data type of %fmt
> my_printf("sss %s gggg", h1.name);
> my_printf("33131231313 %d %s %d %s hhhhh", id, s1.name, (*h2)->name,
> h2->name);
> my_printf("aaaa %s hhhhh", h2->name);
> my_printf("%s", s2->name);
>
> // should do nothing
> my_printf("%s");
> my_printf("%s", city);
>
> // don't match.
> my_printf("a %s %d", h2->name, id);
> my_printf("ddddd %s %s %s", h2->name, city, h2->name);
> my_printf("%d it would work but dunno mydata=%m\n", id, h2);
> my_printf("%d here also, tt=%s | %s and %m\n", id, h2->name, h2->name, s2);
> }
>
> #### Result
> HANDLING: /Volumes/Users/test/Coccinella/sample.c
> diff =
> --- /Volumes/Users/test/Coccinella/sample.c
> +++ /tmp/cocci-output-99329-3a9829-sample.c
> @@ -5,13 +5,13 @@ int foo() {
>   char *city;
>
>   // works fine
> - my_printf("%s", s2->name);
> + my_printf("%m", s2);
>
>   // works without any criterious about the data type of %fmt
> - my_printf("sss %s gggg", h1.name);
> + my_printf("sss %m gggg", &h1);
>   my_printf("33131231313 %d %s %d %s hhhhh", id, s1.name, (*h2)->name,
> h2->name);
> - my_printf("aaaa %s hhhhh", h2->name);
> - my_printf("%s", s2->name);
> + my_printf("aaaa %m hhhhh", h2);
> + my_printf("%m", s2);
>
>   // should do nothing
>   my_printf("%s");
>
>
> Basically, I intend to replace alls "%s" called with "mydata->name" by "%m"
> with "mydata" or "&mydata"
>
>
>
>
>
> On Fri, Nov 29, 2019 at 6:55 PM Julia Lawall <julia.lawall@inria.fr> wrote:
>       Maybe this will help you:
>
>       @r@
>       format list d;
>       @@
>
>       "%@d@"
>
>       @script:ocaml s@
>       d << r.d;
>       res;
>       @@
>       res := make_expr ("\""^(String.concat "%s" (Str.split_delim
>       (Str.regexp_string "%d") d))^"\"")
>
>
>       @@
>       format list r.d;
>       expression s.res;
>       @@
>
>       -"%@d@"
>       +res
>
>       ---------------
>
>       Example:
>
>       int main() {
>         printf("some %d more\n", 12);
>       }
>
>       int main() {
>         printf("%d more\n", 12);
>       }
>
>       int main() {
>         printf("more %d\n", 12);
>       }
>
>       int main() {
>         printf("%d more %d\n", 12);
>       }
>
>       julia
>
>
>

[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  reply	other threads:[~2019-11-30  6:36 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-28  2:11 [Cocci] Replacing printf/format calls based on the data-type Strace Labs
2019-11-28  7:07 ` Julia Lawall
2019-11-28 17:45   ` Strace Labs
2019-11-29 14:48   ` [Cocci] Replacing printf() parameters according to used data types Markus Elfring
2019-11-28  7:50 ` Markus Elfring
2019-11-29  0:35   ` Jorge Pereira
2019-11-29  8:29     ` Markus Elfring
2019-11-29 10:57       ` Strace Labs
2019-11-29 12:33         ` Markus Elfring
2019-11-29 14:47           ` Strace Labs
2019-11-29 16:08             ` Markus Elfring
2019-11-29 17:19               ` Strace Labs
2019-11-29 17:45                 ` Markus Elfring
2019-11-29 20:55             ` Julia Lawall
2019-11-30  2:25               ` Strace Labs
2019-11-30  6:35                 ` Julia Lawall [this message]
2019-11-30  8:46                 ` Markus Elfring
2019-12-01  8:00                 ` [Cocci] Changing format string usage with SmPL? Markus Elfring
2019-12-03  3:30                   ` Strace Labs
2019-12-03  5:18                     ` Julia Lawall
2019-12-03 13:28                       ` Markus Elfring
2019-12-03 15:43                       ` [Cocci] Generation of expression lists by SmPL script rules? Markus Elfring
2019-12-03 17:28                       ` [Cocci] Changing format string usage with SmPL? Strace Labs
2019-12-04  0:21                         ` Strace Labs
2019-12-06 19:36                           ` Markus Elfring
2019-12-07  7:49                           ` Markus Elfring
2019-12-04  6:47                         ` Julia Lawall
2019-12-06 19:44                           ` Markus Elfring
2019-12-06 19:20                         ` Markus Elfring
2019-12-03 10:01                     ` Markus Elfring
2019-11-30 15:11               ` [Cocci] Replacing printf() parameters according to used data types Markus Elfring

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=alpine.DEB.2.21.1911300733180.2588@hadrien \
    --to=julia.lawall@inria.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=stracelabs@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).