Skip to content

Don't crash when saving an attachment whose name is only periods. - #2002

Open
ravindudanthanarayana wants to merge 3 commits into
swiftlang:mainfrom
ravindudanthanarayana:fix-attachment-name-of-only-periods
Open

ravindudanthanarayana wants to merge 3 commits into
swiftlang:mainfrom
ravindudanthanarayana:fix-attachment-name-of-only-periods

Conversation

@ravindudanthanarayana

@ravindudanthanarayana ravindudanthanarayana commented Nov 1, 2026 •

Copy link
Copy Markdown

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) and split(separator: ".") returns an empty array, so preferredNameComponents[0] traps:

Swift/ContiguousArrayBuffer.swift:695: Fatal error: Index out of range

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-path set, or with a custom preferredName(for:basedOn:) implementation that returns "".

Modifications:

  • If splitting the preferred name produces no components, use defaultPreferredName ("untitled") as the base name. This matches how the function already falls back to the default name for other unusable names.
  • Added ".", ".." and "..." to the arguments of writeAttachmentWithBadName(name:).
  • Added writeAttachmentWithEmptyPreferredName(), which uses a custom Attachable type whose preferredName(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 of writeAttachmentWithBadName). 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-assisted label.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

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.

@grynspan grynspan left a comment

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.

Okay, fair enough. I suggested a simpler change.

Is this an issue you actually ran into in production?

@grynspan grynspan added bug 🪲 Something isn't working attachments/activities 🖇️ Work related to attachments and/or activities ai-assisted 🤖 This issue or pull request was created (in whole or in part) by an AI or LLM. labels Nov 1, 2026
@grynspan grynspan added this to the Swift 6.5.0 (main) milestone Nov 1, 2026
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.
@ravindudanthanarayana

Copy link
Copy Markdown
Author

Okay, fair enough. I suggested a simpler change.

Is this an issue you actually ran into in production?

@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 Attachment.record(value, named: "..") and --attachments-path set.

It's definitely an edge case, so I understand if it's not a high priority.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted 🤖 This issue or pull request was created (in whole or in part) by an AI or LLM. attachments/activities 🖇️ Work related to attachments and/or activities bug 🪲 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants