]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Fix potential divide by zero error
authorNeil Horman <nhorman@openssl.org>
Fri, 2 Feb 2024 13:10:32 +0000 (08:10 -0500)
committerTomas Mraz <tomas@openssl.org>
Mon, 6 May 2024 14:17:23 +0000 (16:17 +0200)
Coverity caught the following issues:
1591477
1591475
1591473
1591470

all of which are simmilar, in that they catch potential divide by zero
in double values.  It can't actually happen since the the threads which
increment these counters don't exit until they reach non-zero values,
but its easy to add the checks, so lets do that to ensure that we don't
change something in the future that causes it.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/24337)

test/threadstest.c

index 36101b72bfdd0c576c595b0908e302e91e841bd6..5b6565a45f74f871ae884cdf63e594e5e23da98d 100644 (file)
@@ -263,6 +263,11 @@ static int _torture_rw(void)
     TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
               rwreader1_iterations + rwreader2_iterations,
               rwwriter1_iterations + rwwriter2_iterations, tottime);
+    if ((rwreader1_iterations + rwreader2_iterations == 0)
+        || (rwwriter1_iterations + rwwriter2_iterations == 0)) {
+        TEST_info("Threads did not iterate\n");
+        goto out;
+    }
     avr = tottime / (rwreader1_iterations + rwreader2_iterations);
     avw = (tottime / (rwwriter1_iterations + rwwriter2_iterations));
     TEST_info("Average read time %e/read", avr);
@@ -411,6 +416,7 @@ static int _torture_rcu(void)
     struct timeval dtime;
     double tottime;
     double avr, avw;
+    int rc = 0;
 
     atomiclock = CRYPTO_THREAD_lock_new();
     if (!TEST_ptr(atomiclock))
@@ -443,7 +449,7 @@ static int _torture_rcu(void)
         || !TEST_true(wait_for_thread(writer2))
         || !TEST_true(wait_for_thread(reader1))
         || !TEST_true(wait_for_thread(reader2)))
-        return 0;
+        goto out;
 
     t2 = ossl_time_now();
     dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
@@ -452,17 +458,27 @@ static int _torture_rcu(void)
     TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
               reader1_iterations + reader2_iterations,
               writer1_iterations + writer2_iterations, tottime);
+    if ((reader1_iterations + reader2_iterations == 0)
+        || (writer1_iterations + writer2_iterations == 0)) {
+        TEST_info("Threads did not iterate\n");
+        goto out;
+    }
     avr = tottime / (reader1_iterations + reader2_iterations);
     avw = tottime / (writer1_iterations + writer2_iterations);
     TEST_info("Average read time %e/read", avr);
     TEST_info("Average write time %e/write", avw);
 
+    if (!TEST_int_eq(rcu_torture_result, 1))
+        goto out;
+
+    rc = 1;
+out:
     ossl_rcu_lock_free(rcu_lock);
     CRYPTO_THREAD_lock_free(atomiclock);
     if (!TEST_int_eq(rcu_torture_result, 1))
         return 0;
 
-    return 1;
+    return rc;
 }
 
 static int torture_rcu_low(void)