From c142bb06258c90f07f3b0118aeb21db507b0d628 Mon Sep 17 00:00:00 2001 From: Holger Dengler Date: Thu, 16 Feb 2023 10:16:19 +0100 Subject: [PATCH] lszcrypt: use separate index for inner sub-device loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function show_devices_argv() iterates over all remaining arguments. Each argument may specify a single device or a list of sub-devices. Use a separate index for the inner sub-device loop. This fixes a bug, that not all or wrong arguments are processed. Also terminate the outer arguments loop based on the number of remaining arguments. Signed-off-by: Holger Dengler Reviewed-by: Harald Freudenberger Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- zconf/zcrypt/lszcrypt.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/zconf/zcrypt/lszcrypt.c b/zconf/zcrypt/lszcrypt.c index 717dde75..43a3c39b 100644 --- a/zconf/zcrypt/lszcrypt.c +++ b/zconf/zcrypt/lszcrypt.c @@ -853,12 +853,12 @@ static void show_devices_all(void) /* * Show devices specified on commandline */ -static void show_devices_argv(char *argv[]) +static void show_devices_argv(int argc, char **argv) { + int id, dom, argidx, devidx, n, dev_cnt, sub_cnt; struct util_rec *rec = util_rec_new_wide("-"); - struct dirent **dev_vec, **subdev_vec; char *ap, *grp_dev, *path, *card, *sub_dev; - int id, dom, i, n, dev_cnt, sub_cnt; + struct dirent **dev_vec, **subdev_vec; /* check if ap driver is available */ ap = util_path_sysfs("bus/ap"); @@ -870,10 +870,10 @@ static void show_devices_argv(char *argv[]) define_rec_verbose(rec); util_rec_print_hdr(rec); - for (i = 0; argv[i] != NULL; i++) { + for (argidx = 0; argidx < argc; argidx++) { id = -1; dom = -1; - if (sscanf(argv[i], "%x.%x", &id, &dom) >= 1) { + if (sscanf(argv[argidx], "%x.%x", &id, &dom) >= 1) { /* at least the id field was valid */ if (id >= 0 && dom >= 0) { /* single subdevice */ util_asprintf(&sub_dev, "%02x.%04x", id, dom); @@ -889,7 +889,7 @@ static void show_devices_argv(char *argv[]) } continue; } - if (sscanf(argv[i]+1, "%x", &dom) == 1) { + if (sscanf(argv[argidx] + 1, "%x", &dom) == 1) { /* list specific domains of all adapters */ path = util_path_sysfs("devices/ap/"); dev_cnt = util_scandir(&dev_vec, alphasort, path, @@ -897,9 +897,9 @@ static void show_devices_argv(char *argv[]) if (dev_cnt < 1) errx(EXIT_FAILURE, "No crypto card devices found."); free(path); - for (i = 0; i < dev_cnt; i++) { + for (devidx = 0; devidx < dev_cnt; devidx++) { path = util_path_sysfs("devices/ap/%s", - dev_vec[i]->d_name); + dev_vec[devidx]->d_name); sub_cnt = util_scandir(&subdev_vec, alphasort, path, "[0-9a-fA-F]+.%04x", @@ -1023,6 +1023,6 @@ int main(int argc, char **argv) if (optind == argc) show_devices_all(); else - show_devices_argv(&argv[optind]); + show_devices_argv((argc - optind), &argv[optind]); return EXIT_SUCCESS; }