summaryrefslogtreecommitdiff
path: root/coccinelle/take-fd.cocci
Commit message (Collapse)AuthorAgeFilesLines
* coccinelle: avoid matching 'errno' as a file descriptorFrantisek Sumsal2019-04-271-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* macro: introduce new TAKE_FD() macroLennart Poettering2018-03-221-0/+14
This is similar to TAKE_PTR() but operates on file descriptors, and thus assigns -1 to the fd parameter after returning it. Removes 60 lines from our codebase. Pretty good too I think.