Handle --encrypt -- without crashing

This commit is contained in:
Jay Berkenbilt 2023-12-22 20:09:31 -05:00
parent a2c4a2b12a
commit 1f45686843
3 changed files with 10 additions and 1 deletions

View File

@ -163,6 +163,7 @@ ArgParser::argShowCrypto()
void
ArgParser::argEncrypt()
{
this->c_enc = c_main->encrypt(0, "", "");
this->accumulated_args.clear();
this->ap.selectOptionTable(O_ENCRYPTION);
}

View File

@ -1042,6 +1042,9 @@ QPDFJob::EncConfig::EncConfig(Config* c) :
QPDFJob::Config*
QPDFJob::EncConfig::endEncrypt()
{
if (config->o.m->keylen == 0) {
usage("encryption key length is required");
}
config->o.m->encrypt = true;
config->o.m->decrypt = false;
config->o.m->copy_encryption = false;

View File

@ -15,7 +15,7 @@ cleanup();
my $td = new TestDriver('arg-parsing');
my $n_tests = 21;
my $n_tests = 22;
$td->runtest("required argument",
{$td->COMMAND => "qpdf --password minimal.pdf"},
@ -107,6 +107,11 @@ $td->runtest("empty and replace-input",
{$td->REGEXP => ".*--replace-input may not be used with --empty.*",
$td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES);
$td->runtest("missing key length",
{$td->COMMAND => "qpdf --encrypt --"},
{$td->REGEXP => ".*encryption key length is required",
$td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES);
# Disallow mixing positional and flag-style encryption arguments.
my @bad_enc = (