Coccinelle archive on lore.kernel.org
 help / color / Atom feed
From: Strace Labs <stracelabs@gmail.com>
To: Julia Lawall <julia.lawall@inria.fr>
Cc: Markus Elfring <Markus.Elfring@web.de>, cocci@systeme.lip6.fr
Subject: Re: [Cocci] Replacing printf() parameters according to used data types
Date: Sat, 30 Nov 2019 00:25:43 -0200
Message-ID: <CABvP5W3hmmovw09n6gJQ5m=Cps2YAYJFFfOPmKY6Zu_SJOU=NQ@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.21.1911292154450.3140@hadrien>

[-- Attachment #1.1: Type: text/plain, Size: 2710 bytes --]

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@", ...)

@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 #1.2: Type: text/html, Size: 4320 bytes --]

<div dir="ltr"><div>Hi Julia,</div><div><br></div><div>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, <i>{struct mydata }.name</i> and <i>{ struct mydata *}-&gt;name</i>.</div><div><br></div><div>##### <b> Cocci</b></div><div>@r1@<br>format list d;<br>identifier fn;<br>@@<br><br>fn(&quot;%@d@&quot;, ...)<br><br>@script:ocaml s1@<br>d &lt;&lt; r1.d;<br>res;<br>@@<br>res := make_expr (&quot;\&quot;&quot;^(String.concat &quot;%m&quot; (Str.split_delim(Str.regexp_string &quot;%s&quot;) d))^&quot;\&quot;&quot;)<br><br>@main depends on s1 &amp;&amp; r1@<br>format list r1.d;<br>expression s1.res;<br>identifier r1.fn;<br>struct mydata SMD;<br>struct mydata* SMDP;<br>@@<br><br> fn(<br>-&quot;%@d@&quot;<br>+res<br>,<br>(<br>+ &amp;<br>  SMD<br>- .name<br>|<br>  SMDP<br>- -&gt;name<br>)<br> );<br> </div><div><b>#####  .c Code sample</b><br>int foo() {<br>	int id;<br>	struct mydata h1, *h2, s1, *s2;<br>	char *city;<br><br>	// works fine<br>	my_printf(&quot;%s&quot;, s2-&gt;name);<br><br>	// works without any criterions about the data type of %fmt<br>	my_printf(&quot;sss %s gggg&quot;, <a href="http://h1.name">h1.name</a>);<br>	my_printf(&quot;33131231313 %d %s %d %s hhhhh&quot;, id, <a href="http://s1.name">s1.name</a>, (*h2)-&gt;name, h2-&gt;name);<br>	my_printf(&quot;aaaa %s hhhhh&quot;, h2-&gt;name);<br>	my_printf(&quot;%s&quot;, s2-&gt;name);<br>	<br>	// should do nothing<br>	my_printf(&quot;%s&quot;);<br>	my_printf(&quot;%s&quot;, city);<br><br>	// don&#39;t match.<br>	my_printf(&quot;a %s %d&quot;, h2-&gt;name, id);<br>	my_printf(&quot;ddddd %s %s %s&quot;, h2-&gt;name, city, h2-&gt;name);<br>	my_printf(&quot;%d it would work but dunno mydata=%m\n&quot;, id, h2);<br>	my_printf(&quot;%d here also, tt=%s | %s and %m\n&quot;, id, h2-&gt;name, h2-&gt;name, s2);<br>}<br></div><div><br></div><div><b>#### Result</b></div><div>HANDLING: /Volumes/Users/test/Coccinella/sample.c<br>diff =<br>--- /Volumes/Users/test/Coccinella/sample.c<br>+++ /tmp/cocci-output-99329-3a9829-sample.c<br>@@ -5,13 +5,13 @@ int foo() {<br> 	char *city;<br><br> 	// works fine<br>-	my_printf(&quot;%s&quot;, s2-&gt;name);<br>+	my_printf(&quot;%m&quot;, s2);<br><br> 	// works without any criterious about the data type of %fmt<br>-	my_printf(&quot;sss %s gggg&quot;, <a href="http://h1.name">h1.name</a>);<br>+	my_printf(&quot;sss %m gggg&quot;, &amp;h1);<br> 	my_printf(&quot;33131231313 %d %s %d %s hhhhh&quot;, id, <a href="http://s1.name">s1.name</a>, (*h2)-&gt;name, h2-&gt;name);<br>-	my_printf(&quot;aaaa %s hhhhh&quot;, h2-&gt;name);<br>-	my_printf(&quot;%s&quot;, s2-&gt;name);<br>+	my_printf(&quot;aaaa %m hhhhh&quot;, h2);<br>+	my_printf(&quot;%m&quot;, s2);<br><br> 	// should do nothing<br> 	my_printf(&quot;%s&quot;);<br></div><div><br></div><div><br></div><div>Basically, I intend to replace alls &quot;%s&quot; called with &quot;mydata-&gt;name&quot; by &quot;%m&quot; with &quot;mydata&quot; or &quot;&amp;mydata&quot;</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 29, 2019 at 6:55 PM Julia Lawall &lt;<a href="mailto:julia.lawall@inria.fr">julia.lawall@inria.fr</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Maybe this will help you:<br>
<br>
@r@<br>
format list d;<br>
@@<br>
<br>
&quot;%@d@&quot;<br>
<br>
@script:ocaml s@<br>
d &lt;&lt; r.d;<br>
res;<br>
@@<br>
res := make_expr (&quot;\&quot;&quot;^(String.concat &quot;%s&quot; (Str.split_delim<br>
(Str.regexp_string &quot;%d&quot;) d))^&quot;\&quot;&quot;)<br>
<br>
<br>
@@<br>
format list r.d;<br>
expression s.res;<br>
@@<br>
<br>
-&quot;%@d@&quot;<br>
+res<br>
<br>
---------------<br>
<br>
Example:<br>
<br>
int main() {<br>
  printf(&quot;some %d more\n&quot;, 12);<br>
}<br>
<br>
int main() {<br>
  printf(&quot;%d more\n&quot;, 12);<br>
}<br>
<br>
int main() {<br>
  printf(&quot;more %d\n&quot;, 12);<br>
}<br>
<br>
int main() {<br>
  printf(&quot;%d more %d\n&quot;, 12);<br>
}<br>
<br>
julia<br>
</blockquote></div></div></div>

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

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

  reply index

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 [this message]
2019-11-30  6:35                 ` Julia Lawall
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 publically 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='CABvP5W3hmmovw09n6gJQ5m=Cps2YAYJFFfOPmKY6Zu_SJOU=NQ@mail.gmail.com' \
    --to=stracelabs@gmail.com \
    --cc=Markus.Elfring@web.de \
    --cc=cocci@systeme.lip6.fr \
    --cc=julia.lawall@inria.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

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git