From 61afdd35ffc9478fe9cba2d0a3a575ec9004136b Mon Sep 17 00:00:00 2001 From: yixiangzhike Date: Fri, 13 Jan 2023 17:21:40 +0800 Subject: Fix an issue that all words are deleted when there is no valid input data Fix an issue that dict words in the file pw_dict are deleted when create-cracklib-dict or cracklib-packer has no valid input data. For example: Give an empty file or a file that does't exist or a file has no valid input words to create-cracklib-dict, all words in pw_dict are deleted. $ cracklib-create-dict test Run cracklib-packer and press Enter, all words in pw_dict are deleted. $ cracklib-packer --- src/util/packer.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/util/packer.c b/src/util/packer.c index e3f9500..be6fbf5 100644 --- a/src/util/packer.c +++ b/src/util/packer.c @@ -22,6 +22,7 @@ main(argc, argv) PWDICT *pwp; char buffer[STRINGSIZE], prev[STRINGSIZE]; char *file; + char opened = 0; if (argc <= 1) { @@ -39,12 +40,6 @@ main(argc, argv) return (-1); } - if (!(pwp = PWOpen(file, "w"))) - { - perror(file); - return (-1); - } - wrote = 0; prev[0] = '\0'; @@ -62,6 +57,16 @@ main(argc, argv) continue; } + if (!opened) + { + if (!(pwp = PWOpen(file, "w"))) + { + perror(file); + return (-1); + } + opened = 1; + } + /* * If this happens, strcmp() in FindPW() in packlib.c will be unhappy. */ @@ -79,7 +84,8 @@ main(argc, argv) wrote++; } - PWClose(pwp); + if (opened) + PWClose(pwp); printf("%lu %lu\n", readed, wrote); -- cgit v1.2.1