Skip to content

Commit 2769d40

Browse files
kvakilNo9
authored andcommitted
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.)
1 parent bc857f0 commit 2769d40

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/llv8.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,15 @@ std::string JSFunction::GetSource(Error& err) {
362362
}
363363
int64_t len = end_pos - start_pos;
364364

365-
std::string res = source_str.substr(start_pos, len);
365+
// Make sure the substr isn't out of range
366+
if (start_pos < 0 || len < 0 || start_pos + len > source_len) {
367+
err = Error::Failure("Invalid source range, start_pos=%" PRId64
368+
", len=%" PRId64 ", source_len=%" PRId64,
369+
start_pos, len, source_len);
370+
return std::string();
371+
}
366372

373+
std::string res = source_str.substr(start_pos, len);
367374
return res;
368375
}
369376

0 commit comments

Comments
 (0)