wordpress-mobile / wordpress-mobile/AztecEditor-iOS

`paragraphRange` signature does not specify the returning type could be nullable, leading to unexpected crashes

Open
#1,378 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

[Type] Bug
Dominant language
Swift
Stars
669
Forks
153
Avg merge
11h 27m
Merged PRs (30d)
4

Description

We've seen some crashes in WooCommerce for iOS coming from our AztecEditor wrapper. This seems to come from the underlying usage of paragraphRange, since assumes that the attachment passed into the method is the string, force-unwrapping the first result of the attachments array:

    func paragraphRange(for attachment: NSTextAttachment) -> NSRange {
        // We assume the attachment IS in the string.  This method should not be called otherwise.
        let attachmentRange = ranges(forAttachment: attachment).first!
        
        return paragraphRange(for: attachmentRange)
    }

This leads to unexpected crashes when the method is not used as intended, since its signature does not specify that the result could be nil. Ideally we should change the returning type to NSRange?, or handle the existence of nil values internally.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Aztec/Classes/Extensions/NSAttributedString+ParagraphRange.swift at paragraphRange(for: NSTextAttachment), especially the ranges(forAttachment:) call. Search its callers to understand how the return value is used, then verify the attachment-not-found path no longer causes an unexpected crash and that affected callers handle the result appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.