Coccinelle archive on lore.kernel.org
 help / color / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Strace Labs <stracelabs@gmail.com>
Cc: cocci <cocci@systeme.lip6.fr>
Subject: Re: [Cocci] Changing format string usage with SmPL?
Date: Wed, 4 Dec 2019 07:47:43 +0100 (CET)
Message-ID: <1624931283.11188831.1575442063589.JavaMail.zimbra@inria.fr> (raw)
In-Reply-To: <CABvP5W2+fUip+jEAO-G+ZyUPJhx5iCHcTRxkiYsiok_a3zTuRw@mail.gmail.com>

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

> De: "Strace Labs" <stracelabs@gmail.com>
> À: "Julia Lawall" <julia.lawall@inria.fr>
> Cc: "cocci" <cocci@systeme.lip6.fr>
> Envoyé: Mercredi 4 Décembre 2019 01:28:22
> Objet: Re: [Cocci] Changing format string usage with SmPL?

> Unfortunately, it doesn't work. But, I am working on some solutions using
> Python.

> therefore, once we have something like:

> ...
> @r1@
> format list fl;
> identifier fn;
> expression list e;
> position p;
> @@

> fn("%@fl@", e@p)
> ....

> Then, I could handle the format list using make_expr() as well. But, Is it
> possible to rename/handle the expression list?

Could you fix up the expression list first? Then you can write a rule like 

char[] s; 

fn(s, ..., 
- oldcode 
+ newcode 
,...) 

julia 

> On Tue, Dec 3, 2019 at 3:18 AM Julia Lawall < [ mailto:julia.lawall@inria.fr |
> julia.lawall@inria.fr ] > wrote:

>>> De: "Strace Labs" < [ mailto:stracelabs@gmail.com | stracelabs@gmail.com ] >
>>> À: "Markus Elfring" < [ mailto:Markus.Elfring@web.de | Markus.Elfring@web.de ] >
>>> Cc: "Julia Lawall" < [ mailto:julia.lawall@inria.fr | julia.lawall@inria.fr ] >,
>>> [ mailto:cocci@systeme.lip6.fr | cocci@systeme.lip6.fr ]
>>> Envoyé: Mardi 3 Décembre 2019 11:30:14
>>> Objet: Re: [Cocci] Changing format string usage with SmPL?

>>> On Sun, Dec 1, 2019 at 6:00 AM Markus Elfring < [ mailto:Markus.Elfring@web.de |
>>> Markus.Elfring@web.de ] > wrote:

>>>>> Basically, I intend to replace alls "%s" called with "mydata->name" by "%m" with
>>>> > "mydata" or "&mydata"

>>>> How far would you get the desired source code transformation based on
>>>> software extensions around a search pattern like the following.
>>>> ..........
>>>> Which algorithm will become sufficient for your data processing needs
>>>> around the usage of functions with variadic arguments because of format strings?

>>> Actually, I really didn't get why you're asking about that. because we are
>>> talking about X and you're asking for Y. but, either way. that is not the
>>> point. the point is because I am studying about the Coccinelle and I am just
>>> trying to figure out if the tool could detect "%s" called with "mydata->name"
>>> and then replace by "%m" and remove the "->name"

>>> e.g: Once if we have:

