diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2018-07-07 02:46:12 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2018-07-07 02:46:12 +0000 |
commit | 9659a127a34402d02637e0f52bfab9ee7543fb5b (patch) | |
tree | a5f2e7b3ef63ffcb322e20cffe39606b42921231 /llvm | |
parent | ecce5c95977ff018ab6e5200bb2937c6b10a7ef0 (diff) | |
download | bcm5719-llvm-9659a127a34402d02637e0f52bfab9ee7543fb5b.tar.gz bcm5719-llvm-9659a127a34402d02637e0f52bfab9ee7543fb5b.zip |
[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
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/Support/Errno.h | 5 | ||||
-rw-r--r-- | llvm/unittests/Support/ErrnoTest.cpp | 3 |
2 files changed, 6 insertions, 2 deletions
diff --git a/llvm/include/llvm/Support/Errno.h b/llvm/include/llvm/Support/Errno.h index 35dc1ea7cf8..8069c3639df 100644 --- a/llvm/include/llvm/Support/Errno.h +++ b/llvm/include/llvm/Support/Errno.h @@ -34,9 +34,10 @@ template <typename FailT, typename Fun, typename... Args> inline auto RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &... As) -> decltype(F(As...)) { decltype(F(As...)) Res; - do + do { + errno = 0; Res = F(As...); - while (Res == Fail && errno == EINTR); + } while (Res == Fail && errno == EINTR); return Res; } diff --git a/llvm/unittests/Support/ErrnoTest.cpp b/llvm/unittests/Support/ErrnoTest.cpp index 67f834a938d..701ac960825 100644 --- a/llvm/unittests/Support/ErrnoTest.cpp +++ b/llvm/unittests/Support/ErrnoTest.cpp @@ -33,4 +33,7 @@ TEST(ErrnoTest, RetryAfterSignal) { std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); })); EXPECT_EQ(47, *P); + + errno = EINTR; + EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; })); } |