RE: [PATCH 11/42] tools/nolibc/types: move the FD_* functions to macros in types.h

From: David Laight
Date: Mon Feb 07 2022 - 12:11:23 EST


From: Willy Tarreau
> Sent: 07 February 2022 16:23
>
> FD_SET, FD_CLR, FD_ISSET, FD_ZERO are supposed to be macros and not
> functions.

Are you sure?
I'd have thought they could be either.
There are certainly systems where they are functions.
They can be implemented as an array of fd numbers rather than a bitmap.

> In addition we already have a file dedicated to such macros
> and types used by syscalls, it's types.h, so let's move them
> there and turn them to macros. FD_CLR() and FD_ISSET() were missing,
> so they were added. FD_ZERO() now deals with its own loop so that it
> doesn't rely on memset() that sets one byte at a time.
>
....
> +#define FD_CLR(fd, set) do { \
> + int __fd = (int)(fd); \
> + fd_set *__set = (fd_set *)(set); \

I'm not sure you really want either cast.
They are just likely to hide some horrid bugs.

+ if (__fd >= 0 && __fd < FD_SETSIZE) \
+ __set->fd32[__fd / 32] &= ~(1U << (__fd & 31)); \
+ } while (0)
+

Do you need the range check?
I don't think glibc has one.
Things just break in obscure ways when you use select on big fd.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)