All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] Matching the size of an array
@ 2023-06-06  6:14 Yann Droneaud
  2023-06-06 16:23 ` Julia Lawall
  2023-06-06 17:15 ` Markus Elfring
  0 siblings, 2 replies; 3+ messages in thread
From: Yann Droneaud @ 2023-06-06  6:14 UTC (permalink / raw)
  To: cocci

[-- Attachment #1: Type: text/plain, Size: 713 bytes --]

Hi,

I've managed to use coccinelle to replace something like:

char array[123]

snprintf(array, 123, ...);

with:

#define SNPRINTF(array, fmt, ...) snprintf(array, sizeof(array), fmt, ## 
__VA_ARGS__)

char array[123];

SNPRINTF(array, ...)

The difficult part for me was to figure how to match array and size within
structures, part of typedef or not, anonymous or not, accessed through 
pointer
or not.

And I'm not proud of SmPL I wrote. See the attached monster.

I'm sure I've missed some ways to use simpler constructs.

The example can be found here, with the test case:

https://gitlab.com/ydroneaud/snprintf.cocci/

I hope someone could give some hints to simplify my mess.

Regards

-- 
Yann Droneaud

[-- Attachment #2: SNPRINTF.cocci --]
[-- Type: text/plain, Size: 2670 bytes --]

@@
expression DST;
@@
- snprintf(DST, sizeof(DST),
+ SNPRINTF(DST,
  ...);

// match an array declared as a variable
@as_variable@
identifier DST;
expression DSTSIZE;
@@
(
  char DST[DSTSIZE];
|
  char DST[DSTSIZE] = ...;
)

@@
identifier as_variable.DST;
expression as_variable.DSTSIZE;
@@
- snprintf(DST, DSTSIZE,
+ SNPRINTF(DST,
  ...);

// match a structure
@in_struct@
identifier STRUCT;
identifier DST;
expression DSTSIZE;
@@
  struct STRUCT {
      ...
      char DST[DSTSIZE];
      ...
  }

@@
identifier in_struct.STRUCT;
struct STRUCT VAR;
identifier in_struct.DST;
expression in_struct.DSTSIZE;
@@
- snprintf(VAR.DST, DSTSIZE,
+ SNPRINTF(VAR.DST,
  ...);

@@
identifier in_struct.STRUCT;
struct STRUCT *PTR;
identifier in_struct.DST;
expression in_struct.DSTSIZE;
@@
- snprintf(PTR->DST, DSTSIZE,
+ SNPRINTF(PTR->DST,
  ...);

// match a structure used to declare a new type
@as_typedef@
identifier in_struct.STRUCT;
type TYPE;
@@
  typedef struct STRUCT TYPE;

@@
type as_typedef.TYPE;
TYPE VAR;
identifier in_struct.DST;
expression in_struct.DSTSIZE;
@@
- snprintf(VAR.DST, DSTSIZE,
+ SNPRINTF(VAR.DST,
  ...);

@@
type as_typedef.TYPE;
TYPE *PTR;
identifier in_struct.DST;
expression in_struct.DSTSIZE;
@@
- snprintf(PTR->DST, DSTSIZE,
+ SNPRINTF(PTR->DST,
  ...);

// match an anonymous structure used to declare a new type
@in_anon_typedef@
identifier DST;
expression DSTSIZE;
type TYPE;
@@
  typedef struct
  {
  ...
  char DST[DSTSIZE];
  ...
  } TYPE;

@@
type in_anon_typedef.TYPE;
TYPE VAR;
identifier in_anon_typedef.DST;
expression in_anon_typedef.DSTSIZE;
@@
- snprintf(VAR.DST, DSTSIZE,
+ SNPRINTF(VAR.DST,
  ...);

@@
type in_anon_typedef.TYPE;
TYPE *PTR;
identifier in_anon_typedef.DST;
expression in_anon_typedef.DSTSIZE;
@@
- snprintf(PTR->DST, DSTSIZE,
+ SNPRINTF(PTR->DST,
  ...);

// match a named structure used to declare a new type
@in_typedef@
identifier STRUCT;
identifier DST;
expression DSTSIZE;
type TYPE;
@@
  typedef struct STRUCT
  {
  ...
  char DST[DSTSIZE];
  ...
  } TYPE;

@@
type in_typedef.TYPE;
TYPE VAR;
identifier in_typedef.DST;
expression in_typedef.DSTSIZE;
@@
- snprintf(VAR.DST, DSTSIZE,
+ SNPRINTF(VAR.DST,
  ...);

@@
type in_typedef.TYPE;
TYPE *PTR;
identifier in_typedef.DST;
expression in_typedef.DSTSIZE;
@@
- snprintf(PTR->DST, DSTSIZE,
+ SNPRINTF(PTR->DST,
  ...);



// match an anonymous structure used to declare a new type
@in_anon_var@
identifier DST;
expression DSTSIZE;
identifier VAR;
@@
  struct
  {
  ...
  char DST[DSTSIZE];
  ...
  } VAR;

@@
identifier in_anon_var.VAR;
identifier in_anon_var.DST;
expression in_anon_var.DSTSIZE;
@@
- snprintf(VAR.DST, DSTSIZE,
+ SNPRINTF(VAR.DST,
  ...);

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

* Re: [cocci] Matching the size of an array
  2023-06-06  6:14 [cocci] Matching the size of an array Yann Droneaud
@ 2023-06-06 16:23 ` Julia Lawall
  2023-06-06 17:15 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Julia Lawall @ 2023-06-06 16:23 UTC (permalink / raw)
  To: Yann Droneaud; +Cc: cocci



On Tue, 6 Jun 2023, Yann Droneaud wrote:

> Hi,
>
> I've managed to use coccinelle to replace something like:
>
> char array[123]
>
> snprintf(array, 123, ...);
>
> with:
>
> #define SNPRINTF(array, fmt, ...) snprintf(array, sizeof(array), fmt, ##
> __VA_ARGS__)
>
> char array[123];
>
> SNPRINTF(array, ...)
>
> The difficult part for me was to figure how to match array and size within
> structures, part of typedef or not, anonymous or not, accessed through pointer
> or not.
>
> And I'm not proud of SmPL I wrote. See the attached monster.
>
> I'm sure I've missed some ways to use simpler constructs.
>
> The example can be found here, with the test case:
>
> https://gitlab.com/ydroneaud/snprintf.cocci/
>
> I hope someone could give some hints to simplify my mess.

I thought it looked basically ok.

Not putting a semicolon on line 37 (in_struct rule) was a good choice.

julia

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

* Re: [cocci] Matching the size of an array
  2023-06-06  6:14 [cocci] Matching the size of an array Yann Droneaud
  2023-06-06 16:23 ` Julia Lawall
@ 2023-06-06 17:15 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2023-06-06 17:15 UTC (permalink / raw)
  To: Yann Droneaud; +Cc: cocci

> I'm sure I've missed some ways to use simpler constructs.

I suggest to take a few additional SmPL code variants into account
(by increasing a possible change granularity).

How do you think about to use a SmPL rule like the following?

@replacement1@
expression DST;
@@
-snprintf
+SNPRINTF
         (DST,
-         sizeof(DST),
          ...);


> The example can be found here, with the test case:

https://gitlab.com/ydroneaud/snprintf.cocci/-/blob/cf9d49d31dda5d3e6af94a38c998d31356b2a177/SNPRINTF.cocci#L8

Does your SmPL rule “as_variable” indicate a need for additional isomorphisms
according to array variables?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/6608e45f85a10c57a3c910154cf049a5df4d98e4/standard.iso#L465


Would you like to increase the usage of SmPL variable lists
instead of repeating a key word like “identifier” at a few places?

Regards,
Markus

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

end of thread, other threads:[~2023-06-06 17:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-06  6:14 [cocci] Matching the size of an array Yann Droneaud
2023-06-06 16:23 ` Julia Lawall
2023-06-06 17:15 ` Markus Elfring

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.