]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Address and add tests for edge cases involving short or empty files
authorzriback <zacharythecdr@gmail.com>
Fri, 31 Jan 2025 02:05:32 +0000 (21:05 -0500)
committerViktor Dukhovni <openssl-users@dukhovni.org>
Tue, 29 Apr 2025 04:17:06 +0000 (14:17 +1000)
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26549)

apps/prime.c
test/recipes/20-test_prime.t
test/recipes/20-test_prime_data/empty.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/short_number.txt [new file with mode: 0644]

index 4c103e975fc5ba5477700d9b1e8bc94ef705c031..92c04569a643b391f69b95f0a5c6ee5ef8a6d4fe 100644 (file)
@@ -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;
index 3d22ae3ffa4e1df5d80e83f12d4d52b772f644ec..16e69eb6526c739208e57e26ca428f64159711e1 100644 (file)
@@ -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 (file)
index 0000000..e69de29
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 (file)
index 0000000..b8626c4
--- /dev/null
@@ -0,0 +1 @@
+4