Forráskód Böngészése

Provide more context to error reports for frequent access violation when reading the clipboard

Source commit: afb5cc98a993c9c8d5f99460c663148feb8b9c11
Martin Prikryl 5 éve
szülő
commit
da0392671b
3 módosított fájl, 95 hozzáadás és 35 törlés
  1. 24 1
      source/core/Exceptions.cpp
  2. 1 0
      source/core/Exceptions.h
  3. 70 34
      source/windows/Tools.cpp

+ 24 - 1
source/core/Exceptions.cpp

@@ -59,7 +59,13 @@ static bool __fastcall WellKnownException(
     {
       throw EAccessViolation(E->Message);
     }
-    Message = MainInstructions(LoadStr(ACCESS_VIOLATION_ERROR3));
+    UnicodeString S = E->Message;
+    UnicodeString Dummy;
+    if (!ExtractMainInstructions(S, Dummy))
+    {
+      S = UnicodeString();
+    }
+    Message = MainInstructions(LoadStr(ACCESS_VIOLATION_ERROR3)) + S;
     CounterName = L"AccessViolations";
     Clone.reset(new EAccessViolation(E->Message));
   }
@@ -522,3 +528,20 @@ void __fastcall RethrowException(Exception * E)
     throw ExtException(E, L"");
   }
 }
+//---------------------------------------------------------------------------
+UnicodeString __fastcall AddContextToExceptionMessage(const Exception & E, const UnicodeString & NewContext)
+{
+  UnicodeString MainMessage;
+  UnicodeString Context = E.Message;
+  if (!ExtractMainInstructions(Context, MainMessage))
+  {
+    MainMessage = E.Message;
+    Context = NewContext;
+  }
+  else
+  {
+    Context += L"\n" + NewContext;
+  }
+  UnicodeString Result = MainInstructions(MainMessage) + Context;
+  return Result;
+}

+ 1 - 0
source/core/Exceptions.h

@@ -215,5 +215,6 @@ void __fastcall RethrowException(Exception * E);
 UnicodeString __fastcall GetExceptionHelpKeyword(Exception * E);
 UnicodeString __fastcall MergeHelpKeyword(const UnicodeString & PrimaryHelpKeyword, const UnicodeString & SecondaryHelpKeyword);
 bool __fastcall IsInternalErrorHelpKeyword(const UnicodeString & HelpKeyword);
+UnicodeString __fastcall AddContextToExceptionMessage(const Exception & E, const UnicodeString & NewContext);
 //---------------------------------------------------------------------------
 #endif  // Exceptions

+ 70 - 34
source/windows/Tools.cpp

@@ -796,58 +796,94 @@ bool __fastcall IsFormatInClipboard(unsigned int Format)
 //---------------------------------------------------------------------------
 HANDLE __fastcall OpenTextFromClipboard(const wchar_t *& Text)
 {
-  HANDLE Result = NULL;
-  if (OpenClipboard(0))
+  UnicodeString ErrorContext;
+  try
   {
-    // Check also for CF_TEXT?
-    Result = GetClipboardData(CF_UNICODETEXT);
-    if (Result != NULL)
-    {
-      Text = static_cast<const wchar_t*>(GlobalLock(Result));
-    }
-    else
+    HANDLE Result = NULL;
+    ErrorContext = L"open";
+    if (OpenClipboard(0))
     {
-      CloseClipboard();
+      // Check also for CF_TEXT?
+      ErrorContext = L"getdata";
+      Result = GetClipboardData(CF_UNICODETEXT);
+      if (Result != NULL)
+      {
+        ErrorContext = L"lock";
+        Text = static_cast<const wchar_t*>(GlobalLock(Result));
+      }
+      else
+      {
+        ErrorContext = L"close";
+        CloseClipboard();
+      }
     }
+    return Result;
+  }
+  catch (EAccessViolation & E)
+  {
+    throw EAccessViolation(AddContextToExceptionMessage(E, ErrorContext));
   }
-  return Result;
 }
 //---------------------------------------------------------------------------
 void __fastcall CloseTextFromClipboard(HANDLE Handle)
 {
-  if (Handle != NULL)
+  UnicodeString ErrorContext;
+  try
+  {
+    if (Handle != NULL)
+    {
+      ErrorContext = "unlock";
+      GlobalUnlock(Handle);
+    }
+    ErrorContext = "close";
+    CloseClipboard();
+  }
+  catch (EAccessViolation & E)
   {
-    GlobalUnlock(Handle);
+    throw EAccessViolation(AddContextToExceptionMessage(E, ErrorContext));
   }
-  CloseClipboard();
 }
 //---------------------------------------------------------------------------
 bool __fastcall TextFromClipboard(UnicodeString & Text, bool Trim)
 {
-  const wchar_t * AText = NULL;
-  HANDLE Handle = OpenTextFromClipboard(AText);
-  bool Result = (Handle != NULL);
-  if (Result)
+  UnicodeString ErrorContext;
+  try
   {
-    // For all current uses (URL pasting, key/fingerprint pasting, known_hosts pasting, "more messages" copying,
-    // permissions pasting), 64KB is large enough.
-    const size_t Limit = 64*1024;
-    size_t Size = GlobalSize(Handle);
-    if (Size > Limit)
-    {
-      Text = UnicodeString(AText, Limit);
-    }
-    else
-    {
-      Text = AText;
-    }
-    if (Trim)
+    const wchar_t * AText = NULL;
+    ErrorContext = L"open";
+    HANDLE Handle = OpenTextFromClipboard(AText);
+    bool Result = (Handle != NULL);
+    if (Result)
     {
-      Text = Text.Trim();
+      // For all current uses (URL pasting, key/fingerprint pasting, known_hosts pasting, "more messages" copying,
+      // permissions pasting), 64KB is large enough.
+      const size_t Limit = 64*1024;
+      ErrorContext = L"size";
+      size_t Size = GlobalSize(Handle);
+      if (Size > Limit)
+      {
+        ErrorContext = FORMAT(L"substring(%d)", (int(Size)));
+        Text = UnicodeString(AText, Limit);
+      }
+      else
+      {
+        ErrorContext = FORMAT(L"string(%d)", (int(Size)));
+        Text = AText;
+      }
+      if (Trim)
+      {
+        ErrorContext = L"trim";
+        Text = Text.Trim();
+      }
+      ErrorContext = L"close";
+      CloseTextFromClipboard(Handle);
     }
-    CloseTextFromClipboard(Handle);
+    return Result;
+  }
+  catch (EAccessViolation & E)
+  {
+    throw EAccessViolation(AddContextToExceptionMessage(E, ErrorContext));
   }
-  return Result;
 }
 //---------------------------------------------------------------------------
 bool __fastcall NonEmptyTextFromClipboard(UnicodeString & Text)