bitwiseworks / libcx

kLIBC Extension Library
GNU Lesser General Public License v2.1
11 stars 1 forks source link

LIBCx select returns success even if underlying select indicates failure #104

Closed dmik closed 1 year ago

dmik commented 1 year ago

@SilvanScherrer found out that LIBCx select override will return a positive value if a bad file descriptor is supplied to it, while in fact it should return -1 and set errno to EBADF. Here's the minimal test case:

/* end confdefs.h.  */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

int
main ()
{

  int rc;
  fd_set set;
  FD_ZERO(&set);
  FD_SET(100, &set); // non-existent FD
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 5;
  rc = select (101, &set, NULL, NULL, &timeout);
  printf("rc: %i, EBADF %s errno(%s)\n", rc, errno == EBADF?"yes":"no",strerror(errno));

  return rc;
}
dmik commented 1 year ago

This turns out to be a regression of commit https://github.com/bitwiseworks/libcx/commit/99f3976c3f55464be2c088c023854de98ce98695 indeed (as Silvan has found out because earlier LIBCx from 2020 worked fine here). The final else break is obviously missing there.

There could be a number of other strange effects in applications coming out of this bug (like not reporting errors when they happen and going on as if they didn't take place).

dmik commented 1 year ago

Besides the above regression there was another one from commit https://github.com/bitwiseworks/libcx/commit/b0a2ff0324d490ec68f4f9d201d2357b0b277c5d where supplying an invalid FD to select would make it always treat it as a dead socket after the other end's dead and hide EBADF either. Will also fix that.