[clangd] Don't crash on extremely large JSON messages.
Found by clangd-fuzzer.
llvm-svn: 316774
diff --git a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
index 5e48a07..0aa1f39 100644
--- a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
+++ b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
@@ -196,6 +196,15 @@
}
}
+ // Guard against large messages. This is usually a bug in the client code
+ // and we don't want to crash downstream because of it.
+ if (ContentLength > 1 << 30) { // 1024M
+ In.ignore(ContentLength);
+ Out.log("Skipped overly large message of " + Twine(ContentLength) +
+ " bytes.\n");
+ continue;
+ }
+
if (ContentLength > 0) {
// Now read the JSON. Insert a trailing null byte as required by the YAML
// parser.