]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coccinelle: ignore function transformations causing recursion
authorFrantisek Sumsal <frantisek@sumsal.cz>
Sun, 28 Apr 2019 15:13:29 +0000 (17:13 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 29 Apr 2019 13:38:53 +0000 (15:38 +0200)
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

coccinelle/empty-to-null.cocci
coccinelle/mfree_return.cocci
coccinelle/strempty.cocci

index fbc75b9c343ca49688435e34e9cef32c35d81ff5..bc6c656e798a22cfde36457facb9c85042740f64 100644 (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;
 @@
-- isempty(s) ? NULL : s
+
+- isempty@p(s) ? NULL : s
 + empty_to_null(s)
index 8119fe07f29f738f9749f8f46e63212fca054681..15e6c7d566bd08281e841892b3383500871da64f 100644 (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 mfree(p);
++ return mfree(e);
index 13ceb338f199dad900efa29f5f94d97db762a5e3..7901da3652ce7bb4c9d023e2ffaf7cb84b15ac5d 100644 (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;
 @@
-- s ?: ""
+(
+- s@p ?: ""
 + strempty(s)
-@@
-expression s;
-@@
-- s ? s : ""
+|
+- s@p ? s : ""
 + strempty(s)
+)
+
 @@
+position p : script:python() { p[0].current_element != "strempty" };
 expression s;
 @@
-- if (!s)
+- if (!s@p)
 -         s = "";
 + s = strempty(s);
+
 @@
+position p : script:python() { p[0].current_element != "strnull" };
 expression s;
 @@
-- s ?: "(null)"
+(
+- s@p ?: "(null)"
 + strnull(s)
-@@
-expression s;
-@@
-- s ? s : "(null)"
+|
+- s@p ? s : "(null)"
 + strnull(s)
+)
+
 @@
+position p : script:python() { p[0].current_element != "strnull" };
 expression s;
 @@
-- if (!s)
+- if (!s@p)
 -         s = "(null)";
 + s = strnull(s);
+
 @@
+position p : script:python() { p[0].current_element != "strna" };
 expression s;
 @@
-- s ?: "n/a"
+(
+- s@p ?: "n/a"
 + strna(s)
-@@
-expression s;
-@@
-- s ? s : "n/a"
+|
+- s@p ? s : "n/a"
 + strna(s)
+)
+
 @@
+position p : script:python() { p[0].current_element != "strna" };
 expression s;
 @@
-- if (!s)
+- if (!s@p)
 -         s = "n/a";
 + s = strna(s);