- /* If a CRL, update the last file suffix added for this */
-
- if (type == X509_LU_CRL) {
- CRYPTO_THREAD_write_lock(ctx->lock);
+ /*
+ * If a CRL, update the last file suffix added for this.
+ * We don't need to add an entry if k is 0 as this is the initial value.
+ * This avoids the need for a write lock and sort operation in the
+ * simple case where no CRL is present for a hash.
+ */
+ if (type == X509_LU_CRL && k > 0) {
+ if (!CRYPTO_THREAD_write_lock(ctx->lock))
+ goto finish;