Bug 1252722 - Improve handling of PK11_* function error codes. r=keeler
authorCykesiopka <cykesiopka.bmo@gmail.com>
Thu, 31 Mar 2016 17:33:00 -0700
changeset 291234 71b02a7c2340ce884f3650976621eb7c9ab10051
parent 291233 0f8ee183f2cde3efd970c06efc45d6e69502cbb0
child 291235 0e7aec22dfef3b8f6abd5f3f1a569643bd544800
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler
bugs1252722
milestone48.0a1
Bug 1252722 - Improve handling of PK11_* function error codes. r=keeler MozReview-Commit-ID: DWNNXq8ZJ47
security/manager/ssl/nsPKCS11Slot.cpp
security/manager/ssl/nsPKCS11Slot.h
--- a/security/manager/ssl/nsPKCS11Slot.cpp
+++ b/security/manager/ssl/nsPKCS11Slot.cpp
@@ -1,16 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsPKCS11Slot.h"
 
 #include "mozilla/Logging.h"
 #include "mozilla/Telemetry.h"
+#include "mozilla/unused.h"
 #include "nsCOMPtr.h"
 #include "nsIMutableArray.h"
 #include "nsPK11TokenDB.h"
 #include "secmod.h"
 
 using mozilla::LogLevel;
 
 extern mozilla::LazyLogModule gPIPNSSLog;
@@ -22,25 +23,26 @@ nsPKCS11Slot::nsPKCS11Slot(PK11SlotInfo*
   MOZ_ASSERT(slot);
 
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown())
     return;
 
   mSlot.reset(PK11_ReferenceSlot(slot));
   mSeries = PK11_GetSlotSeries(slot);
-  refreshSlotInfo(locker);
+  Unused << refreshSlotInfo(locker);
 }
 
-void
+nsresult
 nsPKCS11Slot::refreshSlotInfo(const nsNSSShutDownPreventionLock& /*proofOfLock*/)
 {
   CK_SLOT_INFO slotInfo;
-  if (PK11_GetSlotInfo(mSlot.get(), &slotInfo) != SECSuccess) {
-    return;
+  nsresult rv = MapSECStatus(PK11_GetSlotInfo(mSlot.get(), &slotInfo));
+  if (NS_FAILED(rv)) {
+    return rv;
   }
 
   // Set the Description field
   const char* ccDesc = reinterpret_cast<const char*>(slotInfo.slotDescription);
   const nsACString& cDesc = Substring(
     ccDesc,
     ccDesc + PL_strnlen(ccDesc, sizeof(slotInfo.slotDescription)));
   mSlotDesc = NS_ConvertUTF8toUTF16(cDesc);
@@ -60,16 +62,18 @@ nsPKCS11Slot::refreshSlotInfo(const nsNS
   mSlotHWVersion.Append('.');
   mSlotHWVersion.AppendInt(slotInfo.hardwareVersion.minor);
 
   // Set the Firmware Version field
   mSlotFWVersion = EmptyString();
   mSlotFWVersion.AppendInt(slotInfo.firmwareVersion.major);
   mSlotFWVersion.Append('.');
   mSlotFWVersion.AppendInt(slotInfo.firmwareVersion.minor);
+
+  return NS_OK;
 }
 
 nsPKCS11Slot::~nsPKCS11Slot()
 {
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown()) {
     return;
   }
@@ -120,17 +124,20 @@ nsPKCS11Slot::GetDesc(char16_t** aDesc)
 {
   NS_ENSURE_ARG_POINTER(aDesc);
 
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown())
     return NS_ERROR_NOT_AVAILABLE;
 
   if (PK11_GetSlotSeries(mSlot.get()) != mSeries) {
-    refreshSlotInfo(locker);
+    nsresult rv = refreshSlotInfo(locker);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
   }
 
   *aDesc = ToNewUnicode(mSlotDesc);
   if (!*aDesc) return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP
@@ -139,17 +146,20 @@ nsPKCS11Slot::GetManID(char16_t** aManID
   NS_ENSURE_ARG_POINTER(aManID);
 
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown()) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   if (PK11_GetSlotSeries(mSlot.get()) != mSeries) {
-    refreshSlotInfo(locker);
+    nsresult rv = refreshSlotInfo(locker);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
   }
   *aManID = ToNewUnicode(mSlotManID);
   if (!*aManID) return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPKCS11Slot::GetHWVersion(char16_t** aHWVersion)
@@ -157,17 +167,20 @@ nsPKCS11Slot::GetHWVersion(char16_t** aH
   NS_ENSURE_ARG_POINTER(aHWVersion);
 
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown()) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   if (PK11_GetSlotSeries(mSlot.get()) != mSeries) {
-    refreshSlotInfo(locker);
+    nsresult rv = refreshSlotInfo(locker);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
   }
   *aHWVersion = ToNewUnicode(mSlotHWVersion);
   if (!*aHWVersion) return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPKCS11Slot::GetFWVersion(char16_t** aFWVersion)
@@ -175,17 +188,20 @@ nsPKCS11Slot::GetFWVersion(char16_t** aF
   NS_ENSURE_ARG_POINTER(aFWVersion);
 
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown()) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   if (PK11_GetSlotSeries(mSlot.get()) != mSeries) {
-    refreshSlotInfo(locker);
+    nsresult rv = refreshSlotInfo(locker);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
   }
   *aFWVersion = ToNewUnicode(mSlotFWVersion);
   if (!*aFWVersion) return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPKCS11Slot::GetToken(nsIPK11Token** _retval)
@@ -211,17 +227,20 @@ nsPKCS11Slot::GetTokenName(char16_t** aN
     return NS_ERROR_NOT_AVAILABLE;
 
   if (!PK11_IsPresent(mSlot.get())) {
     *aName = nullptr;
     return NS_OK;
   }
 
   if (PK11_GetSlotSeries(mSlot.get()) != mSeries) {
-    refreshSlotInfo(locker);
+    nsresult rv = refreshSlotInfo(locker);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
   }
 
   *aName = ToNewUnicode(NS_ConvertUTF8toUTF16(PK11_GetTokenName(mSlot.get())));
   if (!*aName) return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP
--- a/security/manager/ssl/nsPKCS11Slot.h
+++ b/security/manager/ssl/nsPKCS11Slot.h
@@ -31,17 +31,17 @@ protected:
 
 private:
   mozilla::UniquePK11SlotInfo mSlot;
   nsString mSlotDesc, mSlotManID, mSlotHWVersion, mSlotFWVersion;
   int mSeries;
 
   virtual void virtualDestroyNSSReference() override;
   void destructorSafeDestroyNSSReference();
-  void refreshSlotInfo(const nsNSSShutDownPreventionLock& proofOfLock);
+  nsresult refreshSlotInfo(const nsNSSShutDownPreventionLock& proofOfLock);
 };
 
 class nsPKCS11Module : public nsIPKCS11Module,
                        public nsNSSShutDownObject
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIPKCS11MODULE