Bug 383673 - mozStorageValueArray only asserts on out of bounds (and other cleanup). r=sspitzer
authorsdwilsh@shawnwilsher.com
Tue, 28 Aug 2007 08:29:49 -0700
changeset 5379 a508d161b8d9f34fec97079b0387d2e8f1d6d976
parent 5378 2e0b585201a7f8480db6a6354c87f3e961693d5f
child 5380 2a4cb3a7f070b4176db83365d80d34a698370c0e
push idunknown
push userunknown
push dateunknown
reviewerssspitzer
bugs383673
milestone1.9a8pre
Bug 383673 - mozStorageValueArray only asserts on out of bounds (and other cleanup). r=sspitzer
storage/src/mozStorageValueArray.cpp
--- a/storage/src/mozStorageValueArray.cpp
+++ b/storage/src/mozStorageValueArray.cpp
@@ -67,17 +67,18 @@ mozStorageStatementRowValueArray::GetNum
     *aLength = mNumEntries;
     return NS_OK;
 }
 
 /* long getTypeOfIndex (in unsigned long aIndex); */
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mNumEntries)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     int t = sqlite3_column_type (mSqliteStatement, aIndex);
     switch (t) {
         case SQLITE_INTEGER:
             *_retval = VALUE_TYPE_INTEGER;
             break;
         case SQLITE_FLOAT:
             *_retval = VALUE_TYPE_FLOAT;
@@ -97,48 +98,50 @@ mozStorageStatementRowValueArray::GetTyp
     }
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mNumEntries)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_column_int (mSqliteStatement, aIndex);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mNumEntries)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_column_int64 (mSqliteStatement, aIndex);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetDouble(PRUint32 aIndex, double *_retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mNumEntries)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_column_double (mSqliteStatement, aIndex);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetUTF8String(PRUint32 aIndex, nsACString &_retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
-
+    // GetTypeOfIndex will check aIndex for us, so we don't have to.
     PRInt32 type;
     nsresult rv = GetTypeOfIndex (aIndex, &type);
     NS_ENSURE_SUCCESS(rv, rv);
     if (type == SQLITE_NULL) {
         // null columns get IsVoid set to distinguish them from empty strings
         _retval.Truncate(0);
         _retval.SetIsVoid(PR_TRUE);
     } else {
@@ -147,18 +150,17 @@ mozStorageStatementRowValueArray::GetUTF
         _retval.Assign(cstr, slen);
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
-
+    // GetTypeOfIndex will check aIndex for us, so we don't have to.
     PRInt32 type;
     nsresult rv = GetTypeOfIndex (aIndex, &type);
     NS_ENSURE_SUCCESS(rv, rv);
     if (type == SQLITE_NULL) {
         // null columns get IsVoid set to distinguish them from empty strings
         _retval.Truncate(0);
         _retval.SetIsVoid(PR_TRUE);
     } else {
@@ -167,17 +169,18 @@ mozStorageStatementRowValueArray::GetStr
         _retval.Assign (wstr, slen/2);
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
 {
-    NS_ASSERTION (aIndex < mNumEntries, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mNumEntries)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     int blobsize = sqlite3_column_bytes (mSqliteStatement, aIndex);
     const void *blob = sqlite3_column_blob (mSqliteStatement, aIndex);
 
     void *blobcopy = nsMemory::Clone(blob, blobsize);
     if (blobcopy == NULL)
         return NS_ERROR_OUT_OF_MEMORY;
 
@@ -185,20 +188,20 @@ mozStorageStatementRowValueArray::GetBlo
     *aDataSize = blobsize;
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageStatementRowValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
 {
+    // GetTypeOfIndex will check aIndex for us, so we don't have to.
     PRInt32 t;
     nsresult rv = GetTypeOfIndex (aIndex, &t);
-    if (NS_FAILED(rv))
-        return rv;
+    NS_ENSURE_SUCCESS(rv, rv);
 
     if (t == VALUE_TYPE_NULL)
         *_retval = PR_TRUE;
     else
         *_retval = PR_FALSE;
 
     return NS_OK;
 }
@@ -261,17 +264,18 @@ mozStorageArgvValueArray::GetNumEntries(
     *aLength = mArgc;
     return NS_OK;
 }
 
 /* long getTypeOfIndex (in unsigned long aIndex); */
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     int t = sqlite3_value_type (mArgv[aIndex]);
     switch (t) {
         case SQLITE_INTEGER:
             *_retval = VALUE_TYPE_INTEGER;
             break;
         case SQLITE_FLOAT:
             *_retval = VALUE_TYPE_FLOAT;
@@ -291,81 +295,87 @@ mozStorageArgvValueArray::GetTypeOfIndex
     }
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetInt32(PRUint32 aIndex, PRInt32 *_retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_value_int (mArgv[aIndex]);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetInt64(PRUint32 aIndex, PRInt64 *_retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_value_int64 (mArgv[aIndex]);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetDouble(PRUint32 aIndex, double *_retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     *_retval = sqlite3_value_double (mArgv[aIndex]);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetUTF8String(PRUint32 aIndex, nsACString & _retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
         // null columns get IsVoid set to distinguish them from empty strings
         _retval.Truncate(0);
         _retval.SetIsVoid(PR_TRUE);
     } else {
         int slen = sqlite3_value_bytes (mArgv[aIndex]);
         const unsigned char *cstr = sqlite3_value_text (mArgv[aIndex]);
         _retval.Assign ((char *) cstr, slen);
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetString(PRUint32 aIndex, nsAString & _retval)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     if (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL) {
         // null columns get IsVoid set to distinguish them from empty strings
         _retval.Truncate(0);
         _retval.SetIsVoid(PR_TRUE);
     } else {
         int slen = sqlite3_value_bytes16 (mArgv[aIndex]);
         const PRUnichar *wstr = (const PRUnichar *) sqlite3_value_text16 (mArgv[aIndex]);
         _retval.Assign (wstr, slen);
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData)
 {
-    NS_ASSERTION (aIndex < mArgc, "aIndex out of range");
+    if (aIndex < 0 || aIndex >= mArgc)
+        return NS_ERROR_ILLEGAL_VALUE;
 
     int blobsize = sqlite3_value_bytes (mArgv[aIndex]);
     const void *blob = sqlite3_value_blob (mArgv[aIndex]);
 
     void *blobcopy = nsMemory::Clone(blob, blobsize);
     if (blobcopy == NULL)
         return NS_ERROR_OUT_OF_MEMORY;
 
@@ -374,20 +384,20 @@ mozStorageArgvValueArray::GetBlob(PRUint
 
     return NS_OK;
 }
 
 /* boolean getIsNull (in unsigned long aIndex); */
 NS_IMETHODIMP
 mozStorageArgvValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval)
 {
+    // GetTypeOfIndex will check aIndex for us, so we don't have to.
     PRInt32 t;
     nsresult rv = GetTypeOfIndex (aIndex, &t);
-    if (NS_FAILED(rv))
-        return rv;
+    NS_ENSURE_SUCCESS(rv, rv);
 
     if (t == VALUE_TYPE_NULL)
         *_retval = PR_TRUE;
     else
         *_retval = PR_FALSE;
 
     return NS_OK;
 }