From 5d774cce1de96fcb3ec7ec7ea91f02bc6f24ef75 Mon Sep 17 00:00:00 2001 From: Ralph Holmes Date: Fri, 30 Sep 2016 16:40:17 +0100 Subject: [PATCH] Fix execl(3) sentinel undefined behaviour. execl(3) and its variants use a sentinel to terminate the variadic argument list, in the form of a null pointer constant of type pointer to char. POSIX mandates that NULL is a null pointer constant of type pointer to void, which is not of an equivalent type to that required by execl(3) and its variants, resulting in undefined behaviour. This commit casts all such instances of NULL to pointer to char type. For consistency, it also adds const-qualification to any such instances which had already been casted, and were not const-qualified. --- dispd/client/session.c | 4 ++-- init/init.c | 2 +- libc/stdio/popen.c | 2 +- libc/stdlib/system.c | 2 +- sh/proper-sh.c | 2 +- sh/sh.c | 2 +- trianglix/trianglix.cpp | 10 +++++----- utils/help.c | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dispd/client/session.c b/dispd/client/session.c index f85f3fa5..3ba09470 100644 --- a/dispd/client/session.c +++ b/dispd/client/session.c @@ -91,13 +91,13 @@ bool dispd_session_setup_game_rgba(struct dispd_session* session) const char* chvideomode = "chvideomode"; #if 1 // TODO chvideomode currently launches --bpp 32 as a program... - execlp(chvideomode, chvideomode, NULL); + execlp(chvideomode, chvideomode, (const char*) NULL); #else execlp(chvideomode, chvideomode, "--bpp", "32", "--show-graphics", "true", "--show-text", "false", - NULL); + (const char*) NULL); #endif perror(chvideomode); exit(127); diff --git a/init/init.c b/init/init.c index 1ed41376..b68a68a8 100644 --- a/init/init.c +++ b/init/init.c @@ -395,7 +395,7 @@ static void set_kblayout(void) return warning("unable to set keyboard layout: fork: %m"); if ( !child_pid ) { - execlp("chkblayout", "chkblayout", "--", kblayout, (char*) NULL); + execlp("chkblayout", "chkblayout", "--", kblayout, (const char*) NULL); warning("setting keyboard layout: chkblayout: %m"); _exit(127); } diff --git a/libc/stdio/popen.c b/libc/stdio/popen.c index 405e74b8..7c2ff1b0 100644 --- a/libc/stdio/popen.c +++ b/libc/stdio/popen.c @@ -131,7 +131,7 @@ FILE* popen(const char* command, const char* type) close(pipefds[unused_end]) ) _exit(127); - execlp("sh", "sh", "-c", command, NULL); + execlp("sh", "sh", "-c", command, (const char*) NULL); _exit(127); cleanup_pipes: diff --git a/libc/stdlib/system.c b/libc/stdlib/system.c index fee02d47..efb36cc7 100644 --- a/libc/stdlib/system.c +++ b/libc/stdlib/system.c @@ -39,6 +39,6 @@ int system(const char* command) return ret_error; return status; } - execlp("sh", "sh", "-c", command, NULL); + execlp("sh", "sh", "-c", command, (const char*) NULL); _exit(exit_error); } diff --git a/sh/proper-sh.c b/sh/proper-sh.c index 3f3d60d8..89fbae4d 100644 --- a/sh/proper-sh.c +++ b/sh/proper-sh.c @@ -46,7 +46,7 @@ bool is_existing_shell(const char* candidate) open("/dev/null", O_RDONLY); open("/dev/null", O_WRONLY); open("/dev/null", O_WRONLY); - execlp("which", "which", "--", candidate, (char*) NULL); + execlp("which", "which", "--", candidate, (const char*) NULL); exit(127); } int status; diff --git a/sh/sh.c b/sh/sh.c index 221195b9..af0292b4 100644 --- a/sh/sh.c +++ b/sh/sh.c @@ -1299,7 +1299,7 @@ struct execute_result execute(char** tokens, if ( interactive && errno == ENOENT ) { int errno_saved = errno; - execlp("command-not-found", "command-not-found", argv[0], NULL); + execlp("command-not-found", "command-not-found", argv[0], (const char*) NULL); errno = errno_saved; } diff --git a/trianglix/trianglix.cpp b/trianglix/trianglix.cpp index c0f42c77..02da4d95 100644 --- a/trianglix/trianglix.cpp +++ b/trianglix/trianglix.cpp @@ -193,7 +193,7 @@ static void ExecuteFile(const char* path, int curdirfd) if ( ForkAndWait() ) { fchdir(curdirfd); - execl(path, path, (char*) NULL); + execl(path, path, (const char*) NULL); _exit(127); } } @@ -204,7 +204,7 @@ static void DisplayFile(const char* path, int curdirfd) if ( ForkAndWait() ) { fchdir(curdirfd); - execlp("editor", "editor", path, (char*) NULL); + execlp("editor", "editor", path, (const char*) NULL); _exit(127); } } @@ -215,7 +215,7 @@ static void ExecutePath(const char* path, int curdirfd) if ( ForkAndWait() ) { fchdir(curdirfd); - execlp(path, path, (char*) NULL); + execlp(path, path, (const char*) NULL); _exit(127); } } @@ -229,7 +229,7 @@ void ExecuteShellCommand(const char* command, int curdirfd) if ( ForkAndWait() ) { fchdir(curdirfd); - execlp("sh", "sh", "-c", command, (char*) NULL); + execlp("sh", "sh", "-c", command, (const char*) NULL); error(127, errno, "`%s'", "sh"); } unsigned int cur_termmode; @@ -505,7 +505,7 @@ private: void exec_program::invoke() { - execlp(program, program, (char*) NULL); + execlp(program, program, (const char*) NULL); } class development : public object diff --git a/utils/help.c b/utils/help.c index d2698af2..eb8cf803 100644 --- a/utils/help.c +++ b/utils/help.c @@ -52,8 +52,8 @@ int main(void) dup2(pipe_fds[0], 0); close(pipe_fds[0]); close(pipe_fds[1]); - execlp("column", "column", NULL); - execlp("cat", "cat", NULL); + execlp("column", "column", (const char*) NULL); + execlp("cat", "cat", (const char*) NULL); int ic; while ( (ic = fgetc(stdin)) != EOF ) fputc(ic, stdout);