Bug 1048070, Part 2: Remove uses of PR_NOT_REACHED and PR_ARRAY_SIZE in mozilla::pkix, r=keeeler
authorBrian Smith <brian@briansmith.org>
Sun, 03 Aug 2014 18:24:35 -0700
changeset 198865 d7ddaaa69b5a2c6622bc20deff8a0d317eb0b19c
parent 198864 54c668e916d39e5a9e3cbe5fc34f86d643eb4434
child 198866 36814bee62770f77330d8bf7c5e674bc8170cfc0
push id47515
push userbrian@briansmith.org
push dateMon, 11 Aug 2014 16:36:18 +0000
treeherdermozilla-inbound@d7ddaaa69b5a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeeler
bugs1048070
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1048070, Part 2: Remove uses of PR_NOT_REACHED and PR_ARRAY_SIZE in mozilla::pkix, r=keeeler
security/pkix/include/pkix/Input.h
security/pkix/include/pkix/Result.h
security/pkix/lib/pkixbuild.cpp
security/pkix/lib/pkixcheck.cpp
security/pkix/lib/pkixder.cpp
security/pkix/lib/pkixder.h
security/pkix/lib/pkixocsp.cpp
security/pkix/test/gtest/pkixder_input_tests.cpp
--- a/security/pkix/include/pkix/Input.h
+++ b/security/pkix/include/pkix/Input.h
@@ -24,17 +24,16 @@
 
 #ifndef mozilla_pkix__Input_h
 #define mozilla_pkix__Input_h
 
 #include <cstring>
 
 #include "pkix/nullptr.h"
 #include "pkix/Result.h"
