cppcheck: warn on use of flock

This commit is contained in:
David Adam 2016-11-30 18:14:54 +08:00
parent 2b0bad889a
commit bf53f39cdd
3 changed files with 17 additions and 0 deletions

View file

@ -1,5 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<![CDATA[
<!-- Sadly we can't enable the following two rules since doing so causes false <!-- Sadly we can't enable the following two rules since doing so causes false
positives in standard header files rather than just project specific positives in standard header files rather than just project specific
source files. If we can find a way to enable these rules by also source files. If we can find a way to enable these rules by also
@ -22,3 +23,13 @@
</message> </message>
</rule> </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>

View file

@ -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 // 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. // case we pretend we succeeded. See the comment in save_to_path for the rationale.
if (needs_lock) { 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) { while (flock(fd, LOCK_EX) < 0) {
/* error */ /* error */
if (errno != EINTR) { if (errno != EINTR) {

View file

@ -129,6 +129,9 @@ int killpg(int pgr, int sig);
/// Fallback implementation of flock in terms of fcntl /// Fallback implementation of flock in terms of fcntl
/// Danger! The semantics of flock and fcntl locking are very different. /// Danger! The semantics of flock and fcntl locking are very different.
/// Use with caution. /// 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); int flock(int fd, int op);
#define LOCK_SH 1 /* Shared lock. */ #define LOCK_SH 1 /* Shared lock. */