teemperor created this revision.

Currently clang segfaults when invoked with `clang --autocomplete=`.
This patch adds the necessary boundary checks and some tests for corner cases 
like this.


Repository:
  rL LLVM

https://reviews.llvm.org/D37465

Files:
  lib/Driver/Driver.cpp
  test/Driver/autocomplete.c


Index: test/Driver/autocomplete.c
===================================================================
--- test/Driver/autocomplete.c
+++ test/Driver/autocomplete.c
@@ -2,6 +2,19 @@
 // autocompletion. You may have to update tests in this file when you
 // add/modify flags, change HelpTexts or the values of some flags.
 
+// Some corner cases.
+// RUN: %clang --autocomplete= | FileCheck %s -check-prefix=ALL_FLAGS
+// RUN: %clang --autocomplete=# | FileCheck %s -check-prefix=ALL_FLAGS
+// Let's pick some example flags that are hopefully unlikely to change.
+// ALL_FLAGS: -fast
+// ALL_FLAGS: -fastcp
+// ALL_FLAGS: -fastf
+// Just test that this doesn't crash:
+// RUN: %clang --autocomplete=,
+// RUN: %clang --autocomplete==
+// RUN: %clang --autocomplete=,,
+// RUN: %clang --autocomplete=-
+
 // RUN: %clang --autocomplete=-fsyn | FileCheck %s -check-prefix=FSYN
 // FSYN: -fsyntax-only
 // RUN: %clang --autocomplete=-std= | FileCheck %s -check-prefix=STD
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1165,12 +1165,10 @@
   unsigned short DisableFlags =
       options::NoDriverOption | options::Unsupported | options::Ignored;
   // We want to show cc1-only options only when clang is invoked as "clang
-  // -cc1".
-  // When clang is invoked as "clang -cc1", we add "#" to the beginning of an
-  // --autocomplete
-  // option so that the clang driver can distinguish whether it is requested to
-  // show cc1-only options or not.
-  if (PassedFlags[0] == '#') {
+  // -cc1". When clang is invoked as "clang -cc1", we add "#" to the beginning
+  // of an --autocomplete  option so that the clang driver can distinguish
+  // whether it is requested to show cc1-only options or not.
+  if (PassedFlags.size() > 0 && PassedFlags[0] == '#') {
     DisableFlags &= ~options::NoDriverOption;
     PassedFlags = PassedFlags.substr(1);
   }


Index: test/Driver/autocomplete.c
===================================================================
--- test/Driver/autocomplete.c
+++ test/Driver/autocomplete.c
@@ -2,6 +2,19 @@
 // autocompletion. You may have to update tests in this file when you
 // add/modify flags, change HelpTexts or the values of some flags.
 
+// Some corner cases.
+// RUN: %clang --autocomplete= | FileCheck %s -check-prefix=ALL_FLAGS
+// RUN: %clang --autocomplete=# | FileCheck %s -check-prefix=ALL_FLAGS
+// Let's pick some example flags that are hopefully unlikely to change.
+// ALL_FLAGS: -fast
+// ALL_FLAGS: -fastcp
+// ALL_FLAGS: -fastf
+// Just test that this doesn't crash:
+// RUN: %clang --autocomplete=,
+// RUN: %clang --autocomplete==
+// RUN: %clang --autocomplete=,,
+// RUN: %clang --autocomplete=-
+
 // RUN: %clang --autocomplete=-fsyn | FileCheck %s -check-prefix=FSYN
 // FSYN: -fsyntax-only
 // RUN: %clang --autocomplete=-std= | FileCheck %s -check-prefix=STD
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1165,12 +1165,10 @@
   unsigned short DisableFlags =
       options::NoDriverOption | options::Unsupported | options::Ignored;
   // We want to show cc1-only options only when clang is invoked as "clang
-  // -cc1".
-  // When clang is invoked as "clang -cc1", we add "#" to the beginning of an
-  // --autocomplete
-  // option so that the clang driver can distinguish whether it is requested to
-  // show cc1-only options or not.
-  if (PassedFlags[0] == '#') {
+  // -cc1". When clang is invoked as "clang -cc1", we add "#" to the beginning
+  // of an --autocomplete  option so that the clang driver can distinguish
+  // whether it is requested to show cc1-only options or not.
+  if (PassedFlags.size() > 0 && PassedFlags[0] == '#') {
     DisableFlags &= ~options::NoDriverOption;
     PassedFlags = PassedFlags.substr(1);
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to