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