Coccinelle archive on lore.kernel.org
 help / color / Atom feed
From: Strace Labs <stracelabs@gmail.com>
To: Julia Lawall <julia.lawall@inria.fr>
Cc: cocci@systeme.lip6.fr
Subject: Re: [Cocci] Replacing printf/format calls based on the data-type
Date: Thu, 28 Nov 2019 15:45:34 -0200
Message-ID: <CABvP5W0oeXsxhe3aOzGaVOeNHnMogk+pROVAvEiL53w+wdA04g@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.21.1911280807030.3704@hadrien>

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

Hi Julia,

I am not sure if I was clear. but, let me try to explain again.

1. I have the code.

$ cat sample.c
int foo() {
int id;
struct mydata h1, *h2, s1, *s2;

// works fine
  my_printf("%s", h1.name);
  my_printf("%s", h2->name);

// don't work
my_printf("%d it would work but dunno mydata=%m\n", id, h2);
my_printf("%d here also, tt=%s and %m\n", id, h2->name, s2);
}
$

2. My fix-format.cocci

$ cat fix-format.cocci
@r1_heap@
struct mydata *SMD;
format F =~ "s";
expression f;
@@
-f("%@F@", SMD->name);
+f("%m", SMD);

@r1_stack@
struct mydata SMD;
format F =~ "s";
expression f;
@@
-f("%@F@", SMD.name);
+f("%m", &SMD);

$

