diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 934675fdda..c483ddde68 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -3688,36 +3688,41 @@ function registry_update() {
  * @see drupal_static_reset()
  */
 function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
-  static $data = array(), $default = array();
-  // First check if dealing with a previously defined static variable.
-  if (isset($data[$name]) || array_key_exists($name, $data)) {
-    // Non-NULL $name and both $data[$name] and $default[$name] statics exist.
-    if ($reset) {
-      // Reset pre-existing static variable to its default value.
-      $data[$name] = $default[$name];
+  static $data = array(), $defaults = array();
+  if (isset($name)) {
+    // Check if we're dealing with a previously defined static variable.
+    if (array_key_exists($name, $data)) {
+      // Both $data[$name] and (implicitly) $defaults[$name] statics exist.
+      if ($reset) {
+        // Reset pre-existing static variable to its default value.
+        $data[$name] = $defaults[$name];
+      }
+      return $data[$name];
+    }
+    else {
+      // Neither $data[$name] nor $defaults[$name] static variables exist.
+      if ($reset) {
+        // Reset was called before any value for $name was set, so we should
+        // not set anything ($default_value is not reliable in this case). As
+        // the function returns a reference, we must still return a variable.
+        // (Code using $reset does not use the return value).
+        return $data;
+      }
+      // First call with new non-NULL $name. Initialize a new static variable.
+      $defaults[$name] = $data[$name] = $default_value;
+      return $data[$name];
     }
-    return $data[$name];
   }
-  // Neither $data[$name] nor $default[$name] static variables exist.
-  if (isset($name)) {
-    if ($reset) {
-      // Reset was called before a default is set and yet a variable must be
-      // returned.
-      return $data;
-    }
-    // First call with new non-NULL $name. Initialize a new static variable.
-    $default[$name] = $data[$name] = $default_value;
-    return $data[$name];
-  }
-  // Reset all: ($name == NULL). This needs to be done one at a time so that
-  // references returned by earlier invocations of drupal_static() also get
-  // reset.
-  foreach ($default as $name => $value) {
-    $data[$name] = $value;
-  }
-  // As the function returns a reference, the return should always be a
-  // variable.
-  return $data;
+  else {
+    // Reset all: ($name == NULL). This needs to be done one at a time so that
+    // references returned by earlier invocations of drupal_static() also get
+    // reset.
+    foreach ($defaults as $name => $value) {
+      $data[$name] = $value;
+    }
+    // As the function returns a reference, we must still return a variable.
+    return $data;
+  }
 }
 
 /**
