Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Replacing printf/format calls based on the data-type
@ 2019-11-28  2:11 Strace Labs
  2019-11-28  7:07 ` Julia Lawall
  2019-11-28  7:50 ` Markus Elfring
  0 siblings, 2 replies; 31+ messages in thread
From: Strace Labs @ 2019-11-28  2:11 UTC (permalink / raw)
  To: cocci

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

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..." ?

Thanks in advance,

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

<div dir="ltr">Hi,<div><br></div><div>I am working on a semantic patch for replacing specific format string token based on the used data-type.</div><div>so, I have several calls of my_printf() and some special macros pointing to them around my code.</div><div><br></div><div>e.g: part of my code.</div><div>.....</div><div>struct mydata *m;<br></div><div>struct mydata h;<br></div><div>.....</div><div>my_printf(&quot;%s&quot;, m-&gt;name);</div><div>.....<br>my_printf(&quot;%s&quot;, <a href="http://h.name" target="_blank">h.name</a>);</div><div>.....<br>	my_printf(&quot;whatever id %d following the string %s\n&quot;, id, m-&gt;name);<br></div><div>....</div><div><div>Macro_to_my_printf(&quot;Hey id %d, let&#39;s see %s\n&quot;, id, <a href="http://h.name" target="_blank">h.name</a>);<br></div><div>.....</div><div>Macro2_to_my_printf(fd, &quot;Hey id %d, let&#39;s see %s\n&quot;, id, m-&gt;name);<br></div><div></div></div><div>.....<br></div><div><br></div><div>My current humble *.cocci</div><div><br></div><div>$ cat fix-my_printf.cocci<br>@r1_heap@<br>struct mydata *SMD;<br>format F =~ &quot;s&quot;;<br>@@<br>-my_printf(&quot;%@F@&quot;, SMD-&gt;name);<br>+my_printf(&quot;%m&quot;, SMD);<br><br>@r1_stack@<br>struct mydata SMD;<br>format F =~ &quot;s&quot;;<br>@@<br>-my_printf(&quot;%@F@&quot;, SMD.name);<br>+my_printf(&quot;%m&quot;, &amp;SMD);<br>$</div><div><br></div><div>But, I can match only with partial content as can be seen below.</div><div><br></div><div>$ spatch --partial-match --sp-file fix-my_printf.cocci sample-format-string.c | egrep &quot;^(\+|-)&quot;<br>HANDLING: sample-format-string.c<br>diff =<br>HANDLING: /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c<br>diff =<br>--- /Volumes/Users/jpereira/Devel/Sandbox/sample-format-string.c<br>+++ /var/folders/ld/6tg9c6qj4fx4c85q26mcqrsh0000gn/T/cocci-output-24659-130f86-sample-format-string.c<br>- 	my_printf(&quot;%s&quot;, m-&gt;name);<br>+ 	my_printf(&quot;%m&quot;, m);<br>- 	my_printf(&quot;%s&quot;, <a href="http://h.name" target="_blank">h.name</a>);<br>+ 	my_printf(&quot;%m&quot;, &amp;h);<br>$<br></div><div><br></div><div>Anyone could give me a light about how to proceed to match the entire &quot;.....string format...&quot; ?</div><div><br></div><div>Thanks in advance,</div></div>

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

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

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, back to index

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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