[llvm] r336479 - [Support] Clear errno before calling the function in RetryAfterSignal.

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 6 19:46:13 PDT 2018


Author: chandlerc
Date: Fri Jul  6 19:46:12 2018
New Revision: 336479

URL: http://llvm.org/viewvc/llvm-project?rev=336479&view=rev
Log:
[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

Modified:
    llvm/trunk/include/llvm/Support/Errno.h
    llvm/trunk/unittests/Support/ErrnoTest.cpp

Modified: llvm/trunk/include/llvm/Support/Errno.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Errno.h?rev=336479&r1=336478&r2=336479&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Errno.h (original)
+++ llvm/trunk/include/llvm/Support/Errno.h Fri Jul  6 19:46:12 2018
@@ -34,9 +34,10 @@ template <typename FailT, typename Fun,
 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;
 }
 

Modified: llvm/trunk/unittests/Support/ErrnoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrnoTest.cpp?rev=336479&r1=336478&r2=336479&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrnoTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrnoTest.cpp Fri Jul  6 19:46:12 2018
@@ -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; }));
 }




More information about the llvm-commits mailing list