38 lines · cpp
1// RUN: %clang_analyze_cc1 \2// RUN: -analyzer-checker=core,unix.BlockInCriticalSection \3// RUN: -analyzer-output text -verify %s4 5// expected-no-diagnostics6 7#include "Inputs/system-header-simulator-cxx-std-locks.h"8 9std::mutex mtx;10using ssize_t = long long;11using size_t = unsigned long long;12int open(const char *__file, int __oflag, ...);13ssize_t read(int fd, void *buf, size_t count);14void close(int fd);15#define O_RDONLY 0016#define O_NONBLOCK 0400017 18void foo() {19 std::lock_guard<std::mutex> lock(mtx);20 21 const char *filename = "example.txt";22 int fd = open(filename, O_RDONLY | O_NONBLOCK);23 24 char buffer[200] = {};25 read(fd, buffer, 199); // no-warning: fd is a non-block file descriptor or equals to -126 close(fd);27}28 29void foo1(int fd) {30 std::lock_guard<std::mutex> lock(mtx);31 32 const char *filename = "example.txt";33 char buffer[200] = {};34 if (fd == -1)35 read(fd, buffer, 199); // no-warning: consider file descriptor is a symbol equals to -136 close(fd);37}38