1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00
llvm-mirror/unittests/Support/ErrnoTest.cpp
Chandler Carruth f501d225b8 [Support] Clear errno before calling the function in RetryAfterSignal.
For certain APIs, the return value of the function does not distinguish
between failure (which populates errno) and other non-error conditions
(which do not set errno).

For example, `fgets` returns `NULL` both when an error has occurred, or
upon EOF. If `errno` is already `EINTR` for whatever reason, then
```
RetryAfterSignal(nullptr, fgets, ...);
```
on a stream that has reached EOF would infinite loop.

Fix this by setting `errno` to `0` before each attempt in
`RetryAfterSignal`.

Patch by Ricky Zhou!

Differential Revision: https://reviews.llvm.org/D48755

llvm-svn: 336479
2018-07-07 02:46:12 +00:00

40 lines
1.1 KiB
C++

//===- ErrnoTest.cpp - Error handling unit tests --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errno.h"
#include "gtest/gtest.h"
using namespace llvm::sys;
TEST(ErrnoTest, RetryAfterSignal) {
EXPECT_EQ(1, RetryAfterSignal(-1, [] { return 1; }));
EXPECT_EQ(-1, RetryAfterSignal(-1, [] {
errno = EAGAIN;
return -1;
}));
EXPECT_EQ(EAGAIN, errno);
unsigned calls = 0;
EXPECT_EQ(1, RetryAfterSignal(-1, [&calls] {
errno = EINTR;
++calls;
return calls == 1 ? -1 : 1;
}));
EXPECT_EQ(2u, calls);
EXPECT_EQ(1, RetryAfterSignal(-1, [](int x) { return x; }, 1));
std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
EXPECT_EQ(47, *P);
errno = EINTR;
EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));
}