From 118166a527ae5f572e3faf5e1f3e569659342e7e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 18 Mar 2025 19:31:33 +0100 Subject: [PATCH] Check return value of lsu_com_help(). This function fails if an invalid command name is passed as the argument, yet all callers ignore the error. Modify the callers to print the strerror text as appropriate and no longer do that in lsu_lopsub_error(). Rename this function and introduce the error type argument to print more meaningful error messages. One visible consequence is that para_client help does-not-exist used to succeed while it now exits with status 1. --- audiod_command.c | 8 ++++---- command.c | 13 ++++++++----- lsu.c | 26 +++++++++++++------------- play.c | 12 ++++++++---- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/audiod_command.c b/audiod_command.c index 5f0b35a5..a7b65b1a 100644 --- a/audiod_command.c +++ b/audiod_command.c @@ -204,15 +204,15 @@ __malloc static char *audiod_status_string(void) static int com_help(int fd, struct lls_parse_result *lpr) { char *buf; - int ret; + int ret, ret2; const struct lls_opt_result *r = lls_opt_result(LSG_AUDIOD_CMD_HELP_OPT_LONG, lpr); bool long_help = lls_opt_given(r); - lsu_com_help(long_help, lpr, audiod_cmd_suite, NULL, &buf, NULL); - ret = client_write(fd, buf); + ret = lsu_com_help(long_help, lpr, audiod_cmd_suite, NULL, &buf, NULL); + ret2 = client_write(fd, buf); free(buf); - return ret < 0? ret : 0; + return ret < 0? ret : ret2; } EXPORT_AUDIOD_CMD_HANDLER(help) diff --git a/command.c b/command.c index 78c9106c..5cdeb7b4 100644 --- a/command.c +++ b/command.c @@ -579,13 +579,16 @@ static const char *aux_info_cb(unsigned cmd_num, bool verbose) static int com_help(struct command_context *cc, struct lls_parse_result *lpr) { char *buf; - int ret; unsigned n; bool long_help = SERVER_CMD_OPT_GIVEN(HELP, LONG, lpr); - - lsu_com_help(long_help, lpr, server_cmd_suite, aux_info_cb, &buf, &n); - ret = send_sb(&cc->scc, buf, n, SBD_OUTPUT, false); - return ret; + int ret, ret2; + uint8_t band; + + ret = lsu_com_help(long_help, lpr, server_cmd_suite, aux_info_cb, + &buf, &n); + band = ret < 0? SBD_ERROR_LOG : SBD_OUTPUT; + ret2 = send_sb(&cc->scc, buf, n, band, false); + return ret >= 0? ret2 : ret; } EXPORT_SERVER_CMD_HANDLER(help); diff --git a/lsu.c b/lsu.c index 8ccf80d5..d1c4a4cb 100644 --- a/lsu.c +++ b/lsu.c @@ -11,20 +11,16 @@ #include "lsu.h" #include "fd.h" -static int lsu_lopsub_error(int ret, char **errctx, char **result, unsigned *num_chars) +static void lsu_error(const char *etype, char **errctx, char **result, + unsigned *num_chars) { - const char *se = para_strerror(-ret); - unsigned n; - - if (*errctx) - n = xasprintf(result, "%s: %s\n", *errctx, se); - else - n = xasprintf(result, "lopsub error: %s\n", se); + /* *errctx is NULL if the lopsub function failed to allocate memory. */ + unsigned n = xasprintf(result, "lopsub %s error: %s\n", etype, + *errctx? *errctx : "out of memory"); free(*errctx); *errctx = NULL; if (num_chars) *num_chars = n; - return ret; } static void lsu_get_subcommand_summary(bool long_summary, @@ -116,8 +112,10 @@ int lsu_com_help(bool long_help, const struct lls_parse_result *lpr, const struct lls_command *cmd; ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx)); - if (ret < 0) - return lsu_lopsub_error(ret, &errctx, result, num_chars); + if (ret < 0) { + lsu_error("arg count", &errctx, result, num_chars); + return ret; + } if (lls_num_inputs(lpr) == 0) { lsu_get_subcommand_summary(long_help, suite, aux_info_cb, result, num_chars); @@ -125,8 +123,10 @@ int lsu_com_help(bool long_help, const struct lls_parse_result *lpr, } arg = lls_input(0, lpr); ret = lls(lls_lookup_subcmd(arg, suite, &errctx)); - if (ret < 0) - return lsu_lopsub_error(ret, &errctx, result, num_chars); + if (ret < 0) { + lsu_error("lookup", &errctx, result, num_chars); + return ret; + } cmd = lls_cmd(ret, suite); tmp = long_help? lls_long_help(cmd) : lls_short_help(cmd); if (aux_info_cb) diff --git a/play.c b/play.c index 596b4b6d..359eb371 100644 --- a/play.c +++ b/play.c @@ -708,7 +708,7 @@ EXPORT_PLAY_CMD_HANDLER(quit); static int com_help(struct lls_parse_result *lpr) { - int i; + int i, ret; char *buf; size_t sz; unsigned n; @@ -731,9 +731,13 @@ static int com_help(struct lls_parse_result *lpr) } return 0; } - lsu_com_help(long_help, lpr, play_cmd_suite, NULL, &buf, &n); - btr_add_output(buf, n, pt->btrn); - return 0; + ret = lsu_com_help(long_help, lpr, play_cmd_suite, NULL, &buf, &n); + if (ret < 0) { + PARA_WARNING_LOG("%s", buf); /* no \n needed */ + free(buf); + } else + btr_add_output(buf, n, pt->btrn); + return ret; } EXPORT_PLAY_CMD_HANDLER(help); -- 2.39.5