Uploaded image for project: 'Swift'
  1. Swift
  2. SR-11292

A Character.init doesn't trap like it should iff using -O

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Standard Library
    • Labels:
      None
    • Environment:

      Xcode 10.3 (10G8)
      macOS 10.14.6 (18G87)

      Description

      See this forum post:
      https://forums.swift.org/t/is-this-a-character-bug/27622/8
      and the attached Xcode project.

      (EDIT: Never mind the attached project, the bug is easily demonstrated by the example in the forum post. I've now deleted the attachment to prevent unnecessary confusion.)

      In short, if compiling with optimizations enabled, the following Character initializer will not trap (as it should) when the given string is invalid (ie the string does not hold exactly one extended grapheme cluster).

          /// Creates a character from a single-character string.
          ///
          /// The following example creates a new character from the uppercase version
          /// of a string that only holds one character.
          ///
          ///     let a = "a"
          ///     let capitalA = Character(a.uppercased())
          ///
          /// - Parameter s: The single-character string to convert to a `Character`
          ///   instance. `s` must contain exactly one extended grapheme cluster.
          @inlinable public init(_ s: String)
      

      The result is that very strange Character instances can be created without any notice.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jens Jens Persson
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: