Hello,

I have about 800 occurences of constructs like this one :
    if (ptr) {
        MEMO_FREE(ptr);
        ptr = NULL;
    }

I would like to simplify it to a new API :
    ng_free(&ptr);

It works well with the following rule :
    // <smpl>
    @@
    identifier I;
    @@
    - if (I) {
    -    MEMO_FREE(I);
    -    I = NULL;
    - }
    + ng_free(&I);
    // </smpl>


However, the code also has constructs :
* without the enclosing test on "ptr".
* "if (ptr != NULL)" instead of "if (ptr)"
* without the "ptr = NULL" assignment
* with the assignment outside of the test

I'm having a hard time writing the semantic patch corresponding to
this rework without explicitely specifying all combinations.

Below is the example I'm playing with as well as the rule file I
working on... Any help appreciated ! :o)

Flavien.
---
#include <stdlib.h>

#define MEMO_FREE(ptr) free(ptr)
void new_free(void **ptr)
{ 
    free(*ptr);
    *ptr = NULL;
} 

void f1(void)
{
    char *ptr = malloc(12);

    if (ptr) {
        MEMO_FREE(ptr);
    }
} 

void f2(void)
{
    char *pt = malloc(12);;
    if (ptr) {
        MEMO_FREE(ptr);
        ptr = NULL;
    } 
} 

void f3(void)
{
    char *ptr = malloc(12);
    if (ptr != NULL) {
        MEMO_FREE(ptr);
    }
    ptr = NULL;
}
---
// <smpl>
@@
identifier I;
@@
- if (I) {
-    MEMO_FREE(I);
-    I = NULL;
- }
+ new_free(&I);


@@  
identifier I;
@@  
- if (I) {
-    MEMO_FREE(I);
- }
+ new_free(&I);


@@  
identifier I;
@@      
- MEMO_FREE(I);
- I = NULL;
+ new_free(&I);


@@
identifier I; 
@@  
- MEMO_FREE(I);
+ new_free(&I);
// </smpl>
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to