summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrantisek Sumsal <frantisek@sumsal.cz>2019-04-27 12:26:22 +0200
committerFrantisek Sumsal <frantisek@sumsal.cz>2019-04-27 15:46:48 +0200
commit17e3e37c059aca1db9946675dd3dd9ae1454b0de (patch)
treec810bb16bb4e00f9b63dc87557cca97d48aa77e9
parentb3fd7b53ffcdb3d4ed4adaa42c73762a26cc6df8 (diff)
downloadsystemd-17e3e37c059aca1db9946675dd3dd9ae1454b0de.tar.gz
coccinelle: avoid matching 'errno' as a file descriptor
The `coccinelle/take-fd.cocci` transformation file attempts to rewrite r = fd; fd = -1; to r = TAKE_FD(fd); Unfortunately, using `identifier` or `idexpression` as a metavariable type in this case wouldn't match more complex location descriptions, like: x->fd = fd fd = -1; Using 'expression' metavariable type generates false positives, as you can't specify scope of such expression. The only real example from the current codebase is the global 'errno' variable, which results in following patch generated by `spatch`: --- src/basic/errno-util.h +++ /tmp/cocci-output-28263-971baa-errno-util.h @@ -15,8 +15,7 @@ static inline void _reset_errno_(int *sa #define UNPROTECT_ERRNO \ do { \ - errno = _saved_errno_; \ - _saved_errno_ = -1; \ + errno = TAKE_FD(_saved_errno_); \ } while (false) static inline int negative_errno(void) { Let's explicitly state that the matched expression should not equal 'errno' to avoid this. It's not particularly nice, but it should be enough, at least for now.
-rw-r--r--coccinelle/take-fd.cocci9
1 files changed, 8 insertions, 1 deletions
diff --git a/coccinelle/take-fd.cocci b/coccinelle/take-fd.cocci
index ba242483cd..f7124e7896 100644
--- a/coccinelle/take-fd.cocci
+++ b/coccinelle/take-fd.cocci
@@ -6,8 +6,15 @@ expression q;
- q = -1;
- return p;
+ return TAKE_FD(q);
+
+/* The ideal solution would use 'local idexpression' to avoid matching errno,
+ * which is a global variable. However, 'idexpression' nor 'identifier'
+ * would match, for example, "x->fd", which is considered 'expression' in
+ * the SmPL grammar
+ */
@@
-expression p, q;
+expression p != errno;
+expression q;
@@
- p = q;
- q = -1;