Skip to content

[Serialized diagnostics] Avoid generating filenames that break the reader #70139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/Frontend/SerializedDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ namespace serialized_diagnostics {
} // namespace serialized_diagnostics
} // namespace swift

/// Sanitize a filename for the purposes of the serialized diagnostics reader.
static StringRef sanitizeFilename(
StringRef filename, SmallString<32> &scratch) {
if (!filename.endswith("/") && !filename.endswith("\\"))
return filename;

scratch = filename;
scratch += "_operator";
return scratch;
}

unsigned SerializedDiagnosticConsumer::getEmitFile(
SourceManager &SM, StringRef Filename, unsigned bufferID
) {
Expand All @@ -248,9 +259,14 @@ unsigned SerializedDiagnosticConsumer::getEmitFile(
Record.push_back(entry);
Record.push_back(0); // For legacy.
Record.push_back(0); // For legacy.
Record.push_back(Filename.size());

// Sanitize the filename enough that the serialized diagnostics reader won't
// reject it.
SmallString<32> filenameScratch;
auto sanitizedFilename = sanitizeFilename(Filename, filenameScratch);
Record.push_back(sanitizedFilename.size());
State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME),
Record, Filename.data());
Record, sanitizedFilename.data());

// If the buffer contains code that was synthesized by the compiler,
// emit the contents of the buffer.
Expand Down
2 changes: 2 additions & 0 deletions test/diagnostics/Inputs/slash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public func /(lhs: String, b: Int) -> Bool { false }

16 changes: 16 additions & 0 deletions test/diagnostics/bad-generated-filenames.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t/SlashA.swiftmodule %S/Inputs/slash.swift
// RUN: %target-swift-frontend -emit-module -o %t/SlashB.swiftmodule %S/Inputs/slash.swift
// RUN: not %target-swift-frontend -typecheck -I %t -serialize-diagnostics-path %t/serialized.dia %s
// RUN: c-index-test -read-diagnostics %t/serialized.dia > %t/serialized.txt 2>&1
// RUN: %FileCheck %s -check-prefix CHECK-DIA < %t/serialized.txt

import SlashA
import SlashB

func test(a: String, b: Int) -> Bool {
// CHECK-DIA: [[@LINE+3]]:5: error: ambiguous use of operator '/'
// CHECK-DIA: SlashA./_operator
// CHECK-DIA: SlashB./_operator
a / b
}