Skip to content

Data.Text.isSingleton accesses out of bounds memory for zero length input #674

@sol

Description

@sol

While reading some unrelated code I got distracted by this:

text/src/Data/Text.hs

Lines 634 to 638 in 1be3f68

-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
isSingleton :: Text -> Bool
isSingleton (Text arr off len) =
len == utf8LengthByLeader (A.unsafeIndex arr off)
{-# INLINE isSingleton #-}

(introduced in acf18b6)

Now it's currently only used in places where the input can never be zero length + it's not exported. I still see the risk that someone will use this without being aware of the unsafe nature at some point (or even worse, export it).

So maybe:

  • rename this to unsafeIsSingleton?
  • remove the Haddocks to emphasize that this is an internal function
  • maybe even add a comment that states that the input must be non-zero length, but then again it's pretty obvious from the implementation, so maybe skip that

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions