Since commit feaf75658783 ("nolibc: fix fd_set type"), an fd_set is an
array of unsigned long, but the FD_* macros still build their masks as
an int or an unsigned int: 1 << n in FD_SET(), 1U << n in FD_CLR() and
FD_ISSET(), where n is the fd modulo 64 on 64-bit architectures. So:

- FD_SET() of an fd with n == 31 sets the bits for n = 31-63, as
  1 << 31 is negative and gets sign-extended.
- For n >= 32 the shifts are undefined. x86-64, for instance, masks
  the shift count, so FD_SET(40) and FD_ISSET(40) use the bit of fd 8.
- FD_CLR() zero-extends ~(1U << n), so it also clears the bits for
  n = 32-63, whichever fd it is given.

select() on fd 40, for instance, fails with EBADF, or watches fd 8
instead if that one is open.

Build the masks from 1UL.

Fixes: feaf75658783 ("nolibc: fix fd_set type")
Cc: [email protected] # v6.2+
Assisted-by: LLM
Signed-off-by: Danish Khateeb <[email protected]>
---
 tools/include/nolibc/sys/select.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/include/nolibc/sys/select.h 
b/tools/include/nolibc/sys/select.h
index 6d65d9ef3d6a..f299c79939f5 100644
--- a/tools/include/nolibc/sys/select.h
+++ b/tools/include/nolibc/sys/select.h
@@ -26,7 +26,7 @@ typedef struct {
                int __fd = (fd);                                        \
                if (__fd >= 0)                                          \
                        __set->fds[__fd / FD_SETIDXMASK] &=             \
-                               ~(1U << (__fd & FD_SETBITMASK));        \
+                               ~(1UL << (__fd & FD_SETBITMASK));       \
        } while (0)
 
 #define FD_SET(fd, set) do {                                           \
@@ -34,7 +34,7 @@ typedef struct {
                int __fd = (fd);                                        \
                if (__fd >= 0)                                          \
                        __set->fds[__fd / FD_SETIDXMASK] |=             \
-                               1 << (__fd & FD_SETBITMASK);            \
+                               1UL << (__fd & FD_SETBITMASK);          \
        } while (0)
 
 #define FD_ISSET(fd, set) ({                                           \
@@ -43,7 +43,7 @@ typedef struct {
                int __r = 0;                                            \
                if (__fd >= 0)                                          \
                        __r = !!(__set->fds[__fd / FD_SETIDXMASK] &     \
-1U << (__fd & FD_SETBITMASK));                                         \
+1UL << (__fd & FD_SETBITMASK));                                                
\
                __r;                                                    \
        })
 
-- 
2.55.0


Reply via email to