vitasdk / newlib

PS Vita Newlib port.
GNU General Public License v2.0
47 stars 29 forks source link

select function in "sys/select.h" randomly crashes the app #97

Open cy33hc opened 1 year ago

cy33hc commented 1 year ago

I'm working on a homebrew that will read and download files from a http server. In the app I would check if the socket is open and still readable before trying to read from it. The app would randomly crash on the "select" function.

Here's the code sniplet of that function.

template <typename T> ssize_t handle_EINTR(T fn) {
  ssize_t res = false;
  while (true) {
    res = fn();
    if (res < 0 && errno == EINTR) { continue; }
    break;
  }
  return res;
}

ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
  if (sock >= FD_SETSIZE) { return 1; }

  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(sock, &fds);

  timeval tv;
  tv.tv_sec = static_cast<long>(sec);
  tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);

  return handle_EINTR([&]() {
    return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);  <<< crash on this line randomly
  });
}