]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Check return value of lsu_com_help().
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 18 Mar 2025 18:31:33 +0000 (19:31 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sat, 3 May 2025 16:45:49 +0000 (18:45 +0200)
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
command.c
lsu.c
play.c

index 5f0b35a5dc0f3e4f02d134faa2ef828c6b357763..a7b65b1aa024f80c91866ea1f0c7b6a6e353765e 100644 (file)
@@ -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)
 
index 78c9106c9ff84062e874b326a386f7073e09aeca..5cdeb7b4dc82533d4752c1320a76c89d5acc17b3 100644 (file)
--- 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 8ccf80d541929194f984759e38d7ffa7998d3c97..d1c4a4cbe88928bac51c61a993ecf3071e1eb70e 100644 (file)
--- a/lsu.c
+++ b/lsu.c
 #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 596b4b6dd70402dcd261061d8b39f97e7b722ac8..359eb371bdcb721c77fb9a90eaab2472b904a211 100644 (file)
--- 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);