[PATCH] D48755: Clear errno before calling the function in RetryAfterSignal.

Ricky Zhou via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 28 23:33:50 PDT 2018


ricky created this revision.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D48755

Files:
  llvm/include/llvm/Support/Errno.h
  llvm/unittests/Support/ErrnoTest.cpp


Index: llvm/unittests/Support/ErrnoTest.cpp
===================================================================
--- llvm/unittests/Support/ErrnoTest.cpp
+++ llvm/unittests/Support/ErrnoTest.cpp
@@ -33,4 +33,7 @@
 
   std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
   EXPECT_EQ(47, *P);
+
+  errno = EINTR;
+  EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));
 }
Index: llvm/include/llvm/Support/Errno.h
===================================================================
--- llvm/include/llvm/Support/Errno.h
+++ llvm/include/llvm/Support/Errno.h
@@ -34,9 +34,10 @@
 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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48755.153440.patch
Type: text/x-patch
Size: 925 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180629/393ba70c/attachment.bin>


More information about the llvm-commits mailing list