From bf53f39cddd25709f5a76a57d66a0bae8c409d93 Mon Sep 17 00:00:00 2001 From: David Adam Date: Wed, 30 Nov 2016 18:14:54 +0800 Subject: [PATCH] cppcheck: warn on use of flock --- .cppcheck.rules | 11 +++++++++++ src/env_universal_common.cpp | 3 +++ src/fallback.h | 3 +++ 3 files changed, 17 insertions(+) diff --git a/.cppcheck.rules b/.cppcheck.rules index d6223a14b..8c0d28c62 100644 --- a/.cppcheck.rules +++ b/.cppcheck.rules @@ -1,5 +1,6 @@ + <--!> +]]> + + + flock \( + + flockSemanticsWarning + warning + flock has a fallback implemented in terms of fcntl; ensure that the fcntl semantics will apply (see http://0pointer.de/blog/projects/locking.html) + + diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 955a9196e..c182e7f4a 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -595,6 +595,9 @@ bool env_universal_t::open_and_acquire_lock(const wcstring &path, int *out_fd) { // Try taking the lock, if necessary. If we failed, we may be on lockless NFS, etc.; in that // case we pretend we succeeded. See the comment in save_to_path for the rationale. if (needs_lock) { + // This is safe in terms of the fallback function implemented in terms of fcntl: only ever + // run on the main thread, and protected by the universal variable lock + // cppcheck-suppress flockSemanticsWarning while (flock(fd, LOCK_EX) < 0) { /* error */ if (errno != EINTR) { diff --git a/src/fallback.h b/src/fallback.h index 28e5b3bde..6e60a34fa 100644 --- a/src/fallback.h +++ b/src/fallback.h @@ -129,6 +129,9 @@ int killpg(int pgr, int sig); /// Fallback implementation of flock in terms of fcntl /// Danger! The semantics of flock and fcntl locking are very different. /// Use with caution. +// Ignore the cppcheck warning as this is the implementation that it is +// warning about! +// cppcheck-suppress flockSemanticsWarning int flock(int fd, int op); #define LOCK_SH 1 /* Shared lock. */