From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [94.100.177.109]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A9F584696C3 for ; Fri, 10 Apr 2020 05:51:05 +0300 (MSK) From: Alexander Turenko Date: Fri, 10 Apr 2020 05:50:39 +0300 Message-Id: <0bd904ddc2f18b1a078aac62fcc95485b36edd92.1586486220.git.alexander.turenko@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 01/13] popen: require popen handle to be non-NULL List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Cyrill Gorcunov Cc: tarantool-patches@dev.tarantool.org Further commits will add proper entries into the diagnostics area for failures inside popen functions. We should either report handle == NULL case via the diagnostics area or ensure that the NULL handle case is not possible. The latter approach is implemented in this commit. There are two reasons for this: * This way simplifies function contracts (one less kind of failure). * The popen Lua module (that will be implemented in the further commits) will not construct any logic using NULL as a handle. When 'NULL handle' error is not possible in the C API, it will be easier to verify that this failure is not possible the Lua API. A user of the C API should take care to don't call those functions with NULL handle. Part of #4031 --- src/lib/core/popen.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c index 6b6062215..5f74bc3ce 100644 --- a/src/lib/core/popen.c +++ b/src/lib/core/popen.c @@ -163,13 +163,13 @@ popen_may_io(struct popen_handle *handle, unsigned int idx, } /** - * Test if the handle is not nil and still have - * a living child process. + * Test if the handle still have a living child process. */ static inline bool popen_may_pidop(struct popen_handle *handle) { - if (!handle || handle->pid == -1) { + assert(handle != NULL); + if (handle->pid == -1) { errno = ESRCH; return false; } @@ -182,10 +182,7 @@ popen_may_pidop(struct popen_handle *handle) int popen_stat(struct popen_handle *handle, struct popen_stat *st) { - if (!handle) { - errno = ESRCH; - return -1; - } + assert(handle != NULL); st->pid = handle->pid; st->flags = handle->flags; @@ -204,11 +201,7 @@ popen_stat(struct popen_handle *handle, struct popen_stat *st) const char * popen_command(struct popen_handle *handle) { - if (!handle) { - errno = ESRCH; - return NULL; - } - + assert(handle != NULL); return (const char *)handle->command; } @@ -236,6 +229,8 @@ popen_write_timeout(struct popen_handle *handle, void *buf, size_t count, unsigned int flags, ev_tstamp timeout) { + assert(handle != NULL); + int idx = STDIN_FILENO; if (!(flags & POPEN_FLAG_FD_STDIN)) { @@ -268,6 +263,8 @@ popen_read_timeout(struct popen_handle *handle, void *buf, size_t count, unsigned int flags, ev_tstamp timeout) { + assert(handle != NULL); + int idx = flags & POPEN_FLAG_FD_STDOUT ? STDOUT_FILENO : STDERR_FILENO; @@ -380,10 +377,7 @@ popen_sigchld_handler(EV_P_ ev_child *w, int revents) int popen_state(struct popen_handle *handle, int *state, int *exit_code) { - if (!handle) { - errno = ESRCH; - return -1; - } + assert(handle != NULL); if (handle->pid != -1) { *state = POPEN_STATE_ALIVE; @@ -437,6 +431,8 @@ popen_send_signal(struct popen_handle *handle, int signo) { int ret; + assert(handle != NULL); + /* * A child may be killed or exited already. */ @@ -464,10 +460,7 @@ popen_delete(struct popen_handle *handle) { size_t i; - if (!handle) { - errno = ESRCH; - return -1; - } + assert(handle != NULL); if (popen_send_signal(handle, SIGKILL) && errno != ESRCH) return -1; -- 2.25.0