Skip to content

Commit feaf756

Browse files
svens-s390paulmckrcu
authored andcommitted
nolibc: fix fd_set type
The kernel uses unsigned long for the fd_set bitmap, but nolibc use u32. This works fine on little endian machines, but fails on big endian. Convert to unsigned long to fix this. Signed-off-by: Sven Schnelle <[email protected]> Signed-off-by: Willy Tarreau <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 1b929c0 commit feaf756

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

tools/include/nolibc/types.h

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,39 +89,46 @@
8989
#define EXIT_SUCCESS 0
9090
#define EXIT_FAILURE 1
9191

92+
#define FD_SETIDXMASK (8 * sizeof(unsigned long))
93+
#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
94+
9295
/* for select() */
9396
typedef struct {
94-
uint32_t fd32[(FD_SETSIZE + 31) / 32];
97+
unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
9598
} fd_set;
9699

97-
#define FD_CLR(fd, set) do { \
98-
fd_set *__set = (set); \
99-
int __fd = (fd); \
100-
if (__fd >= 0) \
101-
__set->fd32[__fd / 32] &= ~(1U << (__fd & 31)); \
100+
#define FD_CLR(fd, set) do { \
101+
fd_set *__set = (set); \
102+
int __fd = (fd); \
103+
if (__fd >= 0) \
104+
__set->fds[__fd / FD_SETIDXMASK] &= \
105+
~(1U << (__fd & FX_SETBITMASK)); \
102106
} while (0)
103107

104-
#define FD_SET(fd, set) do { \
105-
fd_set *__set = (set); \
106-
int __fd = (fd); \
107-
if (__fd >= 0) \
108-
__set->fd32[__fd / 32] |= 1U << (__fd & 31); \
108+
#define FD_SET(fd, set) do { \
109+
fd_set *__set = (set); \
110+
int __fd = (fd); \
111+
if (__fd >= 0) \
112+
__set->fds[__fd / FD_SETIDXMASK] |= \
113+
1 << (__fd & FD_SETBITMASK); \
109114
} while (0)
110115

111-
#define FD_ISSET(fd, set) ({ \
112-
fd_set *__set = (set); \
113-
int __fd = (fd); \
114-
int __r = 0; \
115-
if (__fd >= 0) \
116-
__r = !!(__set->fd32[__fd / 32] & 1U << (__fd & 31)); \
117-
__r; \
116+
#define FD_ISSET(fd, set) ({ \
117+
fd_set *__set = (set); \
118+
int __fd = (fd); \
119+
int __r = 0; \
120+
if (__fd >= 0) \
121+
__r = !!(__set->fds[__fd / FD_SETIDXMASK] & \
122+
1U << (__fd & FD_SET_BITMASK)); \
123+
__r; \
118124
})
119125

120-
#define FD_ZERO(set) do { \
121-
fd_set *__set = (set); \
122-
int __idx; \
123-
for (__idx = 0; __idx < (FD_SETSIZE+31) / 32; __idx ++) \
124-
__set->fd32[__idx] = 0; \
126+
#define FD_ZERO(set) do { \
127+
fd_set *__set = (set); \
128+
int __idx; \
129+
int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
130+
for (__idx = 0; __idx < __size; __idx++) \
131+
__set->fds[__idx] = 0; \
125132
} while (0)
126133

127134
/* for poll() */

0 commit comments

Comments
 (0)