Skip to content

fix: handle(_:) がパスの短い URL で Index out of range になる問題を修正 - #11

Merged
GOTO-TSL merged 2 commits into
studyplus:mainfrom
masaru87:fix-handle-index-out-of-range
Aug 5, 2026
Merged

fix: handle(_:) がパスの短い URL で Index out of range になる問題を修正#11
GOTO-TSL merged 2 commits into
studyplus:mainfrom
masaru87:fix-handle-index-out-of-range

Conversation

@masaru87

Copy link
Copy Markdown
Contributor

Studyplus.handle(_:) に、パスの要素数が足りない URL を渡すと Index out of range でクラッシュする箇所がありました。

再現

isAcceptableURL(url:)pathComponents.isEmpty のみを確認していますが、handle(_:)pathComponents[1] を、success の場合はさらに pathComponents[2] を読みます。そのため要素数が 1 または 2 の URL が通過してしまいます。

URL pathComponents 結果
studyplus-{key}://auth-result [] 弾かれる(問題なし)
studyplus-{key}://auth-result/ ["/"] pathComponents[1] でクラッシュ
studyplus-{key}://auth-result/success ["/", "success"] pathComponents[2] でクラッシュ
studyplus-{key}://auth-result/success/{token} ["/", "success", "{token}"] 正常

README の案内どおり AppDelegate で受け取った URL をそのまま handle(_:) に渡す使い方だと、カスタム URL スキームは他のアプリからも開けるため、上のような URL を渡されるとアプリが終了してしまいます。

変更内容

  • isAcceptableURL(url:)pathComponents.count < 2 を弾くようにしました(handle(_:)[1] を読むため)
  • case "success"pathComponents.count >= 3 を確認し、足りない場合は他の受け付けられない URL と同じく studyplusLoginFail(error: .unknownUrl(url)) を呼んで false を返すようにしました

確認したこと

手元に Xcode 環境がないため、handle(_:)isAcceptableURL(url:) のロジックをそのまま書き写したものを Swift 6.3 (macOS) で実行して確認しました。

  • 変更前: 上の表の 2 ケースで Fatal error: Index out of range(SIGTRAP で終了)
  • 変更後: 同じ 2 ケースが .unknownUrlfalse を返します。success/{token}failcancel の 3 ケースは変更前後で挙動が変わりません
  • リポジトリの .swiftlint.ymlswiftlint lint を実行し、新たな警告が出ないことも確認しています

テストターゲットが定義されていないようでしたので、この PR にはテストを追加していません。追加した方がよければ対応します。

isAcceptableURL(url:) は pathComponents.isEmpty のみを確認していたが、
handle(_:) は pathComponents[1] と、success の場合は pathComponents[2] を
読むため、要素数が 1 または 2 の URL でクラッシュしていた。
Comment thread Lib/StudyplusSDK/Studyplus.swift Outdated
Comment on lines 193 to 195
if url.pathComponents.count < 2 {
return false
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正ありがとうございます、記述だけ揃えてもらえれば内容は問題なさそうです

Suggested change
if url.pathComponents.count < 2 {
return false
}
guard url.pathComponents.count >= 2 {
return false
}

@masaru87

masaru87 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

ご確認ありがとうございます。guard に揃えました。

guardelse が必須なので、同ファイルの他の条件と同じ 1 行の guard ... else { return false } の形にしています。

@GOTO-TSL
GOTO-TSL merged commit 22b6e62 into studyplus:main Aug 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants