Skip to content

Commit c4c2208

Browse files
committed
[MERGE #5623 @duongnhn] fixes #5572 clear unused exception for WScript.LoadScript
Merge pull request #5623 from duongnhn:user/duongn/clear-exception-wscript we try to convert inputs to string but for some invalid input, this call sets and records exception along with return error code to context thread. In this case, context thread need to clear unused exception before set and record new exception.
2 parents a21da6b + 13bf408 commit c4c2208

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

bin/ch/WScriptJsrt.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ JsValueRef WScriptJsrt::LoadScriptHelper(JsValueRef callee, bool isConstructCall
381381
Error:
382382
if (errorCode != JsNoError)
383383
{
384+
// check and clear exception if any
385+
bool hasException;
386+
if (ChakraRTInterface::JsHasException(&hasException) == JsNoError && hasException)
387+
{
388+
JsValueRef unusedException = JS_INVALID_REFERENCE;
389+
ChakraRTInterface::JsGetAndClearException(&unusedException);
390+
unusedException;
391+
}
392+
384393
JsValueRef errorObject;
385394
JsValueRef errorMessageString;
386395

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
7+
8+
var tests = [
9+
{
10+
name: "Expect exception with invalid file name to WScript.LoadScript",
11+
body: function () {
12+
assert.throws(function () {
13+
WScript.LoadScript(``, ``, {
14+
toString: function () {
15+
func_0();
16+
}}
17+
);
18+
}, Error,
19+
"Should throw for invalid input to WScript.LoadScript",
20+
"ScriptError");
21+
}
22+
},
23+
{
24+
name: "Expect exception with invalid type to WScript.LoadScript",
25+
body: function () {
26+
assert.throws(function () {
27+
WScript.LoadScript(``, {
28+
toString: function () {
29+
func_0();
30+
}}, ``
31+
);
32+
}, Error,
33+
"Should throw for invalid input to WScript.LoadScript",
34+
"ScriptError");
35+
}
36+
},
37+
{
38+
name: "Expect exception with invalid content to WScript.LoadScript",
39+
body: function () {
40+
assert.throws(function () {
41+
WScript.LoadScript({
42+
toString: function () {
43+
func_0();
44+
}}, ``, ``
45+
);
46+
}, Error,
47+
"Should throw for invalid input to WScript.LoadScript",
48+
"ScriptError");
49+
}
50+
},
51+
{
52+
name: "Expect exception with invalid file name to WScript.LoadModule",
53+
body: function () {
54+
assert.throws(function () {
55+
WScript.LoadModule(``, ``, {
56+
toString: function () {
57+
func_0();
58+
}}
59+
);
60+
}, Error,
61+
"Should throw for invalid input to WScript.LoadModule",
62+
"ScriptError");
63+
}
64+
},
65+
{
66+
name: "Expect exception with invalid type to WScript.LoadModule",
67+
body: function () {
68+
assert.throws(function () {
69+
WScript.LoadModule(``, {
70+
toString: function () {
71+
func_0();
72+
}}, ``
73+
);
74+
}, Error,
75+
"Should throw for invalid input to WScript.LoadModule",
76+
"ScriptError");
77+
}
78+
},
79+
{
80+
name: "Expect exception with invalid content to WScript.LoadModule",
81+
body: function () {
82+
assert.throws(function () {
83+
WScript.LoadModule({
84+
toString: function () {
85+
func_0();
86+
}}, ``, ``
87+
);
88+
}, Error,
89+
"Should throw for invalid input to WScript.LoadModule",
90+
"ScriptError");
91+
}
92+
},
93+
];
94+
95+
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

test/Bugs/rlexe.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,10 @@
537537
<tags>exclude_jshost</tags>
538538
</default>
539539
</test>
540+
<test>
541+
<default>
542+
<files>bug_5572_wscript_loadscript_loadmodule.js</files>
543+
<compile-flags>-args summary -endargs</compile-flags>
544+
</default>
545+
</test>
540546
</regress-exe>

0 commit comments

Comments
 (0)