Skip to content

Commit

Permalink
Merge pull request web-platform-tests#130 from MicrosoftEdge/fix_infi…
Browse files Browse the repository at this point in the history
…nite_loop

Fix potential infinite loop in AssertionError.get_stack(); r=Ms2ger
  • Loading branch information
Ms2ger committed Jun 12, 2015
2 parents d0145b8 + e19ed06 commit 7066fc3
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2048,28 +2048,11 @@ policies and contribution forms [3].
log.removeChild(log.lastChild);
}

var script_prefix = null;
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
var src;
if (scripts[i].src) {
src = scripts[i].src;
} else if (scripts[i].href) {
//SVG case
src = scripts[i].href.baseVal;
}

var matches = src && src.match(/^(.*\/|)testharness\.js$/);
if (matches) {
script_prefix = matches[1];
break;
}
}

if (script_prefix !== null) {
var harness_url = get_harness_url();
if (harness_url !== null) {
var stylesheet = output_document.createElementNS(xhtml_ns, "link");
stylesheet.setAttribute("rel", "stylesheet");
stylesheet.setAttribute("href", script_prefix + "testharness.css");
stylesheet.setAttribute("href", harness_url + "testharness.css");
var heads = output_document.getElementsByTagName("head");
if (heads.length) {
heads[0].appendChild(stylesheet);
Expand Down Expand Up @@ -2425,25 +2408,38 @@ policies and contribution forms [3].

AssertionError.prototype.get_stack = function() {
var stack = new Error().stack;
// IE11 does not initialize 'Error.stack' until the object is thrown.
if (!stack) {
try {
throw new Error();
} catch (e) {
stack = e.stack;
}
}

var lines = stack.split("\n");
var rv = [];
var re = /\/resources\/testharness\.js/;

// Create a pattern to match stack frames originating within testharness.js. These include the
// script URL, followed by the line/col (e.g., '/resources/testharness.js:120:21').
var re = new RegExp(get_script_url() + ":\\d+:\\d+");

// Some browsers include a preamble that specifies the type of the error object. Skip this by
// advancing until we find the first stack frame originating from testharness.js.
var i = 0;
// Fire remove any preamble that doesn't match the regexp
while (!re.test(lines[i])) {
i++
while (!re.test(lines[i]) && i < lines.length) {
i++;
}
// Then remove top frames in testharness.js itself
while (re.test(lines[i])) {
i++

// Then skip the top frames originating from testharness.js to begin the stack at the test code.
while (re.test(lines[i]) && i < lines.length) {
i++;
}

// Paranoid check that we didn't skip all frames. If so, return the original stack unmodified.
if (i >= lines.length) {
return stack;
}

return lines.slice(i).join("\n");
}

Expand Down Expand Up @@ -2491,7 +2487,7 @@ policies and contribution forms [3].
Array.prototype.push.apply(array, items);
}

function forEach (array, callback, thisObj)
function forEach(array, callback, thisObj)
{
for (var i = 0; i < array.length; i++) {
if (array.hasOwnProperty(i)) {
Expand Down Expand Up @@ -2535,11 +2531,42 @@ policies and contribution forms [3].
}
}

/** Returns the 'src' URL of the first <script> tag in the page to include the file 'testharness.js'. */
function get_script_url()
{
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
var src;
if (scripts[i].src) {
src = scripts[i].src;
} else if (scripts[i].href) {
//SVG case
src = scripts[i].href.baseVal;
}

var matches = src && src.match(/^(.*\/|)testharness\.js$/);
if (matches) {
return src;
}
}
return undefined;
}

/** Returns the URL path at which the files for testharness.js are assumed to reside (e.g., '/resources/').
The path is derived from inspecting the 'src' of the <script> tag that included 'testharness.js'. */
function get_harness_url()
{
var script_url = get_script_url();

// Exclude the 'testharness.js' file from the returned path, but '+ 1' to include the trailing slash.
return script_url ? script_url.slice(0, script_url.lastIndexOf('/') + 1) : undefined;
}

function supports_post_message(w)
{
var supports;
var type;
// Given IE implements postMessage across nested iframes but not across
// Given IE implements postMessage across nested iframes but not across
// windows or tabs, you can't infer cross-origin communication from the presence
// of postMessage on the current window object only.
//
Expand Down

0 comments on commit 7066fc3

Please sign in to comment.