Bug 1274135 - Replace char_ptr_cast() and uint8_t_ptr_cast() with mozilla::BitwiseCast. r=keeler,valentin

The functions aren't necessary now that we have BitwiseCast.

MozReview-Commit-ID: 2nzOuwAop4Y

--HG--
extra : rebase_source : 196449249eec75b8eb10e59662231c3f4e83c268
This commit is contained in:
Cykesiopka 2016-09-01 15:58:51 +08:00
parent cfad685755
commit 0193f94d53
8 changed files with 43 additions and 49 deletions

View File

@ -4,11 +4,13 @@
* 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 "pk11pub.h"
#include "mozilla/Logging.h"
#include "ScopedNSSTypes.h"
#include "secoidt.h"
#include "BackgroundFileSaver.h"
#include "ScopedNSSTypes.h"
#include "mozilla/Casting.h"
#include "mozilla/Logging.h"
#include "mozilla/Telemetry.h"
#include "nsCOMArray.h"
#include "nsIAsyncInputStream.h"
#include "nsIFile.h"
#include "nsIMutableArray.h"
@ -16,12 +18,10 @@
#include "nsIX509Cert.h"
#include "nsIX509CertDB.h"
#include "nsIX509CertList.h"
#include "nsCOMArray.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"
#include "BackgroundFileSaver.h"
#include "mozilla/Telemetry.h"
#include "pk11pub.h"
#include "secoidt.h"
#ifdef XP_WIN
#include <windows.h>
@ -583,9 +583,10 @@ BackgroundFileSaver::ProcessStateChange()
return NS_ERROR_NOT_AVAILABLE;
}
nsresult rv = MapSECStatus(PK11_DigestOp(mDigestContext.get(),
uint8_t_ptr_cast(buffer),
count));
nsresult rv = MapSECStatus(
PK11_DigestOp(mDigestContext.get(),
BitwiseCast<unsigned char*, char*>(buffer),
count));
NS_ENSURE_SUCCESS(rv, rv);
}
@ -724,8 +725,9 @@ BackgroundFileSaver::CheckCompletion()
rv = d.End(SEC_OID_SHA256, mDigestContext);
if (NS_SUCCEEDED(rv)) {
MutexAutoLock lock(mLock);
mSha256 = nsDependentCSubstring(char_ptr_cast(d.get().data),
d.get().len);
mSha256 =
nsDependentCSubstring(BitwiseCast<char*, unsigned char*>(d.get().data),
d.get().len);
}
}
}
@ -1232,8 +1234,10 @@ DigestOutputStream::Write(const char* aBuf, uint32_t aCount, uint32_t* retval)
return NS_ERROR_NOT_AVAILABLE;
}
nsresult rv = MapSECStatus(PK11_DigestOp(mDigestContext,
uint8_t_ptr_cast(aBuf), aCount));
nsresult rv = MapSECStatus(
PK11_DigestOp(mDigestContext,
BitwiseCast<const unsigned char*, const char*>(aBuf),
aCount));
NS_ENSURE_SUCCESS(rv, rv);
return mOutputStream->Write(aBuf, aCount, retval);

View File

