Skip to content

Commit

Permalink
cppcheck: warn on use of flock
Browse files Browse the repository at this point in the history
  • Loading branch information
zanchey committed Dec 3, 2016
1 parent 2b0bad8 commit bf53f39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .cppcheck.rules
@@ -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
Expand All @@ -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>
3 changes: 3 additions & 0 deletions src/env_universal_common.cpp
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/fallback.h
Expand Up @@ -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. */
Expand Down

0 comments on commit bf53f39

Please sign in to comment.