Repository navigation
Don't crash when saving an attachment whose name is only periods. - #2002
Open
ravindudanthanarayana wants to merge 3 commits into
Open
ravindudanthanarayana wants to merge 3 commits into
ravindudanthanarayana wants to merge 3 commits into
Conversation
If an attachment's preferred name is empty or consists only of periods
(e.g. "." or ".."), the first attempt to write it fails and the fallback
path splits the name on "." to add a suffix. The resulting array is
empty, so indexing its first element traps and crashes the test process.
This change falls back to the default preferred name ("untitled") in
that case, matching how other unusable names are already handled.
ravindudanthanarayana
marked this pull request as ready for review
November 1, 2026 04:22
ravindudanthanarayana
requested review from
briancroom,
grynspan,
harlanhaskins,
jerryjrchen and
stmontgomery
as code owners
November 1, 2026 04:22
grynspan
requested changes
Nov 1, 2026
grynspan
left a comment
Contributor
There was a problem hiding this comment.
Okay, fair enough. I suggested a simpler change.
Is this an issue you actually ran into in production?
Passing omittingEmptySubsequences: false means a name made up only of periods splits into empty components rather than an empty array, so it no longer needs special handling. An empty name splits into a single empty component, which would produce a file name with no base name, so continue to use the default preferred name in that case.
Author
@grynspan Thanks for taking a look :) No, I didn't run into this in production... I found it while reading through the code and was able to reproduce the crash with It's definitely an edge case, so I understand if it's not a high priority. |
grynspan
reviewed
Nov 1, 2026
If the name splits into something invalid, opening the file will fail later and we'll fall back to the default name. If it splits into something valid, it's okay to save the attachment. All this code needs to do is avoid the crash, which omittingEmptySubsequences: false already does. Update the test for an empty name to check the saved file's contents rather than its name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Don't crash when saving an attachment whose name is empty or only periods.
Motivation:
Attachment.write(toFileInDirectoryAtPath:)first tries to write to the preferred name verbatim. If that fails, it splits the name on"."and adds a suffix to the first component. When the preferred name is empty or made up only of periods (e.g.".","..","..."), the first write fails (the path names the directory itself or its parent) andsplit(separator: ".")returns an empty array, sopreferredNameComponents[0]traps:This crashes the whole test process, not just the test that recorded the attachment. It can be reached through public API, e.g.
Attachment.record(value, named: "..")with--attachments-pathset, or with a custompreferredName(for:basedOn:)implementation that returns"".Modifications:
defaultPreferredName("untitled") as the base name. This matches how the function already falls back to the default name for other unusable names.".",".."and"..."to the arguments ofwriteAttachmentWithBadName(name:).writeAttachmentWithEmptyPreferredName(), which uses a customAttachabletype whosepreferredName(for:basedOn:)returns"".Both tests crash with the error above without the fix and pass with it.
Testing:
On macOS 27.0 (arm64), Apple Swift 6.4 (swiftlang-6.4.0.34.1):
swift test --filter writeAttachment: passes (5 tests, including 7 cases ofwriteAttachmentWithBadName). Without the source change, it exits with signal 5 (Fatal error: Index out of range).swift test: passes (Swift Testing: 649 tests in 94 suites, plus 44 tests in 5 suites; XCTest: 132 tests, 0 failures).Only tested on macOS. The changed code has no platform-specific branches.
AI disclosure:
I used an AI coding assistant (Claude Code) to investigate, write and test this change. I reviewed the change and the test results. Please feel free to apply the
ai-assistedlabel.Checklist: