From 2769d4010e409bc08e0eb196f5eae9908f7b460e Mon Sep 17 00:00:00 2001 From: Keyhan Vakil Date: Sat, 1 Oct 2022 03:23:16 +0000 Subject: [PATCH] src: don't crash on invalid script positions This "fixes" the crash reported in #422, in the sense that you no longer get a crash. However the printing does not actually work, i.e. you currently get an error like this: ```console (lldb) v8 i -s 0x2196b1a09a29 error: Invalid source range, start_pos=3108, len=-3098, source_len=10 ``` I'm deeming this better than crashing. We should really never be crashing as the coredump might be incomplete/partially corrupted. (Also, we already know function printing on v16 doesn't work right now.) --- src/llv8.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/llv8.cc b/src/llv8.cc index c37c0294..af1a4b59 100644 --- a/src/llv8.cc +++ b/src/llv8.cc @@ -362,8 +362,15 @@ std::string JSFunction::GetSource(Error& err) { } int64_t len = end_pos - start_pos; - std::string res = source_str.substr(start_pos, len); + // Make sure the substr isn't out of range + if (start_pos < 0 || len < 0 || start_pos + len > source_len) { + err = Error::Failure("Invalid source range, start_pos=%" PRId64 + ", len=%" PRId64 ", source_len=%" PRId64, + start_pos, len, source_len); + return std::string(); + } + std::string res = source_str.substr(start_pos, len); return res; }