check for non-null connection in mozStorageTransaction helper. b=384224, r=sspitzer
authordwitte@stanford.edu
Wed, 13 Jun 2007 21:58:52 -0700
changeset 2379 3fcb5ce098099bd68e127f02fbba0949225456a9
parent 2378 3d5f56e35402408929fb8e90b5e1578431c68b50
child 2380 90c9b0e1c57c65ffafac36bbf202db8768be8369
push idunknown
push userunknown
push dateunknown
reviewerssspitzer
bugs384224
milestone1.9a6pre
check for non-null connection in mozStorageTransaction helper. b=384224, r=sspitzer
storage/public/mozStorageHelper.h
--- a/storage/public/mozStorageHelper.h
+++ b/storage/public/mozStorageHelper.h
@@ -59,59 +59,62 @@
  */
 class mozStorageTransaction
 {
 public:
   mozStorageTransaction(mozIStorageConnection* aConnection,
                         PRBool aCommitOnComplete,
                         PRInt32 aType = mozIStorageConnection::TRANSACTION_DEFERRED)
     : mConnection(aConnection),
+      mHasTransaction(PR_FALSE),
       mCommitOnComplete(aCommitOnComplete),
       mCompleted(PR_FALSE)
   {
-    PRBool transactionInProgress = PR_FALSE;
-    mConnection->GetTransactionInProgress(&transactionInProgress);
-    mHasTransaction = ! transactionInProgress;
-    if (mHasTransaction)
-      mConnection->BeginTransactionAs(aType);
+    if (mConnection) {
+      PRBool transactionInProgress = PR_FALSE;
+      mConnection->GetTransactionInProgress(&transactionInProgress);
+      mHasTransaction = ! transactionInProgress;
+      if (mHasTransaction)
+        mConnection->BeginTransactionAs(aType);
+    }
   }
   ~mozStorageTransaction()
   {
-    if (mHasTransaction && ! mCompleted) {
+    if (mConnection && mHasTransaction && ! mCompleted) {
       if (mCommitOnComplete)
         mConnection->CommitTransaction();
       else
         mConnection->RollbackTransaction();
     }
   }
 
   /**
    * Commits the transaction if one is in progress. If one is not in progress,
    * this is a NOP since the actual owner of the transaction outside of our
    * scope is in charge of finally comitting or rolling back the transaction.
    */
   nsresult Commit()
   {
-    if (mCompleted)
-      return NS_OK; // already done
+    if (!mConnection || mCompleted)
+      return NS_OK; // no connection, or already done
     mCompleted = PR_TRUE;
     if (! mHasTransaction)
       return NS_OK; // transaction not ours, ignore
     return mConnection->CommitTransaction();
   }
 
   /**
    * Rolls back the transaction in progress. You should only call this function
    * if this object has a real transaction (HasTransaction() = true) because
    * otherwise, there is no transaction to roll back.
    */
   nsresult Rollback()
   {
-    if (mCompleted)
-      return NS_OK; // already done
+    if (!mConnection || mCompleted)
+      return NS_OK; // no connection, or already done
     mCompleted = PR_TRUE;
     if (! mHasTransaction)
       return NS_ERROR_FAILURE;
     return mConnection->RollbackTransaction();
   }
 
   /**
    * Returns whether this object wraps a real transaction. False means that