-#include "prlog.h"
 #include "stdint.h"
 
 namespace mozilla { namespace pkix {
 
 class Reader;
 
 // An Input is a safety-oriented immutable weak reference to a array of bytes
 // of a known size. The data can only be legally accessed by constructing a
@@ -278,18 +277,17 @@ public:
     void operator=(const Mark&) /* = delete */;
   };
 
   Mark GetMark() const { return Mark(*this, input); }
 
   Result GetInput(const Mark& mark, /*out*/ Input& item)
   {
     if (&mark.input != this || mark.mark > input) {
-      PR_NOT_REACHED("invalid mark");
-      return Result::FATAL_ERROR_INVALID_ARGS;
+      return NotReached("invalid mark", Result::FATAL_ERROR_INVALID_ARGS);
     }
     return item.Init(mark.mark, static_cast<uint16_t>(input - mark.mark));
   }
 
 private:
   Result Init(const uint8_t* data, uint16_t len)
   {
     if (input) {
--- a/security/pkix/include/pkix/Result.h
+++ b/security/pkix/include/pkix/Result.h
@@ -20,16 +20,18 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 #ifndef mozilla_pkix__Result_h
 #define mozilla_pkix__Result_h
 
+#include <cassert>
+
 #include "pkix/enumclass.h"
 
 namespace mozilla { namespace pkix {
 
 static const unsigned int FATAL_ERROR_FLAG = 0x800;
 
 MOZILLA_PKIX_ENUM_CLASS Result
 {
@@ -98,11 +100,18 @@ static const Result Success = Result::Su
 #endif
 
 inline bool
 IsFatalError(Result rv)
 {
   return static_cast<unsigned int>(rv) & FATAL_ERROR_FLAG;
 }
 
+inline Result
+NotReached(const char* /*explanation*/, Result result)
+{
+  assert(false);
+  return result;
+}
+
 } } // namespace mozilla::pkix
 
 #endif // mozilla_pkix__Result_h
--- a/security/pkix/lib/pkixbuild.cpp
+++ b/security/pkix/lib/pkixbuild.cpp
@@ -92,18 +92,18 @@ PathBuildingStep::RecordResult(Result ne
   if (newResult == Result::ERROR_UNTRUSTED_CERT) {
     newResult = Result::ERROR_UNTRUSTED_ISSUER;
   } else if (newResult == Result::ERROR_EXPIRED_CERTIFICATE) {
     newResult = Result::ERROR_EXPIRED_ISSUER_CERTIFICATE;
   }
 
   if (resultWasSet) {
     if (result == Success) {
-      PR_NOT_REACHED("RecordResult called after finding a chain");
-      return Result::FATAL_ERROR_INVALID_STATE;
+      return NotReached("RecordResult called after finding a chain",
+                        Result::FATAL_ERROR_INVALID_STATE);
     }
     // If every potential issuer has the same problem (e.g. expired) and/or if
     // there is only one bad potential issuer, then return a more specific
     // error. Otherwise, punt on trying to decide which error should be
     // returned by returning the generic Result::ERROR_UNKNOWN_ISSUER error.
     if (newResult != Success && newResult != result) {
       newResult = Result::ERROR_UNKNOWN_ISSUER;
     }
@@ -235,18 +235,17 @@ BuildForward(TrustDomain& trustDomain,
 
   if (trustLevel == TrustLevel::TrustAnchor) {
     // End of the recursion.
 
     NonOwningDERArray chain;
     for (const BackCert* cert = &subject; cert; cert = cert->childCert) {
       rv = chain.Append(cert->GetDER());
       if (rv != Success) {
-        PR_NOT_REACHED("NonOwningDERArray::SetItem failed.");
-        return rv;
+        return NotReached("NonOwningDERArray::SetItem failed.", rv);
       }
     }
 
     // This must be done here, after the chain is built but before any
     // revocation checks have been done.
     return trustDomain.IsChainValid(chain);
   }
 
--- a/security/pkix/lib/pkixcheck.cpp
+++ b/security/pkix/lib/pkixcheck.cpp
@@ -189,19 +189,19 @@ static const uint8_t anyPolicy[] = {
   4, { 0x55, 0x1d, 0x20, 0x00 }
 };
 
 bool
 CertPolicyId::IsAnyPolicy() const {
   if (this == &CertPolicyId::anyPolicy) {
     return true;
   }
-  return numBytes == PR_ARRAY_SIZE(::mozilla::pkix::anyPolicy) &&
+  return numBytes == sizeof(::mozilla::pkix::anyPolicy) &&
          !memcmp(bytes, ::mozilla::pkix::anyPolicy,
-                 PR_ARRAY_SIZE(::mozilla::pkix::anyPolicy));
+                 sizeof(::mozilla::pkix::anyPolicy));
 }
 
 // certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
 Result
 CheckCertificatePolicies(EndEntityOrCA endEntityOrCA,
                          const Input* encodedCertificatePolicies,
                          const Input* encodedInhibitAnyPolicy,
                          TrustLevel trustLevel,
@@ -519,22 +519,22 @@ MatchEKU(Reader& value, KeyPurposeId req
         match = value.MatchRest(email);
         break;
 
       case KeyPurposeId::id_kp_OCSPSigning:
         match = value.MatchRest(ocsp);
         break;
 
       case KeyPurposeId::anyExtendedKeyUsage:
-        PR_NOT_REACHED("anyExtendedKeyUsage should start with found==true");
-        return Result::FATAL_ERROR_LIBRARY_FAILURE;
+        return NotReached("anyExtendedKeyUsage should start with found==true",
+                          Result::FATAL_ERROR_LIBRARY_FAILURE);
 
       default:
-        PR_NOT_REACHED("unrecognized EKU");
-        return Result::FATAL_ERROR_LIBRARY_FAILURE;
+        return NotReached("unrecognized EKU",
+                          Result::FATAL_ERROR_LIBRARY_FAILURE);
     }
   }
 
   if (match) {
     found = true;
     if (requiredEKU == KeyPurposeId::id_kp_OCSPSigning) {
       foundOCSPSigning = true;
     }
--- a/security/pkix/lib/pkixder.cpp
+++ b/security/pkix/lib/pkixder.cpp
@@ -413,18 +413,18 @@ TimeChoice(Reader& tagged, uint8_t expec
     }
   } else if (expectedTag == UTCTime) {
     rv = ReadTwoDigits(input, 0, 99, yearLo);
     if (rv != Success) {
       return rv;
     }
     yearHi = yearLo >= 50u ? 19u : 20u;
   } else {
-    PR_NOT_REACHED("invalid tag given to TimeChoice");
-    return Result::ERROR_INVALID_TIME;
+    return NotReached("invalid tag given to TimeChoice",
+                      Result::ERROR_INVALID_TIME);
   }
   unsigned int year = (yearHi * 100u) + yearLo;
   if (year < 1970u) {
     // We don't support dates before January 1, 1970 because that is the epoch.
     return Result::ERROR_INVALID_TIME;
   }
   days = DaysBeforeYear(year);
 
@@ -469,18 +469,18 @@ TimeChoice(Reader& tagged, uint8_t expec
              break;
     case 11: daysInMonth = nov; days += jan + feb + mar + apr + may + jun +
                                         jul + aug + sep + oct;
              break;
     case 12: daysInMonth = dec; days += jan + feb + mar + apr + may + jun +
                                         jul + aug + sep + oct + nov;
              break;
     default:
-      PR_NOT_REACHED("month already bounds-checked by ReadTwoDigits");
-      return Result::FATAL_ERROR_INVALID_STATE;
+      return NotReached("month already bounds-checked by ReadTwoDigits",
+                        Result::FATAL_ERROR_INVALID_STATE);
   }
 
   unsigned int dayOfMonth;
   rv = ReadTwoDigits(input, 1u, daysInMonth, dayOfMonth);
   if (rv != Success) {
     return rv;
   }
   days += dayOfMonth - 1;
--- a/security/pkix/lib/pkixder.h
+++ b/security/pkix/lib/pkixder.h
@@ -32,18 +32,16 @@
 // 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,
--- a/security/pkix/lib/pkixocsp.cpp
+++ b/security/pkix/lib/pkixocsp.cpp
@@ -318,18 +318,17 @@ VerifyEncodedOCSPResponse(TrustDomain& t
       }
       return Success;
     case CertStatus::Revoked:
       return Result::ERROR_REVOKED_CERTIFICATE;
     case CertStatus::Unknown:
       return Result::ERROR_OCSP_UNKNOWN_CERT;
   }
 
-  PR_NOT_REACHED("unknown CertStatus");
-  return Result::ERROR_OCSP_UNKNOWN_CERT;
+  return NotReached("unknown CertStatus", Result::ERROR_OCSP_UNKNOWN_CERT);
 }
 
 // OCSPResponse ::= SEQUENCE {
 //       responseStatus         OCSPResponseStatus,
 //       responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL }
 //
 static inline Result
 OCSPResponse(Reader& input, Context& context)
@@ -883,17 +882,17 @@ CreateEncodedOCSPRequest(TrustDomain& tr
   static const uint8_t hashLen = TrustDomain::DIGEST_LENGTH;
 
   static const unsigned int totalLenWithoutSerialNumberData
     = 2                             // OCSPRequest
     + 2                             //   tbsRequest
     + 2                             //     requestList
     + 2                             //       Request
     + 2                             //         reqCert (CertID)
-    + PR_ARRAY_SIZE(hashAlgorithm)  //           hashAlgorithm
+    + sizeof(hashAlgorithm)         //           hashAlgorithm
     + 2 + hashLen                   //           issuerNameHash
     + 2 + hashLen                   //           issuerKeyHash
     + 2;                            //           serialNumber (header)
 
   // The only way we could have a request this large is if the serialNumber was
   // ridiculously and unreasonably large. RFC 5280 says "Conforming CAs MUST
   // NOT use serialNumber values longer than 20 octets." With this restriction,
   // we allow for some amount of non-conformance with that requirement while
@@ -913,17 +912,17 @@ CreateEncodedOCSPRequest(TrustDomain& tr
   uint8_t* d = out;
   *d++ = 0x30; *d++ = totalLen - 2u;  // OCSPRequest (SEQUENCE)
   *d++ = 0x30; *d++ = totalLen - 4u;  //   tbsRequest (SEQUENCE)
   *d++ = 0x30; *d++ = totalLen - 6u;  //     requestList (SEQUENCE OF)
   *d++ = 0x30; *d++ = totalLen - 8u;  //       Request (SEQUENCE)
   *d++ = 0x30; *d++ = totalLen - 10u; //         reqCert (CertID SEQUENCE)
 
   // reqCert.hashAlgorithm
-  for (size_t i = 0; i < PR_ARRAY_SIZE(hashAlgorithm); ++i) {
+  for (size_t i = 0; i < sizeof(hashAlgorithm); ++i) {
     *d++ = hashAlgorithm[i];
   }
 
   // reqCert.issuerNameHash (OCTET STRING)
   *d++ = 0x04;
   *d++ = hashLen;
   Result rv = trustDomain.DigestBuf(certID.issuer, d, hashLen);
   if (rv != Success) {
--- a/security/pkix/test/gtest/pkixder_input_tests.cpp
+++ b/security/pkix/test/gtest/pkixder_input_tests.cpp
@@ -436,17 +436,17 @@ TEST_F(pkixder_input_tests, MarkAndGetIn
   ASSERT_EQ(Success, input.Skip(sizeof expectedItemData));
 
   Input item;
   ASSERT_EQ(Success, input.GetInput(mark, item));
   Input expected(expectedItemData);
   ASSERT_TRUE(InputsAreEqual(expected, item));
 }
 
-// Cannot run this test on debug builds because of the PR_NOT_REACHED
+// Cannot run this test on debug builds because of the NotReached
 #ifndef DEBUG
 TEST_F(pkixder_input_tests, MarkAndGetInputDifferentInput)
 {
   const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
   Input buf(der);
   Reader input(buf);
 
   Reader another;