Accessing elements in an empty va_list is undefined behaviour. Therefore, remove the variadicness from safe_semctl as it always calls semctl with the union semun argument included.
Adapt the SAFE_SEMCTL macro to handle the change by passing a zero-initialised union semun if it's omitted.
Signed-off-by: Tudor Cretu tudor.cretu@arm.com --- A similar change to the safe_open one.
A review branch is created here: https://git.morello-project.org/tudcre01/morello-linux-ltp/-/commits/review/...
--- include/tst_safe_sysv_ipc.h | 15 ++++++++++++--- lib/tst_safe_sysv_ipc.c | 10 +--------- 2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/include/tst_safe_sysv_ipc.h b/include/tst_safe_sysv_ipc.h index 7804ce192..60d5ac5e6 100644 --- a/include/tst_safe_sysv_ipc.h +++ b/include/tst_safe_sysv_ipc.h @@ -10,6 +10,7 @@ #include <sys/msg.h> #include <sys/shm.h> #include <sys/sem.h> +#include "lapi/sem.h"
int safe_msgget(const char *file, const int lineno, key_t key, int msgflg); #define SAFE_MSGGET(key, msgflg) \ @@ -58,10 +59,18 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems, safe_semget(__FILE__, __LINE__, (key), (nsems), (semflg))
int safe_semctl(const char *file, const int lineno, int semid, int semnum, - int cmd, ...); + int cmd, union semun un); +#define _SAFE_SEMCTL_3(semid, semnum, cmd) \ + safe_semctl(__FILE__, __LINE__, (semid), (semnum), (cmd), (union semun){0}) + +#define _SAFE_SEMCTL_4(semid, semnum, cmd, un) \ + safe_semctl(__FILE__, __LINE__, (semid), (semnum), (cmd), (un)) + +#define _SAFE_SEMCTL_HELPER(_0, _1, _SAFE_SEMCTL_X, ...) _SAFE_SEMCTL_X + #define SAFE_SEMCTL(semid, semnum, cmd, ...) ({ \ - int tst_ret_ = safe_semctl(__FILE__, __LINE__, (semid), (semnum), \ - (cmd), ##__VA_ARGS__); \ + int tst_ret_ = _SAFE_SEMCTL_HELPER(, ##__VA_ARGS__, _SAFE_SEMCTL_4, \ + _SAFE_SEMCTL_3) ((semid), (semnum), (cmd), ##__VA_ARGS__); \ (semid) = ((cmd) == IPC_RMID ? -1 : (semid)); \ tst_ret_; })
diff --git a/lib/tst_safe_sysv_ipc.c b/lib/tst_safe_sysv_ipc.c index 5eaa82539..f99f6db5e 100644 --- a/lib/tst_safe_sysv_ipc.c +++ b/lib/tst_safe_sysv_ipc.c @@ -228,17 +228,9 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems, }
int safe_semctl(const char *file, const int lineno, int semid, int semnum, - int cmd, ...) + int cmd, union semun un) { int rval; - va_list va; - union semun un; - - va_start(va, cmd); - - un = va_arg(va, union semun); - - va_end(va);
rval = semctl(semid, semnum, cmd, un);
On 01/11/2022 17:29, Tudor Cretu wrote:
Accessing elements in an empty va_list is undefined behaviour. Therefore, remove the variadicness from safe_semctl as it always calls semctl with the union semun argument included.
Adapt the SAFE_SEMCTL macro to handle the change by passing a zero-initialised union semun if it's omitted.
Signed-off-by: Tudor Cretu tudor.cretu@arm.com
A similar change to the safe_open one.
Very similar indeed ! It looks good to me.
Regards, Téo
A review branch is created here: https://git.morello-project.org/tudcre01/morello-linux-ltp/-/commits/review/...
include/tst_safe_sysv_ipc.h | 15 ++++++++++++--- lib/tst_safe_sysv_ipc.c | 10 +--------- 2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/include/tst_safe_sysv_ipc.h b/include/tst_safe_sysv_ipc.h index 7804ce192..60d5ac5e6 100644 --- a/include/tst_safe_sysv_ipc.h +++ b/include/tst_safe_sysv_ipc.h @@ -10,6 +10,7 @@ #include <sys/msg.h> #include <sys/shm.h> #include <sys/sem.h> +#include "lapi/sem.h" int safe_msgget(const char *file, const int lineno, key_t key, int msgflg); #define SAFE_MSGGET(key, msgflg) \ @@ -58,10 +59,18 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems, safe_semget(__FILE__, __LINE__, (key), (nsems), (semflg)) int safe_semctl(const char *file, const int lineno, int semid, int semnum,
int cmd, ...);
int cmd, union semun un);
+#define _SAFE_SEMCTL_3(semid, semnum, cmd) \
- safe_semctl(__FILE__, __LINE__, (semid), (semnum), (cmd), (union semun){0})
This seems t
+#define _SAFE_SEMCTL_4(semid, semnum, cmd, un) \
- safe_semctl(__FILE__, __LINE__, (semid), (semnum), (cmd), (un))
+#define _SAFE_SEMCTL_HELPER(_0, _1, _SAFE_SEMCTL_X, ...) _SAFE_SEMCTL_X
- #define SAFE_SEMCTL(semid, semnum, cmd, ...) ({ \
- int tst_ret_ = safe_semctl(__FILE__, __LINE__, (semid), (semnum), \
(cmd), ##__VA_ARGS__); \
- int tst_ret_ = _SAFE_SEMCTL_HELPER(, ##__VA_ARGS__, _SAFE_SEMCTL_4, \
(semid) = ((cmd) == IPC_RMID ? -1 : (semid)); \ tst_ret_; })_SAFE_SEMCTL_3) ((semid), (semnum), (cmd), ##__VA_ARGS__); \
diff --git a/lib/tst_safe_sysv_ipc.c b/lib/tst_safe_sysv_ipc.c index 5eaa82539..f99f6db5e 100644 --- a/lib/tst_safe_sysv_ipc.c +++ b/lib/tst_safe_sysv_ipc.c @@ -228,17 +228,9 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems, } int safe_semctl(const char *file, const int lineno, int semid, int semnum,
int cmd, ...)
{ int rval;int cmd, union semun un)
- va_list va;
- union semun un;
- va_start(va, cmd);
- un = va_arg(va, union semun);
- va_end(va);
rval = semctl(semid, semnum, cmd, un);
linux-morello-ltp@op-lists.linaro.org