Skip to contentSkip to conversation

Matching literal strings

Cocci Rasmus Villemoes 2 participants 5 messages Sep 11, 2014

lore
Share & tools
lore
#0 lore raw

Hi,

While at it, I also wanted to change seq_printf(m, str)
into seq_puts(m, str) [1], but the pattern seq_printf(m, "%s", str) also appears. So I tried

@p1 depends on patch@
expression s;
expression t;
@@
(
- seq_printf(s, t)
+ seq_puts(s, t)
|
- seq_printf(s, "%s", t)
+ seq_puts(s, t)
)

However, this seems to match any format string consisting of a single format specifier; for example in kernel/locking/lockdep_proc.c I get these

- seq_printf(m, "%p", class->key);
+ seq_puts(m, class->key);

- seq_printf(m, "%c", c);
+ seq_puts(m, c);

- seq_printf(m, "%14lu", lt->nr);
+ seq_puts(m, lt->nr);

which is obviously wrong. What should I do to make coccinelle match a specific string literal? I guess I could introduce 'constant c =~ "^\"%s\"$";', but there might be a more canonical way.

Thanks,
Rasmus

[1] Besides avoiding the long seq_vprintf -> snprintf -> ... chain and pushing all the non-existent varargs into a va_arg structure, this is also safer if str happens to contain format characters; although usually str is a literal.

#1 lore raw
Quoted 31 lines · Rasmus Villemoes

Hi,

While at it, I also wanted to change seq_printf(m, str)
into seq_puts(m, str) [1], but the pattern seq_printf(m, "%s", str) also appears. So I tried

@p1 depends on patch@
expression s;
expression t;
@@
(
- seq_printf(s, t)
+ seq_puts(s, t)
|
- seq_printf(s, "%s", t)
+ seq_puts(s, t)
)

However, this seems to match any format string consisting of a single format specifier; for example in kernel/locking/lockdep_proc.c I get these

(quoted diff) +3 -3
-       seq_printf(m, "%p", class->key);
+       seq_puts(m, class->key);
 
-               seq_printf(m, "%c", c);
+               seq_puts(m, c);
 
-       seq_printf(m, "%14lu", lt->nr);
+       seq_puts(m, lt->nr);
Quoted 3 lines

which is obviously wrong. What should I do to make coccinelle match a specific string literal? I guess I could introduce 'constant c =~ "^\"%s\"$";', but there might be a more canonical way.

It looks like a bug. There have been some improvements in the handling of format strings, but perhaps the changes have not all been positive...

I will end the iterative version shortly. You will need a version of coccinelle that supports ocaml scripting, which seems to mean that you need to have ocamlfind installed on your machine at the time when you make coccinelle.

julia

#2 lore raw
Quoted 9 lines · Julia Lawall
Quoted 4 lines · Rasmus Villemoes

which is obviously wrong. What should I do to make coccinelle match a specific string literal? I guess I could introduce 'constant c =~ "^\"%s\"$";', but there might be a more canonical way.

It looks like a bug. There have been some improvements in the handling of format strings, but perhaps the changes have not all been positive...

In that case you might like to know

$ spatch --version
spatch version 1.0.0-rc21 with Python support and with PCRE support

Note that it shouldn't really matter that "%s" is a format string (which is why I didn't attempt to use a "format" variable; I don't really understand those); I just want to find instances where seq_printf is called with a specific string literal as its second argument.

Quoted 4 lines

I will end the iterative version shortly. You will need a version of coccinelle that supports ocaml scripting, which seems to mean that you need to have ocamlfind installed on your machine at the time when you make coccinelle.

OK, thanks for working on it.

Rasmus

#3 lore raw
Quoted 14 lines · Rasmus Villemoes
Quoted 9 lines · Julia Lawall
Quoted 4 lines · Rasmus Villemoes

which is obviously wrong. What should I do to make coccinelle match a specific string literal? I guess I could introduce 'constant c =~ "^\"%s\"$";', but there might be a more canonical way.

It looks like a bug. There have been some improvements in the handling of format strings, but perhaps the changes have not all been positive...

In that case you might like to know

Quoted 7 lines
$ spatch --version
spatch version 1.0.0-rc21 with Python support and with PCRE support

Note that it shouldn't really matter that "%s" is a format string (which is why I didn't attempt to use a "format" variable; I don't really understand those); I just want to find instances where seq_printf is called with a specific string literal as its second argument.

The problem seems to be fixed. On the other hand, I'm not sure what patch to send you. Perhaps you could just use the regexp solution in the short term.

julia

#4 lore raw
In reply to Rasmus Villemoes

Here is a semantic patch that iterates for the seq_puts case. The rule "a" may look a little strange. It is just there so that Coccinelle knows that nothing will happen with this semantic patch if there is no double seq_puts in the code. This makes a massive performance improvement, because it doesn't have to parse every file. In the non-iterative version, it was able to figure this out as well, but the need to have the flag added to determine whether a new iteration has already been asked for for the current file makes that mroe obscure.

You should run this with the following options:

--no-includes --include-headers --in-place

julia

--------------------------------------------------------------------------

@initialize:ocaml@
@@

let added = ref false

let add_if_not_present file =
if not !added
then
begin
let it = new iteration() in
it#set_files [file];
it#register();
added := true
end

@a@
@@

seq_puts(...);
seq_puts(...);

@script:ocaml depends on a@
@@

added := false

@r@
position p;
@@

seq_puts(...);
seq_puts(...);
seq_puts at p(...);

@concat1@
expression s;
constant c1, c2;
position p1 != r.p, p2 != r.p;
@@
seq_puts at p1(s, c1);
seq_puts at p2(s, c2);

@script:ocaml@
p1 << concat1.p1;
@@

add_if_not_present (List.hd p1).file

@script:python concat2@
c1 << concat1.c1;
c2 << concat1.c2;
c3;
@@

// The indentation probably needs to be fixed manually coccinelle.c3 = c1 + "\n\t" + c2

@concat3@
identifier concat2.c3;
expression concat1.s;
constant concat1.c1, concat1.c2;
position concat1.p1;
@@
- seq_puts@p1(s, c1);
- seq_puts(s, c2);
+ seq_puts(s, c3);