Skip to content

Commit 4376228

Browse files
committed
[MERGE chakra-core#118] Fix max length condition + prevent assert crash
Merge pull request chakra-core#118 from obastemur:fix_assert_uintmax 1 - `UINT_MAX/3 < MAXLONG` 2 - x86/? max `size_t` == UINT_MAX Current `if` statement wouldn’t work for x86/? arch. Perhaps it wasn’t an issue thanks to following `assert`. So, success of `if` statement prevents the necessity for following `Assert`. As a result, application would throw instead of `assert` crash.
2 parents 7a25f4e + 18005c4 commit 4376228

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

lib/jsrt/JsrtSourceHolder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ namespace Js
3333
Js::JavascriptError::ThrowOutOfMemoryError(nullptr);
3434
}
3535

36-
size_t cbUtf8Buffer = (length + 1) * 3;
37-
if (cbUtf8Buffer > UINT_MAX)
36+
// `length` should not be bigger than MAXLONG
37+
// UINT_MAX / 3 < MAXLONG
38+
size_t cbUtf8Buffer = ((UINT_MAX / 3) - 1 > length) ? (length + 1) * 3 : UINT_MAX;
39+
if (cbUtf8Buffer >= UINT_MAX)
3840
{
3941
Js::JavascriptError::ThrowOutOfMemoryError(nullptr);
4042
}
@@ -49,7 +51,6 @@ namespace Js
4951
*utf8Script = HeapNewArray(utf8char_t, cbUtf8Buffer);
5052
}
5153

52-
Assert(length < MAXLONG);
5354
*utf8Length = utf8::EncodeIntoAndNullTerminate(*utf8Script, script, static_cast<charcount_t>(length));
5455
*scriptLength = length;
5556

0 commit comments

Comments
 (0)