Skip to content

Commit 21fda4e

Browse files
committed
[Basic] Ensure empty NullTerminatedStringRef have valid pointer
NullterminatedStringRef is meant to be safe for reading 'data()' as a valid C-string. rdar://108215490
1 parent 1960739 commit 21fda4e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

include/swift/Basic/StringExtras.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class NullTerminatedStringRef {
490490

491491
/// Create a null-terminated string, copying \p Str into \p A .
492492
template <typename Allocator>
493-
NullTerminatedStringRef(StringRef Str, Allocator &A) : Ref() {
493+
NullTerminatedStringRef(StringRef Str, Allocator &A) : Ref("") {
494494
if (Str.empty())
495495
return;
496496

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ Expr *swift::expandMacroExpr(
677677
return nullptr;
678678

679679
// Evaluate the macro.
680+
llvm::BumpPtrAllocator allocator;
680681
NullTerminatedStringRef evaluatedSource;
681682

682683
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl());
@@ -722,7 +723,6 @@ Expr *swift::expandMacroExpr(
722723
// Expand the definition with the given arguments.
723724
auto result = expandMacroDefinition(
724725
macroDef.getExpanded(), macro, expr->getArgs());
725-
llvm::MallocAllocator allocator;
726726
evaluatedSource = NullTerminatedStringRef(result, allocator);
727727
break;
728728
}
@@ -866,6 +866,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
866866
return None;
867867

868868
// Evaluate the macro.
869+
llvm::BumpPtrAllocator allocator;
869870
NullTerminatedStringRef evaluatedSource;
870871

871872
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef().getDecl());
@@ -899,7 +900,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
899900
// Expand the definition with the given arguments.
900901
auto result = expandMacroDefinition(
901902
macroDef.getExpanded(), macro, med->getArgs());
902-
llvm::MallocAllocator allocator;
903903
evaluatedSource = NullTerminatedStringRef(result, allocator);
904904
break;
905905
}
@@ -1092,6 +1092,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
10921092
}
10931093

10941094
// Evaluate the macro.
1095+
llvm::BumpPtrAllocator allocator;
10951096
NullTerminatedStringRef evaluatedSource;
10961097

10971098
std::string discriminator;
@@ -1114,7 +1115,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
11141115
// Expand the definition with the given arguments.
11151116
auto result = expandMacroDefinition(
11161117
macroDef.getExpanded(), macro, attr->getArgs());
1117-
llvm::MallocAllocator allocator;
11181118
evaluatedSource = NullTerminatedStringRef(result, allocator);
11191119
break;
11201120
}

0 commit comments

Comments
 (0)