Browse Source

[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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336479 91177308-0d34-0410-b5e6-96231b3b80d8
Chandler Carruth 7 năm trước cách đây
mục cha
commit
5fd5dc8e41

+ 3 - 2
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,
 inline auto RetryAfterSignal(const FailT &Fail, const Fun &F,
                              const Args &... As) -> decltype(F(As...)) {
                              const Args &... As) -> decltype(F(As...)) {
   decltype(F(As...)) Res;
   decltype(F(As...)) Res;
-  do
+  do {
+    errno = 0;
     Res = F(As...);
     Res = F(As...);
-  while (Res == Fail && errno == EINTR);
+  } while (Res == Fail && errno == EINTR);
   return Res;
   return Res;
 }
 }
 
 

+ 3 - 0
unittests/Support/ErrnoTest.cpp

@@ -33,4 +33,7 @@ TEST(ErrnoTest, RetryAfterSignal) {
 
 
   std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
   std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
   EXPECT_EQ(47, *P);
   EXPECT_EQ(47, *P);
+
+  errno = EINTR;
+  EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));
 }
 }