From 4636c7a90e91633d0196860f5f5531a633f782b7 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 6 Feb 2019 18:49:06 +0100 Subject: [PATCH] Improve diagnostics of lls_check_arg_count(). The old code could return an error message like at least 1 non-option args required, 0 given even if exactly 1 arg is required. So check this case and print exactly 1 non-option args required, 0 given instead. The check for the upper bound has the same issue. It is considered a bug in the application if min_argc > max_argc, so check for this case as well. --- lopsub.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lopsub.c b/lopsub.c index 44abc93..d495663 100644 --- a/lopsub.c +++ b/lopsub.c @@ -724,9 +724,11 @@ int lls_check_arg_count(const struct lls_parse_result *lpr, { if (errctx) *errctx = NULL; + assert(min_argc <= max_argc); if (lpr->num_inputs < min_argc) { - xasprintf(errctx, "at least %u non-option args required, " - "%u given", min_argc, lpr->num_inputs); + xasprintf(errctx, "%s %u non-option args required, %u given", + min_argc < max_argc? "at least" : "exactly", + min_argc, lpr->num_inputs); return -E_LLS_BAD_ARG_COUNT; } if (lpr->num_inputs > max_argc) { @@ -734,8 +736,10 @@ int lls_check_arg_count(const struct lls_parse_result *lpr, xasprintf(errctx, "no non-option args allowed, " "%u given", lpr->num_inputs); else - xasprintf(errctx, "at most %u non-option args allowed, " - "%u given", max_argc, lpr->num_inputs); + xasprintf(errctx, "%s %u non-option args allowed, " + "%u given", min_argc < max_argc? + "at most" : "exactly", + max_argc, lpr->num_inputs); return -E_LLS_BAD_ARG_COUNT; } return 1; -- 2.39.2