@ -86,7 +86,8 @@ ReadStream(const nsCOMPtr<nsIInputStream>& stream, /*out*/ SECItem& buf)
// instead of length, so that we can check whether the metadata for
// the entry is incorrect.
uint32_t bytesRead;
rv = stream->Read(char_ptr_cast(buf.data), buf.len, &bytesRead);
rv = stream->Read(BitwiseCast<char*, unsigned char*>(buf.data), buf.len,
&bytesRead);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -188,7 +189,8 @@ VerifyStreamContentDigest(nsIInputStream* stream,
uint64_t totalBytesRead = 0;
for (;;) {
uint32_t bytesRead;
rv = stream->Read(char_ptr_cast(buf.data), buf.len, &bytesRead);
rv = stream->Read(BitwiseCast<char*, unsigned char*>(buf.data), buf.len,
&bytesRead);
NS_ENSURE_SUCCESS(rv, rv);
if (bytesRead == 0) {
@ -751,7 +753,7 @@ OpenSignedAppFile(AppTrustedRoot aTrustedRoot, nsIFile* aJarFile,
}
ScopedAutoSECItem mfDigest;
rv = ParseSF(char_ptr_cast(sfBuffer.data), mfDigest);
rv = ParseSF(BitwiseCast<char*, unsigned char*>(sfBuffer.data), mfDigest);
if (NS_FAILED(rv)) {
return rv;
}
@ -777,7 +779,8 @@ OpenSignedAppFile(AppTrustedRoot aTrustedRoot, nsIFile* aJarFile,
nsTHashtable<nsCStringHashKey> items;
rv = ParseMF(char_ptr_cast(manifestBuffer.data), zip, items, buf);
rv = ParseMF(BitwiseCast<char*, unsigned char*>(manifestBuffer.data), zip,
items, buf);
if (NS_FAILED(rv)) {
return rv;
}
@ -1433,7 +1436,7 @@ VerifySignedDirectory(AppTrustedRoot aTrustedRoot,
// Get the expected manifest hash from the signed .sf file
ScopedAutoSECItem mfDigest;
rv = ParseSF(char_ptr_cast(sfBuffer.data), mfDigest);
rv = ParseSF(BitwiseCast<char*, unsigned char*>(sfBuffer.data), mfDigest);
if (NS_FAILED(rv)) {
return NS_ERROR_SIGNED_JAR_MANIFEST_INVALID;
}
@ -1460,7 +1463,7 @@ VerifySignedDirectory(AppTrustedRoot aTrustedRoot,
ScopedAutoSECItem buf(128 * 1024);
nsTHashtable<nsStringHashKey> items;
rv = ParseMFUnpacked(char_ptr_cast(manifestBuffer.data),
rv = ParseMFUnpacked(BitwiseCast<char*, unsigned char*>(manifestBuffer.data),
aDirectory, items, buf);
if (NS_FAILED(rv)){
return rv;

View File

@ -13,6 +13,7 @@
#include "NSSCertDBTrustDomain.h"
#include "NSSErrorsService.h"
#include "cert.h"
#include "mozilla/Casting.h"
#include "nsNSSComponent.h"
#include "nsServiceManagerUtils.h"
#include "pk11pub.h"
@ -711,7 +712,8 @@ CertVerifier::VerifySSLServerCert(const UniqueCERTCertificate& peerCert,
}
Input hostnameInput;
result = hostnameInput.Init(uint8_t_ptr_cast(hostname), strlen(hostname));
result = hostnameInput.Init(BitwiseCast<const uint8_t*, const char*>(hostname),
strlen(hostname));
if (result != Success) {
PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
return SECFailure;

View File

@ -193,8 +193,9 @@ ContentSignatureVerifier::CreateContextInternal(const nsACString& aData,
// Check the SAN
Input hostnameInput;
result = hostnameInput.Init(uint8_t_ptr_cast(aName.BeginReading()),
aName.Length());
result = hostnameInput.Init(
BitwiseCast<const uint8_t*, const char*>(aName.BeginReading()),
aName.Length());
if (result != Success) {
return NS_ERROR_FAILURE;
}

View File

@ -431,8 +431,9 @@ DetermineCertOverrideErrors(const UniqueCERTCertificate& cert,
return SECFailure;
}
Input hostnameInput;
Result result = hostnameInput.Init(uint8_t_ptr_cast(hostName),
strlen(hostName));
Result result = hostnameInput.Init(
BitwiseCast<const uint8_t*, const char*>(hostName),
strlen(hostName));
if (result != Success) {
PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
return SECFailure;

View File

@ -16,7 +16,6 @@
#include "cms.h"
#include "cryptohi.h"
#include "keyhi.h"
#include "mozilla/Casting.h"
#include "mozilla/Likely.h"
#include "mozilla/Scoped.h"
#include "mozilla/UniquePtr.h"
@ -38,24 +37,6 @@
namespace mozilla {
// Deprecated: Use something like |mozilla::BitwiseCast<char*, uint8_t*>(p)|
// instead.
// It is very common to cast between char* and uint8_t* when doing crypto stuff.
// Here, we provide more type-safe wrappers around reinterpret_cast so you don't
// shoot yourself in the foot by reinterpret_casting completely unrelated types.
inline char*
char_ptr_cast(uint8_t* p) { return BitwiseCast<char*>(p); }
inline const char*
char_ptr_cast(const uint8_t* p) { return BitwiseCast<const char*>(p); }
inline uint8_t*
uint8_t_ptr_cast(char* p) { return BitwiseCast<uint8_t*>(p); }
inline const uint8_t*
uint8_t_ptr_cast(const char* p) { return BitwiseCast<const uint8_t*>(p); }
// NSPR APIs use PRStatus/PR_GetError and NSS APIs use SECStatus/PR_GetError to
// report success/failure. This function makes it more convenient and *safer*
// to translate NSPR/NSS results to nsresult. It is safer because it

View File

@ -286,8 +286,10 @@ nsDataSignatureVerifier::VerifySignature(const char* aRSABuf,
*aSigningCert = nullptr;
Digest digest;
nsresult rv = digest.DigestBuf(SEC_OID_SHA1, uint8_t_ptr_cast(aPlaintext),
aPlaintextLen);
nsresult rv = digest.DigestBuf(
SEC_OID_SHA1,
BitwiseCast<const uint8_t*, const char*>(aPlaintext),
aPlaintextLen);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

View File

@ -248,8 +248,8 @@ nsNSSCertificateDB::getCertsFromPackage(const UniquePLArenaPool& arena,
}
collectArgs->arena = arena.get();
if (CERT_DecodeCertPackage(char_ptr_cast(data), length, collect_certs,
collectArgs) != SECSuccess) {
if (CERT_DecodeCertPackage(BitwiseCast<char*, uint8_t*>(data), length,
collect_certs, collectArgs) != SECSuccess) {
return nullptr;
}