summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRomans Kasperovics <romans.kasperovics@mongodb.com>2023-02-01 15:30:36 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-02-02 04:41:26 +0000
commit9bdd17de7c41ba7cdf7ec330b911d1a141e46d94 (patch)
tree88372fcf7be22539cb9e1d9abf7b1e45af2eaf61
parent669b1c04d1a1fec3ee59893750f4c7df507672f7 (diff)
downloadmongo-9bdd17de7c41ba7cdf7ec330b911d1a141e46d94.tar.gz
SERVER-72722 Truncate long uncaught JavaScript error messages to 10KB
-rw-r--r--src/mongo/scripting/mozjs/implscope.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/mongo/scripting/mozjs/implscope.cpp b/src/mongo/scripting/mozjs/implscope.cpp
index 73c541e6161..243a1273212 100644
--- a/src/mongo/scripting/mozjs/implscope.cpp
+++ b/src/mongo/scripting/mozjs/implscope.cpp
@@ -108,6 +108,12 @@ const int kMaxBytesBeforeGC = 0xffffffff;
const int kStackChunkSize = 8192;
/**
+ * Maximum size in bytes of an error string. It should be smaller than 'BufferMaxSize' as it may
+ * share the buffer with error code, call stack, etc.
+ */
+constexpr size_t kMaxErrorStringSize = logv2::constants::kDefaultMaxAttributeOutputSizeKB * 1024;
+
+/**
* Runtime's can race on first creation (on some function statics), so we just
* serialize the initial Runtime creation.
*/
@@ -1110,7 +1116,9 @@ bool MozJSImplScope::_checkErrorState(bool success, bool reportError, bool asser
if (!JS::GetPrivate(excn.toObjectOrNull())) {
ss << "uncaught exception: ";
}
- ss << ValueWriter(_context, excn).toString();
+ JSStringWrapper jsstr;
+ ss << str::UTF8SafeTruncation(ValueWriter(_context, excn).toStringData(&jsstr),
+ kMaxErrorStringSize);
auto stackStr = ObjectWrapper(_context, excn).getString(InternedString::stack);
auto status =
jsExceptionToStatus(_context, excn, ErrorCodes::JSInterpreterFailure, ss);
@@ -1134,7 +1142,10 @@ bool MozJSImplScope::_checkErrorState(bool success, bool reportError, bool asser
} else {
str::stream ss;
- ss << "uncaught exception: " << ValueWriter(_context, excn).toString();
+ JSStringWrapper jsstr;
+ ss << "uncaught exception: "
+ << str::UTF8SafeTruncation(ValueWriter(_context, excn).toStringData(&jsstr),
+ kMaxErrorStringSize);
_status = Status(ErrorCodes::UnknownError, ss);
}
} else {