Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31311)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31312)
@@ -3216,12 +3216,13 @@
                         medium->getLocationFull().raw());
 
-    bool fImplicit = false;
+    bool fIndirect = false;
     if (!medium.isNull())
-        fImplicit = medium->isReadOnly();
+        fIndirect = medium->isReadOnly();
     bool associate = true;
 
     do
     {
-        if (aType == DeviceType_HardDisk && mMediaData.isBackedUp())
+        if (    aType == DeviceType_HardDisk
+             && mMediaData.isBackedUp())
         {
             const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
@@ -3232,5 +3233,5 @@
             if ((pAttachTemp = findAttachment(oldAtts, medium)))
             {
-                AssertReturn(!fImplicit, E_FAIL);
+                AssertReturn(!fIndirect, E_FAIL);
 
                 /* see if it's the same bus/channel/device */
@@ -3251,5 +3252,5 @@
 
         /* go further only if the attachment is to be indirect */
-        if (!fImplicit)
+        if (!fIndirect)
             break;
 
@@ -3319,5 +3320,5 @@
                     mediumLock.attach(medium);
                     /* not implicit, doesn't require association with this VM */
-                    fImplicit = false;
+                    fIndirect = false;
                     associate = false;
                     /* go right to the MediumAttachment creation */
@@ -3342,5 +3343,5 @@
                 const MediaData::AttachmentList &snapAtts = snap->getSnapshotMachine()->mMediaData->mAttachments;
 
-                MediaData::AttachmentList::const_iterator foundIt = snapAtts.end();
+                MediumAttachment *pAttachFound = NULL;
                 uint32_t foundLevel = 0;
 
@@ -3363,17 +3364,17 @@
                          * descendant of medium will be used
                          */
-                        if (    (*it)->getDevice() == aDevice
-                             && (*it)->getPort() == aControllerPort
-                             && (*it)->getControllerName() == aControllerName
+                        if (    pAttach->getDevice() == aDevice
+                             && pAttach->getPort() == aControllerPort
+                             && pAttach->getControllerName() == aControllerName
                            )
                         {
-                            foundIt = it;
+                            pAttachFound = pAttach;
                             break;
                         }
-                        else if (    foundIt == snapAtts.end()
+                        else if (    !pAttachFound
                                   || level > foundLevel /* prefer younger */
                                 )
                         {
-                            foundIt = it;
+                            pAttachFound = pAttach;
                             foundLevel = level;
                         }
@@ -3381,7 +3382,7 @@
                 }
 
-                if (foundIt != snapAtts.end())
+                if (pAttachFound)
                 {
-                    base = (*foundIt)->getMedium();
+                    base = pAttachFound->getMedium();
                     break;
                 }
@@ -3469,5 +3470,5 @@
                           aDevice,
                           aType,
-                          fImplicit,
+                          fIndirect,
                           0 /* No bandwidth limit */);
     if (FAILED(rc)) return rc;
