From 4a33aa7ad93857d0c6b5ea24983e12b1619b10c0 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 21 Jan 2008 09:24:16 +0100 Subject: [PATCH] Be more careful on command exit. If the callback returned zero, there's nothing to do. If it returned positive, we have to send out the result buffer and must free it afterwards. Sending the buffer may well lead to an error condition, so always check the return value of the corresponding call to send_buffer(). Finally, it the command handler returned a negative value, we have to send an error message to the client which might give rise to another error (if sending the errror message failed). In this case, ignore the send errror and return the previous error value. --- aft.c | 33 +++++++++++++++++++++------------ attribute.c | 27 +++++++++++++++++---------- blob.c | 10 +++++++--- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/aft.c b/aft.c index d8436929..001026db 100644 --- a/aft.c +++ b/aft.c @@ -2004,11 +2004,14 @@ int com_touch(int fd, int argc, char * const * const argv) return -E_AFT_SYNTAX; ret = send_option_arg_callback_request(&query, argc - i, argv + i, com_touch_callback, &result); - if (ret > 0) { - send_buffer(fd, (char *)result.data); - free(result.data); - } else if (ret < 0) + if (!ret) + return 0; + if (ret < 0) { send_va_buffer(fd, "%s\n", para_strerror(-ret)); + return ret; + } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } @@ -2116,11 +2119,14 @@ int com_rm(int fd, int argc, char * const * const argv) return -E_AFT_SYNTAX; ret = send_option_arg_callback_request(&query, argc - i, argv + i, com_rm_callback, &result); - if (ret > 0) { - send_buffer(fd, (char *)result.data); - free(result.data); - } else if (ret < 0) + if (!ret) + return 0; + if (ret < 0) { send_va_buffer(fd, "%s\n", para_strerror(-ret)); + return ret; + } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } @@ -2277,11 +2283,14 @@ int com_cpsi(int fd, int argc, char * const * const argv) flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags; ret = send_option_arg_callback_request(&options, argc - i, argv + i, com_cpsi_callback, &result); - if (ret > 0) { - send_buffer(fd, (char *)result.data); - free(result.data); - } else + if (!ret) + return 0; + if (ret < 0) { send_va_buffer(fd, "%s\n", para_strerror(-ret)); + return ret; + } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } diff --git a/attribute.c b/attribute.c index 03a832fb..c4bc0bca 100644 --- a/attribute.c +++ b/attribute.c @@ -202,13 +202,17 @@ int com_lsatt(int fd, int argc, char * const * const argv) } ret = send_option_arg_callback_request(&options, argc - i, argv + i, com_lsatt_callback, &result); - if (ret > 0) { - ret = send_buffer(fd, (char *)result.data); - free(result.data); - } else if (ret < 0) + if (!ret) { + if (argc > 1) + ret = send_va_buffer(fd, "no matches\n"); + return ret; + } + if (ret < 0) { send_va_buffer(fd, "%s\n", para_strerror(-ret)); - else if (argc > 1) - send_va_buffer(fd, "no matches\n"); + return ret; + } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } @@ -482,11 +486,14 @@ int com_rmatt(int fd, int argc, char * const * const argv) return -E_ATTR_SYNTAX; ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback, &result); - if (ret > 0) { - send_buffer(fd, (char *)result.data); - free(result.data); - } else + if (!ret) + return 0; + if (ret < 0) { send_va_buffer(fd, "%s\n", para_strerror(-ret)); + return ret; + } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } diff --git a/blob.c b/blob.c index 33f75dcf..c42a5f7a 100644 --- a/blob.c +++ b/blob.c @@ -146,10 +146,14 @@ static int com_lsblob(callback_function *f, int fd, int argc, char * const * con // return -E_BLOB_SYNTAX; ret = send_option_arg_callback_request(&options, argc - i, argv + i, f, &result); - if (ret > 0) { - send_buffer(fd, (char *)result.data); - free(result.data); + if (!ret) + return 0; + if (ret < 0) { + send_va_buffer(fd, "%s\n", para_strerror(-ret)); + return ret; } + ret = send_buffer(fd, (char *)result.data); + free(result.data); return ret; } -- 2.39.5