[messages] Cap string length we try to format

When building the error message for a TypeError when e.g.
a non-callable is called, we should avoid running into the
max string length. Printing many megabytes there isn't going
to be useful anyway.

Fixed: v8:10963
Change-Id: Ief89800f660bdd48585f84c3e3d4ece21b02b760
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2438068
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Auto-Submit: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70226}
This commit is contained in:
Jakob Kummerow 2020-09-30 14:21:51 +02:00 committed by Commit Bot
parent 44355d750a
commit 5f1ae37a01

View File

@ -1234,7 +1234,18 @@ Handle<String> BuildDefaultCallSite(Isolate* isolate, Handle<Object> object) {
builder.AppendString(Object::TypeOf(isolate, object));
if (object->IsString()) {
builder.AppendCString(" \"");
builder.AppendString(Handle<String>::cast(object));
Handle<String> string = Handle<String>::cast(object);
// This threshold must be sufficiently far below String::kMaxLength that
// the {builder}'s result can never exceed that limit.
constexpr int kMaxPrintedStringLength = 100;
if (string->length() <= kMaxPrintedStringLength) {
builder.AppendString(string);
} else {
string = isolate->factory()->NewProperSubString(string, 0,
kMaxPrintedStringLength);
builder.AppendString(string);
builder.AppendCString("<...>");
}
builder.AppendCString("\"");
} else if (object->IsNull(isolate)) {
builder.AppendCString(" ");