Commit 11cd2317 authored by Jakob Kummerow's avatar Jakob Kummerow Committed by V8 LUCI CQ

[bigint] Truncate huge error messages

When an attempt to parse a huge string to a BigInt fails, then
including the entire string in it makes the exception's message
unwieldy, so this patch puts only the first 1000 characters of
such invalid strings into the exception message.

Bug: chromium:1245239
Change-Id: I2c62f0d34256653ba67da9666e8c5a1a4bbe0599
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3133142
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarMathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76619}
parent eb7e8fb5
......@@ -338,7 +338,7 @@ class FromStringAccumulator {
// So for sufficiently large N, setting max_digits=N here will not actually
// allow parsing BigInts with N digits. We can fix that if/when anyone cares.
explicit FromStringAccumulator(int max_digits)
: max_digits_(std::max(max_digits - kStackParts, kStackParts)) {}
: max_digits_(std::max(max_digits, kStackParts)) {}
// Step 2: Call this method to read all characters.
// {Char} should be a character type, such as uint8_t or uint16_t.
......
......@@ -1107,8 +1107,19 @@ MaybeHandle<BigInt> BigInt::FromObject(Isolate* isolate, Handle<Object> obj) {
if (isolate->has_pending_exception()) {
return MaybeHandle<BigInt>();
} else {
Handle<String> str = Handle<String>::cast(obj);
constexpr int kMaxRenderedLength = 1000;
if (str->length() > kMaxRenderedLength) {
Factory* factory = isolate->factory();
Handle<String> prefix =
factory->NewProperSubString(str, 0, kMaxRenderedLength);
Handle<SeqTwoByteString> ellipsis =
factory->NewRawTwoByteString(1).ToHandleChecked();
ellipsis->SeqTwoByteStringSet(0, 0x2026);
str = factory->NewConsString(prefix, ellipsis).ToHandleChecked();
}
THROW_NEW_ERROR(isolate,
NewSyntaxError(MessageTemplate::kBigIntFromObject, obj),
NewSyntaxError(MessageTemplate::kBigIntFromObject, str),
BigInt);
}
}
......
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const kLongString = 100_000; // Bigger than kMaxRenderedLength in bigint.cc.
const str = 'z'.repeat(kLongString);
try {
BigInt(str);
assertUnreachable("should have thrown");
} catch (e) {
assertTrue(e instanceof SyntaxError);
assertTrue(e.message.startsWith("Cannot convert zzz"));
// Despite adding "Cannot convert", the overall message is shorter than
// the invalid string, because it only includes a prefix of the string.
assertTrue(e.message.length < kLongString);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment