From 253e03cc3731b7edd3ad02fab4e24d3abfd86598 Mon Sep 17 00:00:00 2001 From: lubinglun Date: Wed, 17 Apr 2024 10:39:24 +0800 Subject: [PATCH] add fd size check for FD_SET, FD_CLR, FD_ISSET https://gitee.com/openharmony/interface_sdk_c/issues/I9HC4S Signed-off-by: lubinglun Change-Id: I3e7937a353c68b7632c99cb476ac37ce9da9f96b --- third_party/musl/ndk_musl_include/sys/select.h | 15 ++++++++++++--- third_party/musl/ndk_script/adapter/libc.ndk.json | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/third_party/musl/ndk_musl_include/sys/select.h b/third_party/musl/ndk_musl_include/sys/select.h index d933d308c..48f941038 100644 --- a/third_party/musl/ndk_musl_include/sys/select.h +++ b/third_party/musl/ndk_musl_include/sys/select.h @@ -23,10 +23,19 @@ typedef struct { unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)]; } fd_set; +/** +* @brief Check if fd passed to fd_set is valid (<1024). +* +* This method will check fd(0 <= fd < 1024) is valid for select, abort if not. +* +* @param fd: Specified fd. +*/ +void __fd_chk(int fd); + #define FD_ZERO(s) do { int __i; unsigned long *__b=(s)->fds_bits; for(__i=sizeof (fd_set)/sizeof (long); __i; __i--) *__b++=0; } while(0) -#define FD_SET(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1UL<<((d)%(8*sizeof(long))))) -#define FD_CLR(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1UL<<((d)%(8*sizeof(long))))) -#define FD_ISSET(d, s) !!((s)->fds_bits[(d)/(8*sizeof(long))] & (1UL<<((d)%(8*sizeof(long))))) +#define FD_SET(d, s) do { __fd_chk(d); ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1UL<<((d)%(8*sizeof(long))))); } while(0) +#define FD_CLR(d, s) do { __fd_chk(d); ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1UL<<((d)%(8*sizeof(long))))); } while(0) +#define FD_ISSET(d, s) ((d) >= 0 && (d) < FD_SETSIZE && (!!((s)->fds_bits[(d)/(8*sizeof(long))] & (1UL<<((d)%(8*sizeof(long))))))) int select (int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict); int pselect (int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, const struct timespec *__restrict, const sigset_t *__restrict); diff --git a/third_party/musl/ndk_script/adapter/libc.ndk.json b/third_party/musl/ndk_script/adapter/libc.ndk.json index 09454d9ff..41070eecd 100644 --- a/third_party/musl/ndk_script/adapter/libc.ndk.json +++ b/third_party/musl/ndk_script/adapter/libc.ndk.json @@ -6,6 +6,7 @@ { "name": "__duplocale" }, { "name": "__errno_location" }, { "name": "__fbufsize" }, + { "name": "__fd_chk"}, { "name": "__flbf" }, { "name": "__flt_rounds" }, { "name": "__fpclassify" }, -- Gitee