Avoid throwing in the globalization managed callbacks (dotnet/coreclr#11454)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Tue, 9 May 2017 01:03:05 +0000 (18:03 -0700)
committerGitHub <noreply@github.com>
Tue, 9 May 2017 01:03:05 +0000 (18:03 -0700)
* Avoid throwing in the globalization managed callbacks

* Add Assert

Commit migrated from https://github.com/dotnet/coreclr/commit/eac27ca7fb6e80a0a1af44b889469b2818c45a9a

src/coreclr/src/mscorlib/shared/System/Globalization/CalendarData.Unix.cs

index 319f66a..a2ceeb1 100644 (file)
@@ -306,21 +306,30 @@ namespace System.Globalization
 
         private static void EnumCalendarInfoCallback(string calendarString, IntPtr context)
         {
-            CallbackContext callbackContext = (CallbackContext)((GCHandle)context).Target;
-
-            if (callbackContext.DisallowDuplicates)
+            try
             {
-                foreach (string existingResult in callbackContext.Results)
+                CallbackContext callbackContext = (CallbackContext)((GCHandle)context).Target;
+
+                if (callbackContext.DisallowDuplicates)
                 {
-                    if (string.Equals(calendarString, existingResult, StringComparison.Ordinal))
+                    foreach (string existingResult in callbackContext.Results)
                     {
-                        // the value is already in the results, so don't add it again
-                        return;
+                        if (string.Equals(calendarString, existingResult, StringComparison.Ordinal))
+                        {
+                            // the value is already in the results, so don't add it again
+                            return;
+                        }
                     }
                 }
-            }
 
-            callbackContext.Results.Add(calendarString);
+                callbackContext.Results.Add(calendarString);
+            }
+            catch (Exception e)
+            {
+                Debug.Assert(false, e.ToString());
+                // we ignore the managed exceptions here because EnumCalendarInfoCallback will get called from the native code.
+                // If we don't ignore the exception here that can cause the runtime to fail fast.
+            }
         }
 
         private class CallbackContext