coccinelle: ignore function transformations causing recursion

For example, following transformation:

- isempty(s) ? NULL : s
+ empty_to_null(s)

would get applied to the empty_to_null function itself as well,
causing an infinite recursion, like:

--- src/basic/string-util.h
+++ /tmp/cocci-output-307-9f76e6-string-util.h
@@ -50,11 +50,11 @@ static inline bool isempty(const char *p
 }

 static inline const char *empty_to_null(const char *p) {
-        return isempty(p) ? NULL : p;
+        return empty_to_null(p);
 }

Let's avoid that by checking the current match position
This commit is contained in:
Frantisek Sumsal 2019-04-28 17:13:29 +02:00
parent 33af88cf70
commit 60d9959dd8
3 changed files with 39 additions and 22 deletions

View File

@ -1,5 +1,8 @@
@@ @@
/* Avoid running this transformation on the empty_to_null function itself */
position p : script:python() { p[0].current_element != "empty_to_null" };
expression s; expression s;
@@ @@
- isempty(s) ? NULL : s
- isempty@p(s) ? NULL : s
+ empty_to_null(s) + empty_to_null(s)

View File

@ -1,6 +1,8 @@
@@ @@
expression p; /* Avoid running this transformation on the mfree function itself */
position p : script:python() { p[0].current_element != "mfree" };
expression e;
@@ @@
- free(p); - free@p(e);
- return NULL; - return NULL;
+ return mfree(p); + return mfree(e);

View File

@ -1,48 +1,60 @@
@@ @@
/* Avoid running this transformation on the strempty function itself */
position p : script:python() { p[0].current_element != "strempty" };
expression s; expression s;
@@ @@
- s ?: "" (
- s@p ?: ""
+ strempty(s) + strempty(s)
@@ |
expression s; - s@p ? s : ""
@@
- s ? s : ""
+ strempty(s) + strempty(s)
)
@@ @@
position p : script:python() { p[0].current_element != "strempty" };
expression s; expression s;
@@ @@
- if (!s) - if (!s@p)
- s = ""; - s = "";
+ s = strempty(s); + s = strempty(s);
@@ @@
position p : script:python() { p[0].current_element != "strnull" };
expression s; expression s;
@@ @@
- s ?: "(null)" (
- s@p ?: "(null)"
+ strnull(s) + strnull(s)
@@ |
expression s; - s@p ? s : "(null)"
@@
- s ? s : "(null)"
+ strnull(s) + strnull(s)
)
@@ @@
position p : script:python() { p[0].current_element != "strnull" };
expression s; expression s;
@@ @@
- if (!s) - if (!s@p)
- s = "(null)"; - s = "(null)";
+ s = strnull(s); + s = strnull(s);
@@ @@
position p : script:python() { p[0].current_element != "strna" };
expression s; expression s;
@@ @@
- s ?: "n/a" (
- s@p ?: "n/a"
+ strna(s) + strna(s)
@@ |
expression s; - s@p ? s : "n/a"
@@
- s ? s : "n/a"
+ strna(s) + strna(s)
)
@@ @@
position p : script:python() { p[0].current_element != "strna" };
expression s; expression s;
@@ @@
- if (!s) - if (!s@p)
- s = "n/a"; - s = "n/a";
+ s = strna(s); + s = strna(s);