From 6ae0e6d5fb2cff91f9aef9b19adbc144fe8d20ff Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 5 Mar 2026 12:50:03 -0800 Subject: [PATCH] Don't crash when given an empty input filename. (#184718) Commands such as `clang -- ''` hit two different crash bugs: a buffer overflow caused by using a `memcmp` that might be larger than the input, and a bogus assert in the option parser when attempting typo correction. --- clang/lib/Driver/Driver.cpp | 2 +- clang/test/Driver/empty_arg.c | 2 ++ llvm/lib/Option/OptTable.cpp | 2 -- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 clang/test/Driver/empty_arg.c diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index afa1884d94b7..420340aaab88 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -3082,7 +3082,7 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, InputTypeArg->claim(); // stdin must be handled specially. - if (memcmp(Value, "-", 2) == 0) { + if (strcmp(Value, "-") == 0) { if (IsFlangMode()) { Ty = types::TY_Fortran; } else if (IsDXCMode()) { diff --git a/clang/test/Driver/empty_arg.c b/clang/test/Driver/empty_arg.c new file mode 100644 index 000000000000..94ed8f13cbec --- /dev/null +++ b/clang/test/Driver/empty_arg.c @@ -0,0 +1,2 @@ +// RUN: not %clang -- "" 2>&1 | FileCheck %s +// CHECK: error: no such file or directory: '' diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp index 065036cedc2a..8444675b847e 100644 --- a/llvm/lib/Option/OptTable.cpp +++ b/llvm/lib/Option/OptTable.cpp @@ -264,8 +264,6 @@ unsigned OptTable::internalFindNearest( StringRef Option, std::string &NearestString, unsigned MinimumLength, unsigned MaximumDistance, std::function ExcludeOption) const { - assert(!Option.empty()); - // Consider each [option prefix + option name] pair as a candidate, finding // the closest match. unsigned BestDistance =