getopt: fix null pointer arithmetic ub
[musl] / src / ipc / semtimedop.c
1 #define _GNU_SOURCE
2 #include <sys/sem.h>
3 #include <errno.h>
4 #include "syscall.h"
5 #include "ipc.h"
6
7 #define IS32BIT(x) !((x)+0x80000000ULL>>32)
8 #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
9
10 #if !defined(SYS_semtimedop) && !defined(SYS_ipc)
11 #define NO_TIME32 1
12 #else
13 #define NO_TIME32 0
14 #endif
15
16 int semtimedop(int id, struct sembuf *buf, size_t n, const struct timespec *ts)
17 {
18 #ifdef SYS_semtimedop_time64
19         time_t s = ts ? ts->tv_sec : 0;
20         long ns = ts ? ts->tv_nsec : 0;
21         int r = -ENOSYS;
22         if (NO_TIME32 || !IS32BIT(s))
23                 r = __syscall(SYS_semtimedop_time64, id, buf, n,
24                         ts ? ((long long[]){s, ns}) : 0);
25         if (NO_TIME32 || r!=-ENOSYS) return __syscall_ret(r);
26         ts = ts ? (void *)(long[]){CLAMP(s), ns} : 0;
27 #endif
28 #if defined(SYS_ipc)
29         return syscall(SYS_ipc, IPCOP_semtimedop, id, n, 0, buf, ts);
30 #elif defined(SYS_semtimedop)
31         return syscall(SYS_semtimedop, id, buf, n, ts);
32 #else
33         return __syscall_ret(-ENOSYS);
34 #endif
35 }