From feae914859ee843699ffcfd1dc5fb313893a618b Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Fri, 22 Nov 2013 06:22:52 +0100 Subject: [PATCH] Bug 882130 - Additional cleanup. r=bent --- dom/indexedDB/IDBObjectStore.cpp | 51 +++++++++++++++++++------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/dom/indexedDB/IDBObjectStore.cpp b/dom/indexedDB/IDBObjectStore.cpp index 11ed3de7b6f8..3ae4af9b62b7 100644 --- a/dom/indexedDB/IDBObjectStore.cpp +++ b/dom/indexedDB/IDBObjectStore.cpp @@ -13,13 +13,13 @@ #include #include "jsfriendapi.h" -#include "mozilla/Endian.h" #include "mozilla/dom/ContentChild.h" #include "mozilla/dom/ContentParent.h" #include "mozilla/dom/FileHandleBinding.h" #include "mozilla/dom/StructuredCloneTags.h" #include "mozilla/dom/ipc/Blob.h" #include "mozilla/dom/quota/FileStreams.h" +#include "mozilla/Endian.h" #include "mozilla/storage.h" #include "nsContentUtils.h" #include "nsDOMClassInfo.h" @@ -57,6 +57,7 @@ using namespace mozilla::dom::indexedDB::ipc; using mozilla::dom::quota::FileOutputStream; using mozilla::ErrorResult; using mozilla::fallible_t; +using mozilla::LittleEndian; using mozilla::NativeEndian; BEGIN_INDEXEDDB_NAMESPACE @@ -1580,10 +1581,12 @@ IDBObjectStore::StructuredCloneWriteCallback(JSContext* aCx, } NS_ConvertUTF16toUTF8 convType(fileHandle->Type()); - uint32_t convTypeLength = NativeEndian::swapToLittleEndian(convType.Length()); + uint32_t convTypeLength = + NativeEndian::swapToLittleEndian(convType.Length()); NS_ConvertUTF16toUTF8 convName(fileHandle->Name()); - uint32_t convNameLength = NativeEndian::swapToLittleEndian(convName.Length()); + uint32_t convNameLength = + NativeEndian::swapToLittleEndian(convName.Length()); if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FILEHANDLE, cloneWriteInfo->mFiles.Length()) || @@ -1646,7 +1649,8 @@ IDBObjectStore::StructuredCloneWriteCallback(JSContext* aCx, return false; } NS_ConvertUTF16toUTF8 convType(type); - uint32_t convTypeLength = NativeEndian::swapToLittleEndian(convType.Length()); + uint32_t convTypeLength = + NativeEndian::swapToLittleEndian(convType.Length()); nsCOMPtr file = do_QueryInterface(blob); @@ -1673,7 +1677,8 @@ IDBObjectStore::StructuredCloneWriteCallback(JSContext* aCx, return false; } NS_ConvertUTF16toUTF8 convName(name); - uint32_t convNameLength = NativeEndian::swapToLittleEndian(convName.Length()); + uint32_t convNameLength = + NativeEndian::swapToLittleEndian(convName.Length()); if (!JS_WriteBytes(aWriter, &lastModifiedDate, sizeof(lastModifiedDate)) || !JS_WriteBytes(aWriter, &convNameLength, sizeof(convNameLength)) || @@ -3112,6 +3117,18 @@ NoRequestObjectStoreHelper::OnError() mTransaction->Abort(GetResultCode()); } +// This is a duplicate of the js engine's byte munging in StructuredClone.cpp +uint64_t +ReinterpretDoubleAsUInt64(double d) +{ + union { + double d; + uint64_t u; + } pun; + pun.d = d; + return pun.u; +} + nsresult AddHelper::DoDatabaseWork(mozIStorageConnection* aConnection) { @@ -3158,9 +3175,14 @@ AddHelper::DoDatabaseWork(mozIStorageConnection* aConnection) if (mObjectStore->IsAutoIncrement()) { if (keyUnset) { autoIncrementNum = mObjectStore->Info()->nextAutoIncrementId; + + MOZ_ASSERT(autoIncrementNum > 0, + "Generated key must always be a positive integer"); + if (autoIncrementNum > (1LL << 53)) { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } + mKey.SetFromInteger(autoIncrementNum); } else if (mKey.IsFloat() && @@ -3173,21 +3195,10 @@ AddHelper::DoDatabaseWork(mozIStorageConnection* aConnection) // objectStore with no key in its keyPath set. We needed to figure out // which row id we would get above before we could set that properly. - // This is a duplicate of the js engine's byte munging here - union { - double d; - uint64_t u; - } pun; - - NS_ASSERTION(autoIncrementNum > 0, - "Generated key must always a positive integer"); - pun.d = static_cast(autoIncrementNum); - pun.u = NativeEndian::swapToLittleEndian(pun.u); - - JSAutoStructuredCloneBuffer& buffer = mCloneWriteInfo.mCloneBuffer; - uint64_t offsetToKeyProp = mCloneWriteInfo.mOffsetToKeyProp; - - memcpy((char*)buffer.data() + offsetToKeyProp, &pun.u, sizeof(uint64_t)); + LittleEndian::writeUint64((char*)mCloneWriteInfo.mCloneBuffer.data() + + mCloneWriteInfo.mOffsetToKeyProp, + ReinterpretDoubleAsUInt64(static_cast( + autoIncrementNum))); } }