HomeFreeBSD

libcasper: Use __VA_ARGS__ for function-like macros

Description

libcasper: Use VA_ARGS for function-like macros

cap_net.h uses "#define cap_f(chan, a) f(a)" to call the conventional
service function with the first argument of cap_f() dropped for
compatibility with the environment where the casper service is
unavailable. However, this function-like macro does not work when the
arguments contains C99 compound literals, such as f(chan, (int[]){1,2,3}).
The following is a typical example:

error = cap_getaddrinfo(capnet, "192.168.0.1", "100",
    &(struct addrinfo){
        .ai_family = AF_INET,
        .ai_flags = AI_NUMERICHOST
}, &res);

Using cap_f(chan, ...) and VA_ARGS in C99 seems a reasonable solution
for this problem. While there is a workaround using parenthesis around
the compound literal like f(chan, ((int[]){1,2,3})), it is not intuitive
and the above example works when the cap_net is available and f() is
defined as a function.

A possible moot point is that VA_ARGS cannot control how many
arguments are passed at the macro level, and "chan" is not protected.
However, "chan" is less likely to be written in compound literal, and
the function prototype should check the remaining arguments.

Reviewed by: oshogbo
Differential Revision: https://reviews.freebsd.org/D45858

Details

Provenance
hrsAuthored on Jun 12 2025, 6:04 PM
Reviewer
oshogbo
Differential Revision
D45858: libcasper: Use __VA_ARGS__ for function-like macros
Parents
rG4c556a4e8d14: libusb: implement `libusb_get_parent`
Branches
Unknown
Tags
Unknown