From e22dca5286031bbeef7c26041bc372fb2910b8a3 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 30 May 2018 17:47:11 +0000 Subject: [PATCH] [dsymutil] Escape HTML special characters in plist. When printing string in the Plist, we weren't escaping the characters which lead to invalid XML. This patch adds the escape logic to StringExtras. rdar://39785334 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333565 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/StringExtras.h | 4 ++++ lib/Support/StringExtras.cpp | 17 +++++++++++++++ test/tools/dsymutil/X86/darwin-bundle.test | 4 ++-- tools/dsymutil/dsymutil.cpp | 25 +++++++++++++++------- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/include/llvm/ADT/StringExtras.h b/include/llvm/ADT/StringExtras.h index 5e69016f80f..9af536c8822 100644 --- a/include/llvm/ADT/StringExtras.h +++ b/include/llvm/ADT/StringExtras.h @@ -255,6 +255,10 @@ inline StringRef getOrdinalSuffix(unsigned Val) { /// it if it is not printable or if it is an escape char. void PrintEscapedString(StringRef Name, raw_ostream &Out); +/// Print each character of the specified string, escaping HTML special +/// characters. +void PrintHTMLEscaped(StringRef String, raw_ostream &Out); + /// printLowerCase - Print each character as lowercase if it is uppercase. void printLowerCase(StringRef String, raw_ostream &Out); diff --git a/lib/Support/StringExtras.cpp b/lib/Support/StringExtras.cpp index f4933150d28..d8f8ff4f604 100644 --- a/lib/Support/StringExtras.cpp +++ b/lib/Support/StringExtras.cpp @@ -68,6 +68,23 @@ void llvm::PrintEscapedString(StringRef Name, raw_ostream &Out) { } } +void llvm::PrintHTMLEscaped(StringRef String, raw_ostream &Out) { + for (char C : String) { + if (C == '&') + Out << "&"; + else if (C == '<') + Out << "<"; + else if (C == '>') + Out << ">"; + else if (C == '\"') + Out << """; + else if (C == '\'') + Out << "'"; + else + Out << C; + } +} + void llvm::printLowerCase(StringRef String, raw_ostream &Out) { for (const char C : String) Out << toLower(C); diff --git a/test/tools/dsymutil/X86/darwin-bundle.test b/test/tools/dsymutil/X86/darwin-bundle.test index e1117807a65..7f1224f30a1 100644 --- a/test/tools/dsymutil/X86/darwin-bundle.test +++ b/test/tools/dsymutil/X86/darwin-bundle.test @@ -8,7 +8,7 @@ RUN: cat %p/../Inputs/Info.plist > %t/Info.plist RUN: dsymutil -oso-prepend-path=%p/.. %t/basic.macho.x86_64 -o %t/dsymdest/basic.macho.x86_64.dSYM RUN: FileCheck %s --input-file %t/dsymdest/basic.macho.x86_64.dSYM/Contents/Info.plist -RUN: dsymutil -oso-prepend-path=%p/.. %t/basic.macho.x86_64 -toolchain "toolchain" -o %t/dsymdest/basic.macho.x86_64.dSYM +RUN: dsymutil -oso-prepend-path=%p/.. %t/basic.macho.x86_64 -toolchain "toolchain&and'some @@ -30,6 +30,6 @@ CHECK-NEXT: 2.0 CHECK-NEXT: CFBundleVersion CHECK-NEXT: 2 TOOLCHAIN: Toolchain -TOOLCHAIN-NEXT: toolchain +TOOLCHAIN-NEXT: toolchain&and'some<symbols CHECK: CHECK-NEXT: diff --git a/tools/dsymutil/dsymutil.cpp b/tools/dsymutil/dsymutil.cpp index 7487dd9694e..899c71e02d6 100644 --- a/tools/dsymutil/dsymutil.cpp +++ b/tools/dsymutil/dsymutil.cpp @@ -18,6 +18,7 @@ #include "MachOUtils.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" #include "llvm/DebugInfo/DIContext.h" @@ -193,16 +194,24 @@ static bool createPlistFile(llvm::StringRef Bin, llvm::StringRef BundleRoot) { << "\t\tCFBundleSignature\n" << "\t\t\?\?\?\?\n"; - if (!BI.OmitShortVersion()) - PL << "\t\tCFBundleShortVersionString\n" - << "\t\t" << BI.ShortVersionStr << "\n"; + if (!BI.OmitShortVersion()) { + PL << "\t\tCFBundleShortVersionString\n"; + PL << "\t\t"; + PrintHTMLEscaped(BI.ShortVersionStr, PL); + PL << "\n"; + } - PL << "\t\tCFBundleVersion\n" - << "\t\t" << BI.VersionStr << "\n"; + PL << "\t\tCFBundleVersion\n"; + PL << "\t\t"; + PrintHTMLEscaped(BI.VersionStr, PL); + PL << "\n"; - if (!Toolchain.empty()) - PL << "\t\tToolchain\n" - << "\t\t" << Toolchain << "\n"; + if (!Toolchain.empty()) { + PL << "\t\tToolchain\n"; + PL << "\t\t"; + PrintHTMLEscaped(Toolchain, PL); + PL << "\n"; + } PL << "\t\n" << "\n";