>>> int foo() {
>>> int id;
>>> struct mydata h1, *h2, s1, *s2;
>>> char *city = "Hello";
>>> my_printf("%s", s2->name);
>>> my_printf("hi hi %s gggg", [ http://h1.name/ | h1.name ] );
>>> my_printf("1234 %d %s @ %d %s | %s -> city=%s", id, [ http://s1.name/ | s1.name
>>> ] , 12, (*h2).name , h2->name , city);
>>> my_printf("aaaa %s hhhhh", h2->name);
>>> my_printf("%s", city);
>>> }

>>> Then, replace by:

>>> int foo() {
>>> int id;
>>> struct mydata h1, *h2, s1, *s2;
>>> char *city = "Hello";
>>> my_printf("%m", s2);
>>> my_printf("hi hi %s gggg", &h1);
>>> my_printf("1234 %d %m @ %d %m | %m -> city=%s", id, [ http://s1.name/ | s1.name
>>> ] , 12, (*h2).name , h2->name , city);
>>> my_printf("aaaa %s hhhhh", h2);
>>> my_printf("%s", city);
>>> }

>>> But, I've read again the other samples and the documentation. therefore, I
>>> didn't figure out how it should be. btw, thank you Julia for the suggestion
>>> performing the Ocalm/make_expr/replace . (Due to something wrong with the
>>> Coccinelle distributed by Brew/Osx. I just rewrote your sample using Python and
>>> the result was the same. But, I can't just replace all "%s" by "%m". As I said,
>>> it should be only if the "%s" was declared to use "mydata->name".

>>> so, I still fighting yet. thanks in Advance.

>> OK, if you may have more than one argument to your print, then you can find the
>> offset using an expression list metavariable:

>> @r@
>> expression list[n] between;
>> @@

>> print(s,between,h2->name,...)

>> Then you can use r.n in your python rule to figure out where is the %s to
>> change. Unfortunately, this will not work well if there are multiple name
>> references in the argument list. Because you will be trying to change the
>> format string in multiple ways, eg once where between has length 2 and once
>> where between has length 4. Substantial hacks would be required to deal with
>> this.

>> It would be nice if you could do

>> @r@
>> expression list[bn] between;
>> expression list[an] after;
>> position p;
>> @@
>> print@p(s,between,name,after)

>> @@
>> format list[ [ http://r.bn/ | r.bn ] ] f1;
>> format list[ [ http://r.an/ | r.an ] ] f2;
>> position r.p;
>> @@
>> print@p(
>> - "%@f1@%s%@f2@"
>> + "%@f1@%m%@f2@"
>> , l)

>> I don't know if that would work, though.

>> julia

>>>> Regards,
>>>> Markus

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

<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>De: </b>"Strace Labs" &lt;stracelabs@gmail.com&gt;<br><b>À: </b>"Julia Lawall" &lt;julia.lawall@inria.fr&gt;<br><b>Cc: </b>"cocci" &lt;cocci@systeme.lip6.fr&gt;<br><b>Envoyé: </b>Mercredi 4 Décembre 2019 01:28:22<br><b>Objet: </b>Re: [Cocci] Changing format string usage with SmPL?<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div dir="ltr">Unfortunately, it doesn't work. But, I am working on some solutions using Python.&nbsp;</div><div dir="ltr"><br></div><div>therefore, once we have something like:</div><br><div>...</div><div>@r1@<br>format list fl;<br>identifier fn;<br>expression list e;<br>position p;<br>@@<br><br>fn("%@fl@", e@p)<br></div><div>....</div><br><div>Then, I could handle the <b>format list</b>&nbsp;using&nbsp;<b>make_expr()</b>&nbsp;as well. But, Is it possible to rename/handle the <b>expression list?</b></div></div></blockquote><div><br></div><div>Could you fix up the expression list first?&nbsp; Then you can write a rule like<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>char[] s;<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>fn(s, ...,<br data-mce-bogus="1"></div><div>- oldcode<br data-mce-bogus="1"></div><div>+ newcode<br data-mce-bogus="1"></div><div>&nbsp;&nbsp; ,...)<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>julia<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 3, 2019 at 3:18 AM Julia Lawall &lt;<a href="mailto:julia.lawall@inria.fr" target="_blank">julia.lawall@inria.fr</a>&gt; wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)"><hr id="gmail-m_5206298212494274740zwchr"><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>De: </b>"Strace Labs" &lt;<a href="mailto:stracelabs@gmail.com" target="_blank">stracelabs@gmail.com</a>&gt;<br><b>À: </b>"Markus Elfring" &lt;<a href="mailto:Markus.Elfring@web.de" target="_blank">Markus.Elfring@web.de</a>&gt;<br><b>Cc: </b>"Julia Lawall" &lt;<a href="mailto:julia.lawall@inria.fr" target="_blank">julia.lawall@inria.fr</a>&gt;, <a href="mailto:cocci@systeme.lip6.fr" target="_blank">cocci@systeme.lip6.fr</a><br><b>Envoyé: </b>Mardi 3 Décembre 2019 11:30:14<br><b>Objet: </b>Re: [Cocci] Changing format string usage with SmPL?<br></blockquote></div><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir="ltr"><div dir="ltr">On Sun, Dec 1, 2019 at 6:00 AM Markus Elfring &lt;<a href="mailto:Markus.Elfring@web.de" target="_blank">Markus.Elfring@web.de</a>&gt; wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">&gt; Basically, I intend to replace alls "%s" called with "mydata-&gt;name" by "%m" with "mydata" or "&amp;mydata"<br><br>
How far would you get the desired source code transformation based on<br>
software extensions around a search pattern like the following.<br>..........<br>
Which algorithm will become sufficient for your data processing needs<br>
around the usage of functions with variadic arguments because of format strings?<br><br></blockquote><br><div>Actually, I really didn't get why you're asking about that. because we are talking about X and you're asking for Y. but, either way. that is not the point. the point is because I am studying about the Coccinelle and I am just trying to figure out if the tool could detect "%s" called with "mydata-&gt;name" and then replace by "%m" and remove the "-&gt;name"</div><br><div>e.g: Once if we have:</div><br><div>int foo() {<br>&nbsp; int id;<br>&nbsp; struct mydata h1, *h2, s1, *s2;<br>&nbsp; char *city = "Hello";<br>&nbsp; my_printf("%s", s2-&gt;name);<br>&nbsp; my_printf("hi hi %s gggg", <a href="http://h1.name" target="_blank">h1.name</a>);<br>&nbsp; my_printf("1234 %d <b>%s</b> @ %d <b>%s</b>&nbsp;| <b>%s</b> -&gt; city=%s", id, <b><a href="http://s1.name" target="_blank">s1.name</a></b>, 12, <b>(*h2).name</b>, <b>h2-&gt;name</b>, city);<br>&nbsp; my_printf("aaaa %s hhhhh", h2-&gt;name);<br>&nbsp; my_printf("%s", city);<br></div><div>}</div><br><div>Then, replace by:</div><br><div><div>int foo() {<br>&nbsp; int id;<br>&nbsp; struct mydata h1, *h2, s1, *s2;<br>&nbsp; char *city = "Hello";<br><b>my_printf("%m", s2);</b><br><b>my_printf("hi hi %s gggg", &amp;h1);</b><br>&nbsp; my_printf("1234 %d <b>%m</b>&nbsp;@ %d <b>%m</b>&nbsp;| <b>%m</b>&nbsp;-&gt; city=%s", id, <b><a href="http://s1.name" target="_blank">s1.name</a></b>, 12, <b>(*h2).name</b>, <b>h2-&gt;name</b>, city);<br><b>&nbsp; my_printf("aaaa %s hhhhh", h2);</b><br>&nbsp; my_printf("%s", city);<br></div><div>}</div></div><br><div>But, I've read again the other samples and the documentation. therefore, I didn't figure out how it should be. btw, thank you Julia for the suggestion performing the <i>Ocalm/make_expr/replace</i>. (Due to something wrong with the Coccinelle distributed by Brew/Osx. I just rewrote your sample using Python and the result was the same. But, I can't just replace all "%s" by "%m". As I said, it should be only if the "%s" was declared to use "mydata-&gt;name".</div><br><div>so, I still fighting yet. thanks in Advance.</div></div></div></blockquote><div>OK, if you may have more than one argument to your print, then you can find the offset using an expression list metavariable:<br></div><br><div>@r@<br></div><div>expression list[n] between;<br></div><div>@@<br></div><br><div>print(s,between,h2-&gt;name,...)<br></div><br><div>Then you can use r.n in your python rule to figure out where is the %s to change.&nbsp; Unfortunately, this will not work well if there are multiple name references in the argument list.&nbsp; Because you will be trying to change the format string in multiple ways, eg once where between has length 2 and once where between has length 4.&nbsp; Substantial hacks would be required to deal with this.<br></div><br><div>It would be nice if you could do<br></div><br><div>@r@<br></div><div>expression list[bn] between;<br></div><div>expression list[an] after;<br></div><div>position p;<br></div><div>@@<br></div><div>print@p(s,between,name,after)<br></div><br><div>@@<br></div><div>format list[<a href="http://r.bn" target="_blank">r.bn</a>] f1;<br></div><div>format list[<a href="http://r.an" target="_blank">r.an</a>] f2;<br></div><div>position r.p;<br></div><div>@@<br></div><div>print@p(</div><div>-&nbsp;&nbsp;&nbsp; "%@f1@%s%@f2@"<br></div><div>+&nbsp;&nbsp; "%@f1@%m%@f2@"<br></div><div>, l)<br></div><br><div>I don't know if that would work, though.<br></div><br><div>julia<br></div><br><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Regards,<br>
Markus<br></blockquote></div></div></blockquote></div></div></div></blockquote></div></div><br></blockquote></div></div></body></html>

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

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

  parent 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
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 [this message]
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=1624931283.11188831.1575442063589.JavaMail.zimbra@inria.fr \
    --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

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