Index: trunk/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredProvCredential.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredProvCredential.cpp	(revision 46385)
+++ trunk/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredProvCredential.cpp	(revision 49722)
@@ -124,8 +124,4 @@
  * @param   fCopy                   Whether to just assign or copy the actual buffer
  *                                  contents from source -> dest.
- * @todo    r=bird: It appears that fCopy == true is never used, which is
- *          fortunate as it (a) doesn't check for there being room in the
- *          buffer, (b) terminate the string (which is customary, even if not
- *          strictly necessary), and (c) overwrites MaximumLength.
  */
 HRESULT
@@ -136,18 +132,27 @@
 
     size_t cbLen = RTUtf16Len(pwszSource) * sizeof(RTUTF16);
-    AssertReturn(cbLen >= USHORT_MAX, E_INVALIDARG);
-
-    pUnicodeDest->Length        = (USHORT)cbLen;
-    pUnicodeDest->MaximumLength = (USHORT)cbLen;
+    AssertReturn(cbLen <= USHORT_MAX, E_INVALIDARG);
+
+    HRESULT hr;
 
     if (fCopy)
     {
-        AssertFailed(/*see todo*/);
-        memcpy(pUnicodeDest->Buffer, pwszSource, cbLen);
+        if (cbLen <= pUnicodeDest->MaximumLength)
+        {
+            memcpy(pUnicodeDest->Buffer, pwszSource, cbLen);
+            pUnicodeDest->Length = (USHORT)cbLen;
+            hr = S_OK;
+        }
+        else
+            hr = E_INVALIDARG;
     }
     else /* Just assign the buffer. */
-        pUnicodeDest->Buffer    = pwszSource;
-
-    return S_OK;
+    {
+        pUnicodeDest->Buffer = pwszSource;
+        pUnicodeDest->Length = (USHORT)cbLen;
+        hr = S_OK;
+    }
+
+    return hr;
 }
 
@@ -893,92 +898,103 @@
     }
 
