Bug 580839 - 'finally' blocks now check for a valid statement to avoid potentially masking the original exception. r=dolske
authorJared Wein <jwein@mozilla.com>
Fri, 17 Jun 2011 18:01:34 -0700
changeset 71240 1d962ae5a4b68835554e39465de31c53c95530a6
parent 71239 e2d9e1039dca2b22447d260f17f39ff453c098ff
child 71241 c0b984e8ade0c18995f65d63f7cb6a339dadd02d
push idunknown
push userunknown
push dateunknown
reviewersdolske
bugs580839
milestone7.0a1
Bug 580839 - 'finally' blocks now check for a valid statement to avoid potentially masking the original exception. r=dolske
toolkit/components/satchel/nsFormAutoComplete.js
toolkit/components/satchel/nsFormHistory.js
--- a/toolkit/components/satchel/nsFormAutoComplete.js
+++ b/toolkit/components/satchel/nsFormAutoComplete.js
@@ -309,17 +309,19 @@ FormAutoComplete.prototype = {
                 }
                 values.push(entry);
             }
 
         } catch (e) {
             this.log("getValues failed: " + e.name + " : " + e.message);
             throw "DB failed getting form autocomplete values";
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         return values;
     },
 
 
     _dbStmts      : null,
 
--- a/toolkit/components/satchel/nsFormHistory.js
+++ b/toolkit/components/satchel/nsFormHistory.js
@@ -212,17 +212,19 @@ FormHistory.prototype = {
             try {
                 stmt = this.dbCreateStatement(query, params);
                 stmt.execute();
                 this.sendStringNotification("modifyEntry", name, value, guid);
             } catch (e) {
                 this.log("addEntry (modify) failed: " + e);
                 throw e;
             } finally {
-                stmt.reset();
+                if (stmt) {
+                    stmt.reset();
+                }
             }
 
         } else {
             // Add new entry
             guid = this.generateGUID();
 
             let query = "INSERT INTO moz_formhistory (fieldname, value, timesUsed, firstUsed, lastUsed, guid) " +
                             "VALUES (:fieldname, :value, :timesUsed, :firstUsed, :lastUsed, :guid)";
@@ -238,17 +240,19 @@ FormHistory.prototype = {
             try {
                 stmt = this.dbCreateStatement(query, params);
                 stmt.execute();
                 this.sendStringNotification("addEntry", name, value, guid);
             } catch (e) {
                 this.log("addEntry (create) failed: " + e);
                 throw e;
             } finally {
-                stmt.reset();
+                if (stmt) {
+                    stmt.reset();
+                }
             }
         }
     },
 
 
     removeEntry : function (name, value) {
         this.log("removeEntry for " + name + "=" + value);
 
@@ -262,17 +266,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.execute();
             this.sendStringNotification("removeEntry", name, value, guid);
         } catch (e) {
             this.log("removeEntry failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
     },
 
 
     removeEntriesForName : function (name) {
         this.log("removeEntriesForName with name=" + name);
 
         this.sendStringNotification("before-removeEntriesForName", name);
@@ -284,17 +290,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.execute();
             this.sendStringNotification("removeEntriesForName", name);
         } catch (e) {
             this.log("removeEntriesForName failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
     },
 
 
     removeAllEntries : function () {
         this.log("removeAllEntries");
 
         this.sendNotification("before-removeAllEntries", null);
@@ -305,17 +313,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query);
             stmt.execute();
             this.sendNotification("removeAllEntries", null);
         } catch (e) {
             this.log("removeEntriesForName failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         // privacy cleanup, if there's an old mork formhistory around, just delete it
         let oldFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
         oldFile.append("formhistory.dat");
         if (oldFile.exists())
             oldFile.remove(false);
     },
@@ -329,17 +339,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.executeStep();
             return (stmt.row.numEntries > 0);
         } catch (e) {
             this.log("nameExists failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
     },
 
     entryExists : function (name, value) {
         this.log("entryExists for " + name + "=" + value);
         let [id, guid] = this.getExistingEntryID(name, value);
         this.log("entryExists: id=" + id);
         return (id != -1);
@@ -359,17 +371,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.executeStep();
             this.sendIntNotification("removeEntriesByTimeframe", beginTime, endTime);
         } catch (e) {
             this.log("removeEntriesByTimeframe failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
     },
 
 
     get DBConnection() {
         return this.dbConnection;
     },
@@ -471,17 +485,19 @@ FormHistory.prototype = {
             if (stmt.executeStep()) {
                 id   = stmt.row.id;
                 guid = stmt.row.guid;
             }
         } catch (e) {
             this.log("getExistingEntryID failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         return [id, guid];
     },
 
 
     countAllEntries : function () {
         let query = "SELECT COUNT(1) AS numEntries FROM moz_formhistory";
@@ -490,17 +506,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query, null);
             stmt.executeStep();
             numEntries = stmt.row.numEntries;
         } catch (e) {
             this.log("countAllEntries failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         this.log("countAllEntries: counted entries: " + numEntries);
         return numEntries;
     },
 
 
     expireOldEntries : function () {
@@ -526,17 +544,19 @@ FormHistory.prototype = {
 
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.execute();
         } catch (e) {
             this.log("expireOldEntries failed: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         let endingCount = this.countAllEntries();
 
         // If we expired a large batch of entries, shrink the DB to reclaim wasted
         // space. This is expected to happen when entries predating timestamps
         // (added in the v.1 schema) expire in mass, 180 days after the DB was
         // upgraded -- entries not used since then expire all at once.
@@ -695,17 +715,19 @@ FormHistory.prototype = {
         let params = { time: (Date.now() - DAY_IN_MS) * 1000 }
         try {
             stmt = this.dbCreateStatement(query, params);
             stmt.execute();
         } catch (e) {
             this.log("Failed setting timestamps: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
     },
 
 
     /*
      * dbMigrateToVersion2
      *
      * Updates the DB schema to v2 (bug 243136).
@@ -744,17 +766,19 @@ FormHistory.prototype = {
         try {
             stmt = this.dbCreateStatement(query);
             while (stmt.executeStep())
                 ids.push(stmt.row.id);
         } catch (e) {
             this.log("Failed getting IDs: " + e);
             throw e;
         } finally {
-            stmt.reset();
+            if (stmt) {
+                stmt.reset();
+            }
         }
 
         // Generate a GUID for each login and update the DB.
         query = "UPDATE moz_formhistory SET guid = :guid WHERE id = :id";
         for each (let id in ids) {
             let params = {
                 id   : id,
                 guid : this.generateGUID()
@@ -762,17 +786,19 @@ FormHistory.prototype = {
 
             try {
                 stmt = this.dbCreateStatement(query, params);
                 stmt.execute();
             } catch (e) {
                 this.log("Failed setting GUID: " + e);
                 throw e;
             } finally {
-                stmt.reset();
+                if (stmt) {
+                    stmt.reset();
+                }
             }
         }
     },
 
 
     /*
      * dbAreExpectedColumnsPresent
      *