From: zriback Date: Fri, 31 Jan 2025 02:05:32 +0000 (-0500) Subject: Address and add tests for edge cases involving short or empty files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=585a1e6f8ba5fcf2c17bf800744b2db4be40d0b0;p=thirdparty%2Fopenssl.git Address and add tests for edge cases involving short or empty files Reviewed-by: Paul Dale Reviewed-by: Viktor Dukhovni (Merged from https://github.com/openssl/openssl/pull/26549) --- diff --git a/apps/prime.c b/apps/prime.c index 4c103e975fc..92c04569a64 100644 --- a/apps/prime.c +++ b/apps/prime.c @@ -172,10 +172,16 @@ opthelp: file_read_buf = (char *)realloc(file_read_buf, BUFSIZE + total_read); } + /* Deal with the case of an empty file */ + if (total_read == 0) { + BIO_printf(bio_err, "Cannot process empty file\n"); + goto end; + } + /* Deal with Unix and Windows line endings */ - if (file_read_buf[total_read - 2] == '\r') + if (total_read >= 2 && file_read_buf[total_read - 2] == '\r') file_read_buf[total_read - 2] = '\0'; - else if (file_read_buf[total_read - 1] == '\n') + else if (total_read >= 1 && file_read_buf[total_read - 1] == '\n') file_read_buf[total_read - 1] = '\0'; check_val = file_read_buf; diff --git a/test/recipes/20-test_prime.t b/test/recipes/20-test_prime.t index 3d22ae3ffa4..16e69eb6526 100644 --- a/test/recipes/20-test_prime.t +++ b/test/recipes/20-test_prime.t @@ -14,14 +14,17 @@ use OpenSSL::Test; setup("test_prime"); -plan tests => 8; +plan tests => 10; my $prime_file = data_file("prime.txt"); my $composite_file = data_file("composite.txt"); my $long_number_file = data_file("long_number.txt"); +my $short_number_file = data_file("short_number.txt"); my $non_number_file = data_file("non_number.txt"); my $hex_number_file = data_file("hex_number.txt"); my $bad_format_file = data_file("bad_format.txt"); +my $empty_file = data_file("empty.txt"); + ok(run(app(["openssl", "prime", "-in", $prime_file])), "Run openssl prime with prime number -in file"); @@ -32,6 +35,9 @@ ok(run(app(["openssl", "prime", "-in", $composite_file])), ok(run(app(["openssl", "prime", "-in", $long_number_file])), "Run openssl prime with long number -in file"); +ok(run(app(["openssl", "prime", "-in", $short_number_file])), + "Run openssl prime with a short number -in file"); + ok(!run(app(["openssl", "prime", "-in", $non_number_file])), "Run openssl prime with non number -in file"); @@ -41,6 +47,9 @@ ok(run(app(["openssl", "prime", "-in", "-hex", $hex_number_file])), ok(!run(app(["openssl", "prime", "-in", $bad_format_file])), "Run openssl prime with bad format -in file"); +ok(!run(app(["openssl", "prime", "-in", $empty_file])), + "Run openssl prime with an empty -in file"); + ok(run(app(["openssl", "prime", "-in", $prime_file, $composite_file, $long_number_file])), "Run openssl prime with multiple -in files"); diff --git a/test/recipes/20-test_prime_data/empty.txt b/test/recipes/20-test_prime_data/empty.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/recipes/20-test_prime_data/short_number.txt b/test/recipes/20-test_prime_data/short_number.txt new file mode 100644 index 00000000000..b8626c4cff2 --- /dev/null +++ b/test/recipes/20-test_prime_data/short_number.txt @@ -0,0 +1 @@ +4