+    /* Fill in the username and password. */
     if (SUCCEEDED(hr))
     {
-        /* Fill in the username and password. */
+        hr = RTUTF16ToUnicode(&pKerberosLogon->UserName,
+                              m_apwszCredentials[VBOXCREDPROV_FIELDID_USERNAME],
+                              false /* Just assign, no copy */);
         if (SUCCEEDED(hr))
         {
-            hr = RTUTF16ToUnicode(&pKerberosLogon->UserName,
-                                  m_apwszCredentials[VBOXCREDPROV_FIELDID_USERNAME],
+            hr = RTUTF16ToUnicode(&pKerberosLogon->Password,
+                                  m_apwszCredentials[VBOXCREDPROV_FIELDID_PASSWORD],
                                   false /* Just assign, no copy */);
             if (SUCCEEDED(hr))
             {
-                hr = RTUTF16ToUnicode(&pKerberosLogon->Password,
-                                      m_apwszCredentials[VBOXCREDPROV_FIELDID_PASSWORD],
-                                      false /* Just assign, no copy */);
+                /* Set credential type according to current usage scenario. */
+                AssertPtr(pKerberosLogon);
+                switch (m_enmUsageScenario)
+                {
+                    case CPUS_UNLOCK_WORKSTATION:
+                        pKerberosLogon->MessageType = KerbWorkstationUnlockLogon;
+                        break;
+
+                    case CPUS_LOGON:
+                        pKerberosLogon->MessageType = KerbInteractiveLogon;
+                        break;
+
+                    case CPUS_CREDUI:
+                        pKerberosLogon->MessageType = (KERB_LOGON_SUBMIT_TYPE)0; /* No message type required here. */
+                        break;
+
+                    default:
+                        hr = E_FAIL;
+                        break;
+                }
+
+                if (FAILED(hr))
+                    VBoxCredProvVerbose(0, "VBoxCredProvCredential::GetSerialization: Unknown usage scenario=%ld\n", m_enmUsageScenario);
+
+                if (SUCCEEDED(hr)) /* Build the logon package. */
+                {
+                    hr = AllocateLogonPackage(KerberosUnlockLogon,
+                                              &pcpCredentialSerialization->rgbSerialization,
+                                              &pcpCredentialSerialization->cbSerialization);
+                    if (FAILED(hr))
+                        VBoxCredProvVerbose(0, "VBoxCredProvCredential::GetSerialization: Failed to allocate logon package, hr=0x%08x\n", hr);
+                }
+
                 if (SUCCEEDED(hr))
                 {
-                    /* Set credential type according to current usage scenario. */
-                    AssertPtr(pKerberosLogon);
-                    switch (m_enmUsageScenario)
+                    ULONG ulAuthPackage;
+
+                    HANDLE hLsa;
+                    NTSTATUS s = LsaConnectUntrusted(&hLsa);
+                    if (SUCCEEDED(HRESULT_FROM_NT(s)))
                     {
-                        case CPUS_UNLOCK_WORKSTATION:
-                            pKerberosLogon->MessageType = KerbWorkstationUnlockLogon;
-                            break;
-
-                        case CPUS_LOGON:
-                            pKerberosLogon->MessageType = KerbInteractiveLogon;
-                            break;
-
-                        case CPUS_CREDUI:
-                            pKerberosLogon->MessageType = (KERB_LOGON_SUBMIT_TYPE)0; /* No message type required here. */
-                            break;
-
-                        default:
-                            hr = E_FAIL;
-                            break;
+                        LSA_STRING lsaszKerberosName;
+                        size_t cchKerberosName;
+                        hr = StringCchLengthA(NEGOSSP_NAME_A, USHORT_MAX, &cchKerberosName);
+                        if (SUCCEEDED(hr))
+                        {
+                            USHORT usLength;
+                            hr = SizeTToUShort(cchKerberosName, &usLength);
+                            if (SUCCEEDED(hr))
+                            {
+                                lsaszKerberosName.Buffer        = (PCHAR)NEGOSSP_NAME_A;
+                                lsaszKerberosName.Length        = usLength;
+                                lsaszKerberosName.MaximumLength = lsaszKerberosName.Length + 1;
+
+                            }
+                        }
+
+                        if (SUCCEEDED(hr))
+                        {
+                            s = LsaLookupAuthenticationPackage(hLsa, &lsaszKerberosName,
+                                                               &ulAuthPackage);
+                            if (FAILED(HRESULT_FROM_NT(s)))
+                            {
+                                hr = HRESULT_FROM_NT(s);
+                                VBoxCredProvVerbose(0, "VBoxCredProvCredential::GetSerialization: Failed looking up authentication package, hr=0x%08x\n", hr);
+                            }
+                        }
+
+                        LsaDeregisterLogonProcess(hLsa);
                     }
-
-                    if (SUCCEEDED(hr)) /* Build the logon package. */
-                        hr = AllocateLogonPackage(KerberosUnlockLogon,
-                                                  &pcpCredentialSerialization->rgbSerialization,
-                                                  &pcpCredentialSerialization->cbSerialization);
 
                     if (SUCCEEDED(hr))
                     {
-                        ULONG ulAuthPackage;
-
-                        HANDLE hLsa;
-                        NTSTATUS s = LsaConnectUntrusted(&hLsa);
-                        if (SUCCEEDED(HRESULT_FROM_NT(s)))
-                        {
-                            LSA_STRING lsaszKerberosName;
-                            size_t cchKerberosName;
-                            hr = StringCchLengthA(NEGOSSP_NAME_A, USHORT_MAX, &cchKerberosName);
-                            if (SUCCEEDED(hr))
-                            {
-                                USHORT usLength;
-                                hr = SizeTToUShort(cchKerberosName, &usLength);
-                                if (SUCCEEDED(hr))
-                                {
-                                    lsaszKerberosName.Buffer        = (PCHAR)NEGOSSP_NAME_A;
-                                    lsaszKerberosName.Length        = usLength;
-                                    lsaszKerberosName.MaximumLength = lsaszKerberosName.Length + 1;
-
-                                }
-                            }
-
-                            if (SUCCEEDED(hr))
-                            {
-                                s = LsaLookupAuthenticationPackage(hLsa, &lsaszKerberosName,
-                                                                   &ulAuthPackage);
-                                if (FAILED(HRESULT_FROM_NT(s)))
-                                    hr = HRESULT_FROM_NT(s);
-                            }
-
-                            LsaDeregisterLogonProcess(hLsa);
-                        }
-
-                        if (SUCCEEDED(hr))
-                        {
-                            pcpCredentialSerialization->ulAuthenticationPackage = ulAuthPackage;
-                            pcpCredentialSerialization->clsidCredentialProvider = CLSID_VBoxCredProvider;
-
-                            /* We're done -- let the logon UI know. */
-                            *pcpGetSerializationResponse = CPGSR_RETURN_CREDENTIAL_FINISHED;
-                        }
+                        pcpCredentialSerialization->ulAuthenticationPackage = ulAuthPackage;
+                        pcpCredentialSerialization->clsidCredentialProvider = CLSID_VBoxCredProvider;
+
+                        /* We're done -- let the logon UI know. */
+                        *pcpGetSerializationResponse = CPGSR_RETURN_CREDENTIAL_FINISHED;
                     }
                 }
             }
-        }
+            else
+                VBoxCredProvVerbose(0, "VBoxCredProvCredential::GetSerialization: Error copying password, hr=0x%08x\n", hr);
+        }
+        else
+            VBoxCredProvVerbose(0, "VBoxCredProvCredential::GetSerialization: Error copying user name, hr=0x%08x\n", hr);
     }
 
