Permalink
Browse files

cppcheck: warn on use of flock

  • Loading branch information...
1 parent 2b0bad8 commit bf53f39cddd25709f5a76a57d66a0bae8c409d93 @zanchey zanchey committed Nov 30, 2016
Showing with 17 additions and 0 deletions.
  1. +11 −0 .cppcheck.rules
  2. +3 −0 src/env_universal_common.cpp
  3. +3 −0 src/fallback.h
View
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
+<![CDATA[
<!-- Sadly we can't enable the following two rules since doing so causes false
positives in standard header files rather than just project specific
source files. If we can find a way to enable these rules by also
@@ -22,3 +23,13 @@
</message>
</rule>
<--!>
+]]>
+
+<rule>
+ <pattern>flock \(</pattern>
+ <message>
+ <id>flockSemanticsWarning</id>
+ <severity>warning</severity>
+ <summary>flock has a fallback implemented in terms of fcntl; ensure that the fcntl semantics will apply (see http://0pointer.de/blog/projects/locking.html)</summary>
+ </message>
+</rule>
@@ -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) {
View
@@ -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. */

0 comments on commit bf53f39

Please sign in to comment.