Avoid throwing in the globalization managed callbacks (#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

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