Bug Summary

File:lib/Support/Mutex.cpp
Warning:line 68, column 15
Null pointer passed as an argument to a 'nonnull' parameter

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Mutex.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/lib/Support -I /build/llvm-toolchain-snapshot-7~svn329677/lib/Support -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/lib/Support -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp
1//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Mutex.h"
15#include "llvm/Support/ErrorHandling.h"
16#include "llvm/Config/config.h"
17
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
23#if !defined(LLVM_ENABLE_THREADS1) || LLVM_ENABLE_THREADS1 == 0
24// Define all methods as no-ops if threading is explicitly disabled
25namespace llvm {
26using namespace sys;
27MutexImpl::MutexImpl( bool recursive) { }
28MutexImpl::~MutexImpl() { }
29bool MutexImpl::acquire() { return true; }
30bool MutexImpl::release() { return true; }
31bool MutexImpl::tryacquire() { return true; }
32}
33#else
34
35#if defined(HAVE_PTHREAD_H1) && defined(HAVE_PTHREAD_MUTEX_LOCK1)
36
37#include <cassert>
38#include <pthread.h>
39#include <stdlib.h>
40
41namespace llvm {
42using namespace sys;
43
44// Construct a Mutex using pthread calls
45MutexImpl::MutexImpl( bool recursive)
46 : data_(nullptr)
47{
48 // Declare the pthread_mutex data structures
49 pthread_mutex_t* mutex =
1
'mutex' initialized here
50 static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
51
52 if (mutex == nullptr)
2
Assuming pointer value is null
3
Taking true branch
53 report_bad_alloc_error("Mutex allocation failed");
54
55 pthread_mutexattr_t attr;
56
57 // Initialize the mutex attributes
58 int errorcode = pthread_mutexattr_init(&attr);
59 assert(errorcode == 0)(static_cast <bool> (errorcode == 0) ? void (0) : __assert_fail
("errorcode == 0", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 59, __extension__ __PRETTY_FUNCTION__))
; (void)errorcode;
60
61 // Initialize the mutex as a recursive mutex, if requested, or normal
62 // otherwise.
63 int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
4
Assuming 'recursive' is 0
5
'?' condition is false
64 errorcode = pthread_mutexattr_settype(&attr, kind);
65 assert(errorcode == 0)(static_cast <bool> (errorcode == 0) ? void (0) : __assert_fail
("errorcode == 0", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 65, __extension__ __PRETTY_FUNCTION__))
;
66
67 // Initialize the mutex
68 errorcode = pthread_mutex_init(mutex, &attr);
6
Null pointer passed as an argument to a 'nonnull' parameter
69 assert(errorcode == 0)(static_cast <bool> (errorcode == 0) ? void (0) : __assert_fail
("errorcode == 0", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 69, __extension__ __PRETTY_FUNCTION__))
;
70
71 // Destroy the attributes
72 errorcode = pthread_mutexattr_destroy(&attr);
73 assert(errorcode == 0)(static_cast <bool> (errorcode == 0) ? void (0) : __assert_fail
("errorcode == 0", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 73, __extension__ __PRETTY_FUNCTION__))
;
74
75 // Assign the data member
76 data_ = mutex;
77}
78
79// Destruct a Mutex
80MutexImpl::~MutexImpl()
81{
82 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
83 assert(mutex != nullptr)(static_cast <bool> (mutex != nullptr) ? void (0) : __assert_fail
("mutex != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 83, __extension__ __PRETTY_FUNCTION__))
;
84 pthread_mutex_destroy(mutex);
85 free(mutex);
86}
87
88bool
89MutexImpl::acquire()
90{
91 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
92 assert(mutex != nullptr)(static_cast <bool> (mutex != nullptr) ? void (0) : __assert_fail
("mutex != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 92, __extension__ __PRETTY_FUNCTION__))
;
93
94 int errorcode = pthread_mutex_lock(mutex);
95 return errorcode == 0;
96}
97
98bool
99MutexImpl::release()
100{
101 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
102 assert(mutex != nullptr)(static_cast <bool> (mutex != nullptr) ? void (0) : __assert_fail
("mutex != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 102, __extension__ __PRETTY_FUNCTION__))
;
103
104 int errorcode = pthread_mutex_unlock(mutex);
105 return errorcode == 0;
106}
107
108bool
109MutexImpl::tryacquire()
110{
111 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
112 assert(mutex != nullptr)(static_cast <bool> (mutex != nullptr) ? void (0) : __assert_fail
("mutex != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Support/Mutex.cpp"
, 112, __extension__ __PRETTY_FUNCTION__))
;
113
114 int errorcode = pthread_mutex_trylock(mutex);
115 return errorcode == 0;
116}
117
118}
119
120#elif defined(LLVM_ON_UNIX1)
121#include "Unix/Mutex.inc"
122#elif defined( LLVM_ON_WIN32)
123#include "Windows/Mutex.inc"
124#else
125#warning Neither LLVM_ON_UNIX1 nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
126#endif
127#endif