OSDN Git Service

Check the return value of GlobalLock()
authorStarg <starg@users.osdn.me>
Thu, 12 Aug 2021 12:15:14 +0000 (21:15 +0900)
committerStarg <starg@users.osdn.me>
Thu, 12 Aug 2021 12:15:14 +0000 (21:15 +0900)
interface/w32g_new_console.cpp

index c1ea590..1128ef2 100644 (file)
@@ -74,21 +74,29 @@ bool CopyTextToClipboard(TStringView text)
         if (hGlobal)
         {
             auto p = reinterpret_cast<LPTSTR>(::GlobalLock(hGlobal));
-            text.copy(p, text.size());
-            p[text.size()] = _T('\0');
-            ::GlobalUnlock(hGlobal);
 
-            ::EmptyClipboard();
+            if (p)
+            {
+                text.copy(p, text.size());
+                p[text.size()] = _T('\0');
+                ::GlobalUnlock(hGlobal);
+
+                ::EmptyClipboard();
 
 #ifdef UNICODE
-            UINT format = CF_UNICODETEXT;
+                UINT format = CF_UNICODETEXT;
 #else
-            UINT format = CF_TEXT;
+                UINT format = CF_TEXT;
 #endif
 
-            if (::SetClipboardData(format, hGlobal))
-            {
-                ret = true;
+                if (::SetClipboardData(format, hGlobal))
+                {
+                    ret = true;
+                }
+                else
+                {
+                    ::GlobalFree(hGlobal);
+                }
             }
             else
             {