3. therefore, It is not matching with *my_printf("%d it would work but
dunno mydata=%m\n", id, h2);*

e.g:

$ spatch --sp-file fix-format.cocci sample.c
HANDLING: /tmp/sample.c
diff =
--- /tmp/sample.c
+++ /tmp/T/cocci-output-92962-556a08-sample.c
@@ -4,8 +4,8 @@ int foo() {
  struct mydata h1, *h2, s1, *s2;

  // works fine
- my_printf("%s", h1.name);
- my_printf("%s", h2->name);
+ my_printf("%m", &h1);
+ my_printf("%m", h2);

  // don't work
  my_printf("%d it would work but dunno mydata=%m\n", id, h2);
$

so, is it possible to fix my format-string?

On Thu, Nov 28, 2019 at 5:08 AM Julia Lawall <julia.lawall@inria.fr> wrote:

>
>
> --- Please note the new email address ---
>
>
> On Thu, 28 Nov 2019, Strace Labs wrote:
>
> > Hi,
> > I am working on a semantic patch for replacing specific format string
> token based on the used data-type.
> > so, I have several calls of my_printf() and some special macros pointing
> to them around my code.
> >
> > e.g: part of my code.
> > .....
> > struct mydata *m;
> > struct mydata h;
> > .....
> > my_printf("%s", m->name);
> > .....
> > my_printf("%s", h.name);
> > .....
> > my_printf("whatever id %d following the string %s\n", id, m->name);
> > ....
> > Macro_to_my_printf("Hey id %d, let's see %s\n", id, h.name);
> > .....
> > Macro2_to_my_printf(fd, "Hey id %d, let's see %s\n", id, m->name);
> > .....
> >
> > My current humble *.cocci
> >
> > $ cat fix-my_printf.cocci
> > @r1_heap@
> > struct mydata *SMD;
> > format F =~ "s";
> > @@
> > -my_printf("%@F@", SMD->name);
> > +my_printf("%m", SMD);
> >
> > @r1_stack@
> > struct mydata SMD;
> > format F =~ "s";
> > @@
> > -my_printf("%@F@", SMD.name);
> > +my_printf("%m", &SMD);
> > $
> >
> > But, I can match only with partial content as can be seen below.
> >
> > $ spatch --partial-match --sp-file fix-my_printf.cocci
> sample-format-string.c | egrep "^(\+|-)"
> > HANDLING: sample-format-string.c
> > diff =
> > HANDLING: /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c
> > diff =
> > --- /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c
> > +++
> /var/folders/ld/6tg9c6qj4fx4c85q26mcqrsh0000gn/T/cocci-output-24659-130f86-sample-format-string.c
> > - my_printf("%s", m->name);
> > + my_printf("%m", m);
> > - my_printf("%s", h.name);
> > + my_printf("%m", &h);
> > $
> >
> > Anyone could give me a light about how to proceed to match the entire
> ".....string format..." ?
>
> I think that what you are asking is why you can't write a pattern like:
>
> foo("...
> -%@d@
> +%x
>   ...")
>
> At the moment, there seems to be a bug.  I will check on that.
>
> julia

[-- Attachment #1.2: Type: text/html, Size: 4946 bytes --]

<div dir="ltr"><div dir="ltr">Hi Julia,<div><br></div><div>I am not sure if I was clear. but, let me try to explain again.</div><div><br></div><div>1. I have the code.</div><div><br></div><div>$ cat sample.c<br>int foo() {<br>	int id;<br>	struct mydata h1, *h2, s1, *s2;<br><br>	// works fine<br> 	my_printf(&quot;%s&quot;, <a href="http://h1.name">h1.name</a>);<br> 	my_printf(&quot;%s&quot;, h2-&gt;name);<br><br>	// don&#39;t work<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 and %m\n&quot;, id, h2-&gt;name, s2);<br>}<br>$<br></div><div><br></div><div>2. My fix-format.cocci</div><div><br></div><div>$ cat fix-format.cocci<br>@r1_heap@<br>struct mydata *SMD;<br>format F =~ &quot;s&quot;;<br>expression f;<br>@@<br>-f(&quot;%@F@&quot;, SMD-&gt;name);<br>+f(&quot;%m&quot;, SMD);<br><br>@r1_stack@<br>struct mydata SMD;<br>format F =~ &quot;s&quot;;<br>expression f;<br>@@<br>-f(&quot;%@F@&quot;, SMD.name);<br>+f(&quot;%m&quot;, &amp;SMD);<br><br>$<br></div><div><br></div><div>3. therefore, It is not matching with <b>my_printf(&quot;%d it would work but dunno mydata=%m\n&quot;, id, h2);</b></div><div><br></div><div>e.g:</div><div><br></div><div>$ spatch --sp-file fix-format.cocci sample.c<br>HANDLING: /tmp/sample.c<br>diff =<br>--- /tmp/sample.c<br>+++ /tmp/T/cocci-output-92962-556a08-sample.c<br>@@ -4,8 +4,8 @@ int foo() {<br> 	struct mydata h1, *h2, s1, *s2;<br><br> 	// works fine<br>- 	my_printf(&quot;%s&quot;, <a href="http://h1.name">h1.name</a>);<br>- 	my_printf(&quot;%s&quot;, h2-&gt;name);<br>+ 	my_printf(&quot;%m&quot;, &amp;h1);<br>+ 	my_printf(&quot;%m&quot;, h2);<br><br> 	// don&#39;t work<br> 	my_printf(&quot;%d it would work but dunno mydata=%m\n&quot;, id, h2);<br>$<br></div><div><br></div><div>so, is it possible to fix my format-string?</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 28, 2019 at 5:08 AM 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"><br>
<br>
--- Please note the new email address ---<br>
<br>
<br>
On Thu, 28 Nov 2019, Strace Labs wrote:<br>
<br>
&gt; Hi,<br>
&gt; I am working on a semantic patch for replacing specific format string token based on the used data-type.<br>
&gt; so, I have several calls of my_printf() and some special macros pointing to them around my code.<br>
&gt;<br>
&gt; e.g: part of my code.<br>
&gt; .....<br>
&gt; struct mydata *m;<br>
&gt; struct mydata h;<br>
&gt; .....<br>
&gt; my_printf(&quot;%s&quot;, m-&gt;name);<br>
&gt; .....<br>
&gt; my_printf(&quot;%s&quot;, <a href="http://h.name" rel="noreferrer" target="_blank">h.name</a>);<br>
&gt; .....<br>
&gt; my_printf(&quot;whatever id %d following the string %s\n&quot;, id, m-&gt;name);<br>
&gt; ....<br>
&gt; Macro_to_my_printf(&quot;Hey id %d, let&#39;s see %s\n&quot;, id, <a href="http://h.name" rel="noreferrer" target="_blank">h.name</a>);<br>
&gt; .....<br>
&gt; Macro2_to_my_printf(fd, &quot;Hey id %d, let&#39;s see %s\n&quot;, id, m-&gt;name);<br>
&gt; .....<br>
&gt;<br>
&gt; My current humble *.cocci<br>
&gt;<br>
&gt; $ cat fix-my_printf.cocci<br>
&gt; @r1_heap@<br>
&gt; struct mydata *SMD;<br>
&gt; format F =~ &quot;s&quot;;<br>
&gt; @@<br>
&gt; -my_printf(&quot;%@F@&quot;, SMD-&gt;name);<br>
&gt; +my_printf(&quot;%m&quot;, SMD);<br>
&gt;<br>
&gt; @r1_stack@<br>
&gt; struct mydata SMD;<br>
&gt; format F =~ &quot;s&quot;;<br>
&gt; @@<br>
&gt; -my_printf(&quot;%@F@&quot;, SMD.name);<br>
&gt; +my_printf(&quot;%m&quot;, &amp;SMD);<br>
&gt; $<br>
&gt;<br>
&gt; But, I can match only with partial content as can be seen below.<br>
&gt;<br>
&gt; $ spatch --partial-match --sp-file fix-my_printf.cocci sample-format-string.c | egrep &quot;^(\+|-)&quot;<br>
&gt; HANDLING: sample-format-string.c<br>
&gt; diff =<br>
&gt; HANDLING: /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c<br>
&gt; diff =<br>
&gt; --- /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c<br>
&gt; +++ /var/folders/ld/6tg9c6qj4fx4c85q26mcqrsh0000gn/T/cocci-output-24659-130f86-sample-format-string.c<br>
&gt; - my_printf(&quot;%s&quot;, m-&gt;name);<br>
&gt; + my_printf(&quot;%m&quot;, m);<br>
&gt; - my_printf(&quot;%s&quot;, <a href="http://h.name" rel="noreferrer" target="_blank">h.name</a>);<br>
&gt; + my_printf(&quot;%m&quot;, &amp;h);<br>
&gt; $<br>
&gt;<br>
&gt; Anyone could give me a light about how to proceed to match the entire &quot;.....string format...&quot; ?<br>
<br>
I think that what you are asking is why you can&#39;t write a pattern like:<br>
<br>
foo(&quot;...<br>
-%@d@<br>
+%x<br>
  ...&quot;)<br>
<br>
At the moment, there seems to be a bug.  I will check on that.<br>
<br>
julia</blockquote></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 Strace Labs
2019-11-28  7:07 ` Julia Lawall
2019-11-28 17:45   ` Strace Labs [this message]
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
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=CABvP5W0oeXsxhe3aOzGaVOeNHnMogk+pROVAvEiL53w+wdA04g@mail.gmail.com \
    --to=stracelabs@gmail.com \
    --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