Title: [207397] trunk/Source/WebCore
Revision
207397
Author
cdu...@apple.com
Date
2016-10-16 18:28:51 -0700 (Sun, 16 Oct 2016)

Log Message

REGRESSION (r206750): Crash when pressing Caps Lock if “Use the Caps Lock key to switch to and from U.S.” is selected in Input Sources preferences
https://bugs.webkit.org/show_bug.cgi?id=163506
<rdar://problem/28792483>

Reviewed by Darin Adler.

As per the NSEvent documentation [1], calling [NSEvent characters] is only
valid on key up / key down events and will raise an NSInternalInconsistencyException
if accessed on any other kind of event object. The crash happens when keyForKeyEvent()
is called with the third kind of key event (NSFlagsChanged) which is used for
detecting modifier keys. We normally detect the modifier key and return early before
calling [NSEvent characters]. However, in some rare cases, we fail to detect the
modifier key and we fall through.

To address the issue, we now return "Unidentified" for NSFlagsChanged events, if we
fail to detect the modifier key and before calling [NSEvent characters].

[1] https://developer.apple.com/reference/appkit/nsevent/1534183-characters

No new test, not easily testable.

* platform/mac/PlatformEventFactoryMac.mm:
(WebCore::keyForKeyEvent):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (207396 => 207397)


--- trunk/Source/WebCore/ChangeLog	2016-10-17 00:16:05 UTC (rev 207396)
+++ trunk/Source/WebCore/ChangeLog	2016-10-17 01:28:51 UTC (rev 207397)
@@ -1,3 +1,29 @@
+2016-10-16  Chris Dumez  <cdu...@apple.com>
+
+        REGRESSION (r206750): Crash when pressing Caps Lock if “Use the Caps Lock key to switch to and from U.S.” is selected in Input Sources preferences
+        https://bugs.webkit.org/show_bug.cgi?id=163506
+        <rdar://problem/28792483>
+
+        Reviewed by Darin Adler.
+
+        As per the NSEvent documentation [1], calling [NSEvent characters] is only
+        valid on key up / key down events and will raise an NSInternalInconsistencyException
+        if accessed on any other kind of event object. The crash happens when keyForKeyEvent()
+        is called with the third kind of key event (NSFlagsChanged) which is used for
+        detecting modifier keys. We normally detect the modifier key and return early before
+        calling [NSEvent characters]. However, in some rare cases, we fail to detect the
+        modifier key and we fall through.
+
+        To address the issue, we now return "Unidentified" for NSFlagsChanged events, if we
+        fail to detect the modifier key and before calling [NSEvent characters].
+
+        [1] https://developer.apple.com/reference/appkit/nsevent/1534183-characters
+
+        No new test, not easily testable.
+
+        * platform/mac/PlatformEventFactoryMac.mm:
+        (WebCore::keyForKeyEvent):
+
 2016-10-16  Darin Adler  <da...@apple.com>
 
         Move CSS classes from ExceptionCode to Exception

Modified: trunk/Source/WebCore/platform/mac/PlatformEventFactoryMac.mm (207396 => 207397)


--- trunk/Source/WebCore/platform/mac/PlatformEventFactoryMac.mm	2016-10-17 00:16:05 UTC (rev 207396)
+++ trunk/Source/WebCore/platform/mac/PlatformEventFactoryMac.mm	2016-10-17 01:28:51 UTC (rev 207397)
@@ -36,6 +36,7 @@
 #import <HIToolbox/Events.h>
 #import <mach/mach_time.h>
 #import <wtf/ASCIICType.h>
+#import <wtf/mac/AppKitCompatibilityDeclarations.h>
 
 namespace WebCore {
 
@@ -221,11 +222,7 @@
 
 static inline String textFromEvent(NSEvent* event)
 {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
     if ([event type] == NSEventTypeFlagsChanged)
-#else
-    if ([event type] == NSFlagsChanged)
-#endif
         return emptyString();
     return String([event characters]);
 }
@@ -232,11 +229,7 @@
 
 static inline String unmodifiedTextFromEvent(NSEvent* event)
 {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
     if ([event type] == NSEventTypeFlagsChanged)
-#else
-    if ([event type] == NSFlagsChanged)
-#endif
         return emptyString();
     return String([event charactersIgnoringModifiers]);
 }
@@ -264,16 +257,18 @@
         return ASCIILiteral("Control");
     }
 
+    // If the event is an NSEventTypeFlagsChanged events and we have not returned yet then this means we could not
+    // identify the modifier key. We return now and report the key as "Unidentified".
+    // Note that [event characters] below raises an exception if called on an NSEventTypeFlagsChanged event.
+    if ([event type] == NSEventTypeFlagsChanged)
+        return ASCIILiteral("Unidentified");
+
     // If more than one key is being pressed and the key combination includes one or more modifier keys
     // that result in the key no longer producing a printable character (e.g., Control + a), then the
     // key value should be the printable key value that would have been produced if the key had been
     // typed with the default keyboard layout with no modifier keys except for Shift and AltGr applied.
     // https://w3c.github.io/uievents/#keys-guidelines
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
     bool isControlDown = ([event modifierFlags] & NSEventModifierFlagControl);
-#else
-    bool isControlDown = ([event modifierFlags] & NSControlKeyMask);
-#endif
     NSString *s = isControlDown ? [event charactersIgnoringModifiers] : [event characters];
     auto length = [s length];
     // characters / charactersIgnoringModifiers return an empty string for dead keys.
@@ -500,11 +495,7 @@
 
 String keyIdentifierForKeyEvent(NSEvent* event)
 {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
     if ([event type] == NSEventTypeFlagsChanged) {
-#else
-    if ([event type] == NSFlagsChanged) {
-#endif
         switch ([event keyCode]) {
             case 54: // Right Command
             case 55: // Left Command
@@ -692,7 +683,6 @@
 {
     OptionSet<PlatformEvent::Modifier> modifiers;
 
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
     if (event.modifierFlags & NSEventModifierFlagShift)
         modifiers |= PlatformEvent::Modifier::ShiftKey;
     if (event.modifierFlags & NSEventModifierFlagControl)
@@ -703,18 +693,6 @@
         modifiers |= PlatformEvent::Modifier::MetaKey;
     if (event.modifierFlags & NSEventModifierFlagCapsLock)
         modifiers |= PlatformEvent::Modifier::CapsLockKey;
-#else
-    if (event.modifierFlags & NSShiftKeyMask)
-        modifiers |= PlatformEvent::Modifier::ShiftKey;
-    if (event.modifierFlags & NSControlKeyMask)
-        modifiers |= PlatformEvent::Modifier::CtrlKey;
-    if (event.modifierFlags & NSAlternateKeyMask)
-        modifiers |= PlatformEvent::Modifier::AltKey;
-    if (event.modifierFlags & NSCommandKeyMask)
-        modifiers |= PlatformEvent::Modifier::MetaKey;
-    if (event.modifierFlags & NSAlphaShiftKeyMask)
-        modifiers |= PlatformEvent::Modifier::CapsLockKey;
-#endif
 
     return modifiers;
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to