Skip to content

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

@cy33hc

Description

@cy33hc

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
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions