Bug 1048070, Part 1: Replace uses of PR_ASSERT in mozilla::pkix, r=keeler
authorBrian Smith <brian@briansmith.org>
Sat, 02 Aug 2014 09:17:59 -0700
changeset 14645 7e8a72fe2994ad818628ec5d28d5ea251fc92ad7
parent 14644 75a4cfe83b66d47bd2f9f9d8a55a55364f1718c0
child 14646 bf2a551e8306de14b8426bbdf4354f191c130ca0
push id3202
push userfranziskuskiefer@gmail.com
push dateMon, 01 Oct 2018 08:30:12 +0000
reviewerskeeler
bugs1048070
Bug 1048070, Part 1: Replace uses of PR_ASSERT in mozilla::pkix, r=keeler
lib/mozpkix/lib/pkixbuild.cpp
lib/mozpkix/lib/pkixcheck.cpp
lib/mozpkix/lib/pkixder.cpp
lib/mozpkix/lib/pkixder.h
lib/mozpkix/lib/pkixocsp.cpp
lib/mozpkix/lib/pkixutil.h
lib/mozpkix/test/gtest/pkixocsp_VerifyEncodedOCSPResponse.cpp
lib/mozpkix/test/lib/pkixtestutil.cpp
--- a/lib/mozpkix/lib/pkixbuild.cpp
+++ b/lib/mozpkix/lib/pkixbuild.cpp
@@ -257,17 +257,17 @@ BuildForward(TrustDomain& trustDomain,
     static_assert(1/*end-entity*/ + MAX_SUBCA_COUNT + 1/*root*/ ==
                   NonOwningDERArray::MAX_LENGTH,
                   "MAX_SUBCA_COUNT and NonOwningDERArray::MAX_LENGTH mismatch.");
     if (subCACount >= MAX_SUBCA_COUNT) {
       return Result::ERROR_UNKNOWN_ISSUER;
     }
     ++subCACount;
   } else {
-    PR_ASSERT(subCACount == 0);
+    assert(subCACount == 0);
   }
 
   // Find a trusted issuer.
 
   PathBuildingStep pathBuilder(trustDomain, subject, time,
                                requiredEKUIfPresent, requiredPolicy,
                                stapledOCSPResponse, subCACount);
 
--- a/lib/mozpkix/lib/pkixcheck.cpp
+++ b/lib/mozpkix/lib/pkixcheck.cpp
@@ -58,17 +58,17 @@ CheckValidity(Input encodedValidity, Tim
 }
 
 // 4.2.1.3. Key Usage (id-ce-keyUsage)
 
 // As explained in the comment in CheckKeyUsage, bit 0 is the most significant
 // bit and bit 7 is the least significant bit.
 inline uint8_t KeyUsageToBitMask(KeyUsage keyUsage)
 {
-  PR_ASSERT(keyUsage != KeyUsage::noParticularKeyUsageRequired);
+  assert(keyUsage != KeyUsage::noParticularKeyUsageRequired);
   return 0x80u >> static_cast<uint8_t>(keyUsage);
 }
 
 Result
 CheckKeyUsage(EndEntityOrCA endEntityOrCA, const Input* encodedKeyUsage,
               KeyUsage requiredKeyUsageIfPresent)
 {
   if (!encodedKeyUsage) {
--- a/lib/mozpkix/lib/pkixder.cpp
+++ b/lib/mozpkix/lib/pkixder.cpp
@@ -30,17 +30,17 @@
 namespace mozilla { namespace pkix { namespace der {
 
 namespace internal {
 
 // Too complicated to be inline
 Result
 ExpectTagAndGetLength(Reader& input, uint8_t expectedTag, uint16_t& length)
 {
-  PR_ASSERT((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
+  assert((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
 
   uint8_t tag;
   Result rv;
   rv = input.Read(tag);
   if (rv != Success) {
     return rv;
   }
 
--- a/lib/mozpkix/lib/pkixder.h
+++ b/lib/mozpkix/lib/pkixder.h
@@ -32,16 +32,18 @@
 // Match* functions advance the input mark and return true if the input matches
 // the given criteria; they return false without changing the input mark if the
 // input does not match the criteria.
 //
 // Skip* functions unconditionally advance the input mark and return Success if
 // they are able to do so; otherwise they fail with the input mark in an
 // undefined state.
 
+#include <cassert>
+
 #include "pkix/Input.h"
 #include "pkix/pkixtypes.h"
 
 namespace mozilla { namespace pkix { namespace der {
 
 enum Class
 {
    UNIVERSAL = 0 << 6,
@@ -69,18 +71,18 @@ enum Tag
   GENERALIZED_TIME = UNIVERSAL | 0x18,
 };
 
 MOZILLA_PKIX_ENUM_CLASS EmptyAllowed { No = 0, Yes = 1 };
 
 inline Result
 ExpectTagAndLength(Reader& input, uint8_t expectedTag, uint8_t expectedLength)
 {
-  PR_ASSERT((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
-  PR_ASSERT(expectedLength < 128); // must be a single-byte length
+  assert((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
+  assert(expectedLength < 128); // must be a single-byte length
 
   uint16_t tagAndLength;
   Result rv = input.Read(tagAndLength);
   if (rv != Success) {
     return rv;
   }
 
   uint16_t expectedTagAndLength = static_cast<uint16_t>(expectedTag << 8);
--- a/lib/mozpkix/lib/pkixocsp.cpp
+++ b/lib/mozpkix/lib/pkixocsp.cpp
@@ -947,14 +947,14 @@ CreateEncodedOCSPRequest(TrustDomain& tr
   do {
     rv = serialNumber.Read(*d);
     if (rv != Success) {
       return rv;
     }
     ++d;
   } while (!serialNumber.AtEnd());
 
-  PR_ASSERT(d == out + totalLen);
+  assert(d == out + totalLen);
 
   return Success;
 }
 
 } } // namespace mozilla::pkix
--- a/lib/mozpkix/lib/pkixutil.h
+++ b/lib/mozpkix/lib/pkixutil.h
@@ -180,18 +180,18 @@ private:
 
   NonOwningDERArray(const NonOwningDERArray&) /* = delete*/;
   void operator=(const NonOwningDERArray&) /* = delete*/;
 };
 
 inline unsigned int
 DaysBeforeYear(unsigned int year)
 {
-  PR_ASSERT(year >= 1);
-  PR_ASSERT(year <= 9999);
+  assert(year >= 1);
+  assert(year <= 9999);
   return ((year - 1u) * 365u)
        + ((year - 1u) / 4u)    // leap years are every 4 years,
        - ((year - 1u) / 100u)  // except years divisible by 100,
        + ((year - 1u) / 400u); // except years divisible by 400.
 }
 
 } } // namespace mozilla::pkix
 
--- a/lib/mozpkix/test/gtest/pkixocsp_VerifyEncodedOCSPResponse.cpp
+++ b/lib/mozpkix/test/gtest/pkixocsp_VerifyEncodedOCSPResponse.cpp
@@ -387,17 +387,17 @@ protected:
   // signerDEROut is owned by the arena
   Input CreateEncodedIndirectOCSPSuccessfulResponse(
               const char* certSubjectName,
               OCSPResponseContext::CertStatus certStatus,
               const char* signerName,
               SECOidTag signerEKU = SEC_OID_OCSP_RESPONDER,
               /*optional, out*/ Input* signerDEROut = nullptr)
   {
-    PR_ASSERT(certSubjectName);
+    assert(certSubjectName);
 
     const SECItem* extensions[] = {
       signerEKU != SEC_OID_UNKNOWN
         ? CreateEncodedEKUExtension(arena.get(), &signerEKU, 1,
                                     ExtensionCriticality::NotCritical)
         : nullptr,
       nullptr
     };
--- a/lib/mozpkix/test/lib/pkixtestutil.cpp
+++ b/lib/mozpkix/test/lib/pkixtestutil.cpp
@@ -54,20 +54,20 @@ deleteCharArray(char* chars)
   delete[] chars;
 }
 
 } // unnamed namespace
 
 FILE*
 OpenFile(const char* dir, const char* filename, const char* mode)
 {
-  PR_ASSERT(dir);
-  PR_ASSERT(*dir);
-  PR_ASSERT(filename);
-  PR_ASSERT(*filename);
+  assert(dir);
+  assert(*dir);
+  assert(filename);
+  assert(*filename);
 
   ScopedPtr<char, deleteCharArray>
     path(new (nothrow) char[strlen(dir) + 1 + strlen(filename) + 1]);
   if (!path) {
     PR_SetError(SEC_ERROR_NO_MEMORY, 0);
     return nullptr;
   }
   strcpy(path.get(), dir);
@@ -166,35 +166,35 @@ public:
     , length(0)
   {
   }
 
   // Makes a shallow copy of the input item. All input items must have a
   // lifetime that extends at least to where Squash is called.
   Result Add(const SECItem* item)
   {
-    PR_ASSERT(item);
-    PR_ASSERT(item->data);
+    assert(item);
+    assert(item->data);
 
     if (numItems >= MaxSequenceItems) {
       return Result::FATAL_ERROR_INVALID_ARGS;
     }
     if (length + item->len > 65535) {
       return Result::FATAL_ERROR_INVALID_ARGS;
     }
 
     contents[numItems] = item;
     numItems++;
     length += item->len;
     return Success;
   }
 
   SECItem* Squash(PLArenaPool* arena, uint8_t tag)
   {
-    PR_ASSERT(arena);
+    assert(arena);
 
     size_t lengthLength = length < 128 ? 1
                         : length < 256 ? 2
                                        : 3;
     size_t totalLength = 1 + lengthLength + length;
     SECItem* output = SECITEM_AllocItem(arena, nullptr, totalLength);
     if (!output) {
       return nullptr;
@@ -362,26 +362,26 @@ BitString(PLArenaPool* arena, const SECI
   // resulting in unused bits in the bitstring encoding
   SECItem* prefixed = SECITEM_AllocItem(arena, nullptr, rawBytes->len + 1);
   if (!prefixed) {
     return nullptr;
   }
   prefixed->data[0] = 0;
   memcpy(prefixed->data + 1, rawBytes->data, rawBytes->len);
   if (corrupt) {
-    PR_ASSERT(prefixed->len > 8);
+    assert(prefixed->len > 8);
     prefixed->data[8]++;
   }
   return EncodeNested(arena, der::BIT_STRING, prefixed);
 }
 
 static SECItem*
 Boolean(PLArenaPool* arena, bool value)
 {
-  PR_ASSERT(arena);
+  assert(arena);
   SECItem* result(SECITEM_AllocItem(arena, nullptr, 3));
   if (!result) {
     return nullptr;
   }
   result->data[0] = der::BOOLEAN;
   result->data[1] = 1; // length
   result->data[2] = value ? 0xff : 0x00;
   return result;
@@ -419,17 +419,17 @@ OID(PLArenaPool* arena, SECOidTag tag)
 enum TimeEncoding { UTCTime = 0, GeneralizedTime = 1 };
 
 // http://tools.ietf.org/html/rfc5280#section-4.1.2.5
 // UTCTime:           YYMMDDHHMMSSZ (years 1950-2049 only)
 // GeneralizedTime: YYYYMMDDHHMMSSZ
 static SECItem*
 PRTimeToEncodedTime(PLArenaPool* arena, PRTime time, TimeEncoding encoding)
 {
-  PR_ASSERT(encoding == UTCTime || encoding == GeneralizedTime);
+  assert(encoding == UTCTime || encoding == GeneralizedTime);
 
   PRExplodedTime exploded;
   PR_ExplodeTime(time, PR_GMTParameters, &exploded);
   if (exploded.tm_sec >= 60) {
     // round down for leap seconds
     exploded.tm_sec = 59;
   }
 
@@ -513,19 +513,19 @@ YMDHMS(int16_t year, int16_t month, int1
                                    Time::ONE_DAY_IN_SECONDS));
 }
 
 static SECItem*
 SignedData(PLArenaPool* arena, const SECItem* tbsData,
            SECKEYPrivateKey* privKey, SECOidTag hashAlg,
            bool corrupt, /*optional*/ SECItem const* const* certs)
 {
-  PR_ASSERT(arena);
-  PR_ASSERT(tbsData);
-  PR_ASSERT(privKey);
+  assert(arena);
+  assert(tbsData);
+  assert(privKey);
   if (!arena || !tbsData || !privKey) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   SECOidTag signatureAlgTag = SEC_GetSignatureAlgorithmOidTag(privKey->keyType,
                                                               hashAlg);
   if (signatureAlgTag == SEC_OID_UNKNOWN) {
@@ -596,17 +596,17 @@ SignedData(PLArenaPool* arena, const SEC
 //                  -- contains the DER encoding of an ASN.1 value
 //                  -- corresponding to the extension type identified
 //                  -- by extnID
 //      }
 static SECItem*
 Extension(PLArenaPool* arena, SECOidTag extnIDTag,
           ExtensionCriticality criticality, Output& value)
 {
-  PR_ASSERT(arena);
+  assert(arena);
   if (!arena) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   Output output;
 
   const SECItem* extnID(OID(arena, extnIDTag));
@@ -637,17 +637,17 @@ Extension(PLArenaPool* arena, SECOidTag 
   }
 
   return output.Squash(arena, der::SEQUENCE);
 }
 
 SECItem*
 MaybeLogOutput(SECItem* result, const char* suffix)
 {
-  PR_ASSERT(suffix);
+  assert(suffix);
 
   if (!result) {
     return nullptr;
   }
 
   // This allows us to more easily debug the generated output, by creating a
   // file in the directory given by MOZILLA_PKIX_TEST_LOG_DIR for each
   // NOT THREAD-SAFE!!!
@@ -688,21 +688,21 @@ GenerateKeyPair(/*out*/ ScopedSECKEYPubl
     params.keySizeInBits = 2048;
     params.pe = 3;
     SECKEYPublicKey* publicKeyTemp = nullptr;
     privateKey = PK11_GenerateKeyPair(slot.get(), CKM_RSA_PKCS_KEY_PAIR_GEN,
                                       &params, &publicKeyTemp, false, true,
                                       nullptr);
     if (privateKey) {
       publicKey = publicKeyTemp;
-      PR_ASSERT(publicKey);
+      assert(publicKey);
       return SECSuccess;
     }
 
-    PR_ASSERT(!publicKeyTemp);
+    assert(!publicKeyTemp);
 
     if (PR_GetError() != SEC_ERROR_PKCS11_FUNCTION_FAILED) {
       return SECFailure;
     }
 
     PRTime now = PR_Now();
     if (PK11_RandomUpdate(&now, sizeof(PRTime)) != SECSuccess) {
       return SECFailure;
@@ -731,19 +731,19 @@ CreateEncodedCertificate(PLArenaPool* ar
                          SECOidTag signature, const SECItem* serialNumber,
                          const SECItem* issuerNameDER, PRTime notBefore,
                          PRTime notAfter, const SECItem* subjectNameDER,
                          /*optional*/ SECItem const* const* extensions,
                          /*optional*/ SECKEYPrivateKey* issuerPrivateKey,
                          SECOidTag signatureHashAlg,
                          /*out*/ ScopedSECKEYPrivateKey& privateKeyResult)
 {
-  PR_ASSERT(arena);
-  PR_ASSERT(issuerNameDER);
-  PR_ASSERT(subjectNameDER);
+  assert(arena);
+  assert(issuerNameDER);
+  assert(subjectNameDER);
   if (!arena || !issuerNameDER || !subjectNameDER) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   // It may be the case that privateKeyResult refers to the
   // ScopedSECKEYPrivateKey that owns issuerPrivateKey; thus, we can't set
   // privateKeyResult until after we're done with issuerPrivateKey.
@@ -791,20 +791,20 @@ CreateEncodedCertificate(PLArenaPool* ar
 static SECItem*
 TBSCertificate(PLArenaPool* arena, long versionValue,
                const SECItem* serialNumber, SECOidTag signatureOidTag,
                const SECItem* issuer, PRTime notBeforeTime,
                PRTime notAfterTime, const SECItem* subject,
                const SECKEYPublicKey* subjectPublicKey,
                /*optional*/ SECItem const* const* extensions)
 {
-  PR_ASSERT(arena);
-  PR_ASSERT(issuer);
-  PR_ASSERT(subject);
-  PR_ASSERT(subjectPublicKey);
+  assert(arena);
+  assert(issuer);
+  assert(subject);
+  assert(subjectPublicKey);
   if (!arena || !issuer || !subject || !subjectPublicKey) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   Output output;
 
   if (versionValue != static_cast<long>(der::Version::v1)) {
@@ -930,17 +930,17 @@ CreateEncodedSerialNumber(PLArenaPool* a
 // BasicConstraints ::= SEQUENCE {
 //         cA                      BOOLEAN DEFAULT FALSE,
 //         pathLenConstraint       INTEGER (0..MAX) OPTIONAL }
 SECItem*
 CreateEncodedBasicConstraints(PLArenaPool* arena, bool isCA,
                               /*optional*/ long* pathLenConstraintValue,
                               ExtensionCriticality criticality)
 {
-  PR_ASSERT(arena);
+  assert(arena);
   if (!arena) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   Output value;
 
   if (isCA) {
@@ -963,18 +963,18 @@ CreateEncodedBasicConstraints(PLArenaPoo
 }
 
 // ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
 // KeyPurposeId ::= OBJECT IDENTIFIER
 SECItem*
 CreateEncodedEKUExtension(PLArenaPool* arena, SECOidTag const* ekus,
                           size_t ekusCount, ExtensionCriticality criticality)
 {
-  PR_ASSERT(arena);
-  PR_ASSERT(ekus);
+  assert(arena);
+  assert(ekus);
   if (!arena || (!ekus && ekusCount != 0)) {
     PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
     return nullptr;
   }
 
   Output value;
   for (size_t i = 0; i < ekusCount; ++i) {
     SECItem* encodedEKUOID = OID(arena, ekus[i]);