From a962b40f0bcaee07296836e175112602c58e439f Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 19 Nov 2010 21:14:29 +0000 Subject: Fix a use after free. Patch by Frits van Bommel. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119842 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index ae66110..38ef084 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -765,6 +765,15 @@ void cl::ParseCommandLineOptions(int argc, char **argv, } } + // Now that we know if -debug is specified, we can use it. + // Note that if ReadResponseFiles == true, this must be done before the + // memory allocated for the expanded command line is free()d below. + DEBUG(dbgs() << "Args: "; + for (int i = 0; i < argc; ++i) + dbgs() << argv[i] << ' '; + dbgs() << '\n'; + ); + // Free all of the memory allocated to the map. Command line options may only // be processed once! Opts.clear(); @@ -779,12 +788,6 @@ void cl::ParseCommandLineOptions(int argc, char **argv, free(*i); } - DEBUG(dbgs() << "Args: "; - for (int i = 0; i < argc; ++i) - dbgs() << argv[i] << ' '; - dbgs() << '\n'; - ); - // If we had an error processing our arguments, don't let the program execute if (ErrorParsing) exit(1); } -- cgit v1.1 From 1f6efa3996dd1929fbc129203ce5009b620e6969 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Mon, 29 Nov 2010 18:16:10 +0000 Subject: Merge System into Support. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120298 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 38ef084..a99e46d 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -23,8 +23,8 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetRegistry.h" -#include "llvm/System/Host.h" -#include "llvm/System/Path.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/Path.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" -- cgit v1.1 From 333fb04506233255f10d8095c9e2de5e5f0fdc6f Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 9 Dec 2010 17:36:48 +0000 Subject: Support/MemoryBuffer: Replace all uses of std::string *ErrMsg with error_code &ec. And fix clients. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121379 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index a99e46d..e856509 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -22,6 +22,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" #include "llvm/Target/TargetRegistry.h" #include "llvm/Support/Host.h" #include "llvm/Support/Path.h" @@ -464,8 +465,9 @@ static void ExpandResponseFiles(unsigned argc, char** argv, if (FileStat && FileStat->getSize() != 0) { // Mmap the response file into memory. + error_code ec; OwningPtr - respFilePtr(MemoryBuffer::getFile(respFile.c_str())); + respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec)); // If we could open the file, parse its contents, otherwise // pass the @file option verbatim. -- cgit v1.1 From 3ff9563c3e391954b2e224afcf8b2b0fcc3888aa Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 16 Dec 2010 03:29:14 +0000 Subject: MemoryBuffer now return an error_code and returns a OwningPtr via an out parm. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121958 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index e856509..373a1a2 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -464,11 +464,6 @@ static void ExpandResponseFiles(unsigned argc, char** argv, const sys::FileStatus *FileStat = respFile.getFileStatus(); if (FileStat && FileStat->getSize() != 0) { - // Mmap the response file into memory. - error_code ec; - OwningPtr - respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec)); - // If we could open the file, parse its contents, otherwise // pass the @file option verbatim. @@ -477,7 +472,9 @@ static void ExpandResponseFiles(unsigned argc, char** argv, // itself contain additional @file options; any such options will be // processed recursively.") - if (respFilePtr != 0) { + // Mmap the response file into memory. + OwningPtr respFilePtr; + if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) { ParseCStringVector(newArgv, respFilePtr->getBufferStart()); continue; } -- cgit v1.1 From b3127bb06a9b2a95a587bbe555c2ea0d83e4eaba Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Sat, 18 Dec 2010 00:19:10 +0000 Subject: Support/PathV1: Deprecate getLast. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122116 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 373a1a2..6f5d9dd 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -505,7 +505,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, } // Copy the program name into ProgName, making sure not to overflow it. - std::string ProgName = sys::Path(argv[0]).getLast(); + std::string ProgName = sys::path::filename(argv[0]); size_t Len = std::min(ProgName.size(), size_t(79)); memcpy(ProgramName, ProgName.data(), Len); ProgramName[Len] = '\0'; -- cgit v1.1 From f4fb66acae1ae9a296c091a7a857c1d01953b444 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Tue, 18 Jan 2011 01:59:24 +0000 Subject: Support/CommandLine: Add "Did you mean" print for mismatched operands. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123717 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 6f5d9dd..2b4fba8 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -180,6 +180,34 @@ static Option *LookupOption(StringRef &Arg, StringRef &Value, return I->second; } +/// LookupNearestOption - Lookup the closest match to the option specified by +/// the specified option on the command line. If there is a value specified +/// (after an equal sign) return that as well. This assumes that leading dashes +/// have already been stripped. +static Option *LookupNearestOption(StringRef Arg, + const StringMap &OptionsMap) { + // Reject all dashes. + if (Arg.empty()) return 0; + + // Split on any equal sign. + StringRef LHS = Arg.split('=').first; + + // Find the closest match. + Option *Best = 0; + unsigned BestDistance = 0; + for (StringMap::const_iterator it = OptionsMap.begin(), + ie = OptionsMap.end(); it != ie; ++it) { + unsigned Distance = StringRef(it->second->ArgStr).edit_distance( + Arg, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance); + if (!Best || Distance < BestDistance) { + Best = it->second; + BestDistance = Distance; + } + } + + return Best; +} + /// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that /// does special handling of cl::CommaSeparated options. static bool CommaSeparateAndAddOccurence(Option *Handler, unsigned pos, @@ -571,6 +599,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, bool DashDashFound = false; // Have we read '--'? for (int i = 1; i < argc; ++i) { Option *Handler = 0; + Option *NearestHandler = 0; StringRef Value; StringRef ArgName = ""; @@ -644,12 +673,22 @@ void cl::ParseCommandLineOptions(int argc, char **argv, if (Handler == 0) Handler = HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing, Opts); + + // Otherwise, look for the closest available option to report to the user + // in the upcoming error. + if (Handler == 0 && SinkOpts.empty()) + NearestHandler = LookupNearestOption(ArgName, Opts); } if (Handler == 0) { if (SinkOpts.empty()) { errs() << ProgramName << ": Unknown command line argument '" << argv[i] << "'. Try: '" << argv[0] << " -help'\n"; + + // If we know a near match, report it as well. + errs() << ProgramName << ": Did you mean '-" + << NearestHandler->ArgStr << "'?\n"; + ErrorParsing = true; } else { for (SmallVectorImpl::iterator I = SinkOpts.begin(), -- cgit v1.1 From c98d82a7f0a2a0a8b939c611954d59bab677bfe7 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Mon, 24 Jan 2011 17:27:17 +0000 Subject: Support/CommandLine: Fix LookupNearestOption to also search extra option names. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124124 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'lib/Support/CommandLine.cpp') diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 2b4fba8..7e74499 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -185,7 +185,8 @@ static Option *LookupOption(StringRef &Arg, StringRef &Value, /// (after an equal sign) return that as well. This assumes that leading dashes /// have already been stripped. static Option *LookupNearestOption(StringRef Arg, - const StringMap &OptionsMap) { + const StringMap &OptionsMap, + const char *&NearestString) { // Reject all dashes. if (Arg.empty()) return 0; @@ -197,11 +198,21 @@ static Option *LookupNearestOption(StringRef Arg, unsigned BestDistance = 0; for (StringMap::const_iterator it = OptionsMap.begin(), ie = OptionsMap.end(); it != ie; ++it) { - unsigned Distance = StringRef(it->second->ArgStr).edit_distance( - Arg, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance); - if (!Best || Distance < BestDistance) { - Best = it->second; - BestDistance = Distance; + Option *O = it->second; + SmallVector OptionNames; + O->getExtraOptionNames(OptionNames); + if (O->ArgStr[0]) + OptionNames.push_back(O->ArgStr); + + for (size_t i = 0, e = OptionNames.size(); i != e; ++i) { + StringRef Name = OptionNames[i]; + unsigned Distance = StringRef(Name).edit_distance( + Arg, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance); + if (!Best || Distance < BestDistance) { + Best = O; + NearestString = OptionNames[i]; + BestDistance = Distance; + } } } @@ -600,6 +611,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, for (int i = 1; i < argc; ++i) { Option *Handler = 0; Option *NearestHandler = 0; + const char *NearestHandlerString = 0; StringRef Value; StringRef ArgName = ""; @@ -677,7 +689,8 @@ void cl::ParseCommandLineOptions(int argc, char **argv, // Otherwise, look for the closest available option to report to the user // in the upcoming error. if (Handler == 0 && SinkOpts.empty()) - NearestHandler = LookupNearestOption(ArgName, Opts); + NearestHandler = LookupNearestOption(ArgName, Opts, + NearestHandlerString); } if (Handler == 0) { @@ -685,9 +698,11 @@ void cl::ParseCommandLineOptions(int argc, char **argv, errs() << ProgramName << ": Unknown command line argument '" << argv[i] << "'. Try: '" << argv[0] << " -help'\n"; - // If we know a near match, report it as well. - errs() << ProgramName << ": Did you mean '-" - << NearestHandler->ArgStr << "'?\n"; + if (NearestHandler) { + // If we know a near match, report it as well. + errs() << ProgramName << ": Did you mean '-" + << NearestHandlerString << "'?\n"; + } ErrorParsing = true; } else { -- cgit v1.1